Class: Present

Inherits:
Object
  • Object
show all
Defined in:
lib/present.rb,
lib/present/page.rb

Defined Under Namespace

Classes: Page

Constant Summary collapse

VERSION =
'0.1.1'
TIOCGWINSZ =
0x5413

Instance Method Summary collapse

Constructor Details

#initialize(path, timer = 5, page = 1) ⇒ Present

Returns a new instance of Present.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/present.rb', line 12

def initialize(path, timer = 5, page = 1)
  @source = path
  @script = NKF.nkf('-e', open(path).read)
  @stanzas = @script.split(/\n\n+/)
  str = [0, 0, 0, 0].pack("SSSS")
  if $stdin.ioctl(TIOCGWINSZ, str) >= 0 then
    @rows, @cols, xpixels, ypixels = str.unpack("SSSS")
  else
    @rows, @cols = 13, 40
  end
  @pages = @stanzas.map{|stanza| Page.new(stanza, self)}
  @page = page.to_i - 1
  @timer = timer.to_i * 60
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



210
211
212
# File 'lib/present.rb', line 210

def method_missing(method, *args, &block)
  @win.send method, *args, &block
end

Instance Method Details

#caption(text, options = {}) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/present.rb', line 97

def caption(text, options = {})
  return if text.empty?
  font = options[:font] || 'mini'
  text = figlet font, @cols, text
  rows, cols = sizeof(text)
  top = (@rows - rows - 2)/2
  left = (@cols - cols)/2
  @win.attron Ncurses::A_BOLD
  text = text.split("\n").each_with_index do |line, i|
    mvwprintw top + i, left, line
  end
  @win.attroff Ncurses::A_BOLD
  @row = top + rows + 1
end

#center(text, options = {}) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/present.rb', line 83

def center(text, options = {})
  return if text.empty?
  font = options[:font] || 'mini'
  text = figlet font, @cols, text
  rows, cols = sizeof(text)
  top = (@rows - rows)/2
  left = (@cols - cols)/2
  @win.attron Ncurses::A_BOLD
  text = text.split("\n").each_with_index do |line, i|
    mvwprintw top + i, left, line
  end
  @win.attroff Ncurses::A_BOLD
end

#color(front, back = Ncurses::COLOR_BLACK) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/present.rb', line 129

def color(front, back = Ncurses::COLOR_BLACK)
  if front =~ /^\d+$/
    pair_id = front.to_i
  elsif front.is_a?(String)
    front = eval "Ncurses::COLOR_#{front.upcase}"
  end
  if back.is_a?(String)
    back = eval "Ncurses::COLOR_#{back.upcase}"
  end
  pair_id ||= register_pair front, back
  @win.color_set pair_id, nil
end

#do_go(page) ⇒ Object



214
215
216
# File 'lib/present.rb', line 214

def do_go(page)
  @page = [[0, page.to_i - 1].max, @pages.size - 1].min
end

#do_qObject



224
225
226
# File 'lib/present.rb', line 224

def do_q
  exit
end

#do_shObject



218
219
220
221
222
# File 'lib/present.rb', line 218

def do_sh
  Ncurses.endwin
  system ENV['SHELL'] || 'sh'
  Ncurses.resetty
end

#figlet(font, cols, text) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/present.rb', line 68

def figlet(font, cols, text)
  font = 'term' if mb?(text)
  cmd = Escape.shell_command(['figlet', '-f', "#{font}.flf", '-w', cols.to_s])
  IO.popen(cmd, 'r+') do |io|
    io.write text
    io.close_write
    io.read
  end
end

#line(text, options = {}) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/present.rb', line 112

def line(text, options = {})
  return @row += 1 if text.empty?
  font = options[:font] || 'term'
  @row = options[:row] if options[:row]
  @row += options[:padding] if options[:padding]
  text = figlet font, @cols, text
  rows, cols = sizeof(text)
  left = options[:left] || (@cols - cols)/2
  left = @cols - cols - options[:right] if right = options[:right]
  @win.attron Ncurses::A_BOLD if options[:bold]
  text = text.split("\n").each_with_index do |line, i|
    mvwprintw @row + i, left, line
  end
  @win.attroff Ncurses::A_BOLD if options[:bold]
  @row += rows + options[:margin].to_i
end

#mb?(text) ⇒ Boolean

Returns:

  • (Boolean)


159
160
161
# File 'lib/present.rb', line 159

def mb?(text)
  text.split(//u).size != text.length
end

#paginateObject



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/present.rb', line 163

def paginate
  @status.mvwprintw 0, 0, " " * @cols
  pages = @pages.size
  k = pages.to_s.length
  j = 2 + k*2
  @status.mvwprintw 0, 0, "%#{k}d/%#{k}d|" % [@page + 1, pages]
  cols = @cols - 6 - j
  q = cols % pages
  r = q > 0 ? pages / q : 0
  w = [cols / pages, 1].max
  w += 1 if r > 0 && @page % r == r - 1
  x = (@page*cols + pages/2) / pages
  @status.mvwprintw 0, x + j, "*" * w
  time = Time.now.to_i - @start.to_i
  min, sec = time / 60, time % 60
  @status.mvwprintw 0, cols + j, "|%02d:%02d" % [min, sec]
  @status.refresh
end

#register_pair(front, back) ⇒ Object



202
203
204
205
206
207
208
# File 'lib/present.rb', line 202

def register_pair(front, back)
  key = [front, back]
  return @colors[key] if @colors[key]
  pair_id = @colors.size + 1
  Ncurses.init_pair pair_id, front, back
  @colors[key] = pair_id
end

#reloadObject



78
79
80
81
# File 'lib/present.rb', line 78

def reload
  Ncurses.endwin
  exec "#{$0} #{@source} #{@timer/60} #{@page + 1}"
end

#scan_commandObject



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/present.rb', line 182

def scan_command
  @status.mvwprintw 0, 0, " "*@cols
  @status.mvwprintw 0, 0, ':'
  buf = []
  begin
    case input = @status.getch
    when 10, 13; break
    when 27; return nil
    when 127; buf.pop
    else buf << input
    end
    @status.mvwprintw 0, 1, " "*(@cols - 1)
    @status.mvwprintw 0, 1, buf.pack('C*')
    @status.refresh
  end while input > 0
  command, *args = buf.pack('C*').split(/\s+/)
  command = "do_#{command}"
  send command, *args rescue nil if respond_to? command
end

#sizeof(text) ⇒ Object



153
154
155
156
157
# File 'lib/present.rb', line 153

def sizeof(text)
  lines = text.split("\n")
  cols = lines.map{|line| line.length}.max
  [lines.size, cols]
end

#startObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/present.rb', line 27

def start
  Ncurses.initscr
  Ncurses.raw
  Ncurses.cbreak
  Ncurses.noecho
  Ncurses.start_color
  Ncurses.refresh
  @win = Ncurses::WINDOW.new @rows - 1, @cols, 0, 0
  @win.keypad true
  Ncurses.nodelay @win, 1
  @status = Ncurses::WINDOW.new 1, @cols + 1, @rows - 1, 0
  @status.color_set 1, nil
  @start = Time.now
  @colors = {}
  register_pair Ncurses::COLOR_BLACK, Ncurses::COLOR_WHITE
  while page = @pages[@page]
    @row = @rows / 4
    paginate
    page.start
    
    begin
      input = @win.getch
      case input
      when 27, ?q; return # exit
      when ?:; scan_command
      when ?k, ?h, 259, 260; @page = [@page - 1, 0].max
      when ?<; @page = 0
      when ?>; @page = @pages.size - 1
      when ?j, ?l, 10, 13, 32, 258, 261
        @page = [@page + 1, @pages.size - 1].min
      when ?r; reload
      else
        sleep 0.1
        paginate
      end
    end while input == -1
  end
ensure
  Ncurses.endwin
end

#wait(time) ⇒ Object



142
143
144
145
146
147
148
149
150
151
# File 'lib/present.rb', line 142

def wait(time)
  if time == 0.0
    case input = @status.getch
    when ?j, 32, 10, 13, 27
    else Ncurses.ungetch input
    end
  else
    sleep time
  end
end