7 stdscr.getmaxyx lame, lamer
13 stdscr.getmaxyx lame, lamer
19 stdscr.getyx lame, lamer
23 def mutex; @mutex ||= Mutex.new; end
24 def sync &b; mutex.synchronize(&b); end
26 ## magically, this stuff seems to work now. i could swear it didn't
29 if IO.select([$stdin], nil, nil, 1)
36 module_function :rows, :cols, :curx, :nonblocking_getch, :mutex, :sync
39 KEY_CANCEL = ?\a # ctrl-g
46 attr_reader :mode, :x, :y, :width, :height, :title
48 bool_accessor :force_to_top
50 def initialize window, mode, width, height, opts={}
55 @title = opts[:title] || ""
56 @force_to_top = opts[:force_to_top] || false
57 @x, @y, @width, @height = 0, 0, width, height
60 def content_height; @height - 1; end
61 def content_width; @width; end
64 return if cols == @width && rows == @height
68 mode.resize rows, cols
77 def mark_dirty; @dirty = true; end
90 ## s nil means a blank line!
91 def write y, x, s, opts={}
92 return if x >= @width || y >= @height
94 @w.attrset Colormap.color_for(opts[:color] || :none, opts[:highlight])
97 @w.mvaddstr y, x, s[0 ... maxl]
98 unless s.length >= maxl || opts[:no_fill]
99 @w.mvaddstr(y, x + s.length, " " * (maxl - s.length))
108 write @height - 1, 0, " [#{mode.name}] #{title} #{mode.status}",
109 :color => :status_color
128 attr_reader :focus_buf
136 @minibuf_mutex = Mutex.new
139 @shelled = @asking = false
141 self.class.i_am_the_instance self
144 def buffers; @name_map.to_a; end
147 raise ArgumentError, "buffer not on stack: #{buf.inspect}" unless @buffers.member? buf
148 return if buf == @focus_buf
149 @focus_buf.blur if @focus_buf
154 def raise_to_front buf
155 raise ArgumentError, "buffer not on stack: #{buf.inspect}" unless @buffers.member? buf
158 if @buffers.length > 0 && @buffers.last.force_to_top?
159 @buffers.insert(-2, buf)
167 ## we reset force_to_top when rolling buffers. this is so that the
168 ## human can actually still move buffers around, while still
169 ## programmatically being able to pop stuff up in the middle of
170 ## drawing a window without worrying about covering it up.
172 ## if we ever start calling roll_buffers programmatically, we will
173 ## have to change this. but it's not clear that we will ever actually
176 @buffers.last.force_to_top = false
177 raise_to_front @buffers.first
180 def roll_buffers_backwards
181 return unless @buffers.length > 1
182 @buffers.last.force_to_top = false
183 raise_to_front @buffers[@buffers.length - 2]
187 @focus_buf && @focus_buf.mode.handle_input(c)
190 def exists? n; @name_map.member? n; end
191 def [] n; @name_map[n]; end
193 raise ArgumentError, "duplicate buffer name" if b && @name_map.member?(n)
194 raise ArgumentError, "title must be a string" unless n.is_a? String
198 def completely_redraw_screen
204 draw_screen :sync => false
208 def draw_screen opts={}
211 Ncurses.mutex.lock unless opts[:sync] == false
213 ## disabling this for the time being, to help with debugging
214 ## (currently we only have one buffer visible at a time).
215 ## TODO: reenable this if we allow multiple buffers
216 false && @buffers.inject(@dirty) do |dirty, buf|
217 buf.resize Ncurses.rows - minibuf_lines, Ncurses.cols
218 #dirty ? buf.draw : buf.redraw
226 buf.resize Ncurses.rows - minibuf_lines, Ncurses.cols
227 @dirty ? buf.draw : buf.redraw
230 draw_minibuf :sync => false unless opts[:skip_minibuf]
234 Ncurses.refresh if opts[:refresh]
235 Ncurses.mutex.unlock unless opts[:sync] == false
238 ## gets the mode from the block, which is only called if the buffer
239 ## doesn't already exist. this is useful in the case that generating
240 ## the mode is expensive, as it often is.
241 def spawn_unless_exists title, opts={}
242 if @name_map.member? title
243 raise_to_front @name_map[title] unless opts[:hidden]
246 spawn title, mode, opts
251 def spawn title, mode, opts={}
252 raise ArgumentError, "title must be a string" unless title.is_a? String
255 while @name_map.member? realtitle
256 realtitle = "#{title} <#{num}>"
260 width = opts[:width] || Ncurses.cols
261 height = opts[:height] || Ncurses.rows - 1
263 ## since we are currently only doing multiple full-screen modes,
264 ## use stdscr for each window. once we become more sophisticated,
265 ## we may need to use a new Ncurses::WINDOW
267 ## w = Ncurses::WINDOW.new(height, width, (opts[:top] || 0),
268 ## (opts[:left] || 0))
270 b = Buffer.new w, mode, width, height, :title => realtitle, :force_to_top => (opts[:force_to_top] || false)
272 @name_map[realtitle] = b
276 focus_on b unless @focus_buf
283 ## requires the mode to have #done? and #value methods
284 def spawn_modal title, mode, opts={}
285 b = spawn title, mode, opts
289 c = Ncurses.nonblocking_getch
290 next unless c # getch timeout
291 break if c == Ncurses::KEY_CANCEL
301 def kill_all_buffers_safely
302 until @buffers.empty?
303 ## inbox mode always claims it's unkillable. we'll ignore it.
304 return false unless @buffers.last.mode.is_a?(InboxMode) || @buffers.last.mode.killable?
305 kill_buffer @buffers.last
310 def kill_buffer_safely buf
311 return false unless buf.mode.killable?
317 kill_buffer @buffers.first until @buffers.empty?
321 raise ArgumentError, "buffer not on stack: #{buf.inspect}" unless @buffers.member? buf
325 @name_map.delete buf.title
326 @focus_buf = nil if @focus_buf == buf
328 ## TODO: something intelligent here
329 ## for now I will simply prohibit killing the inbox buffer.
331 raise_to_front @buffers.last
335 def ask_with_completions domain, question, completions, default=nil
336 ask domain, question, default do |s|
337 completions.select { |x| x =~ /^#{s}/i }.map { |x| [x.downcase, x] }
341 def ask_many_with_completions domain, question, completions, default=nil, sep=" "
342 ask domain, question, default do |partial|
344 case partial.gsub(/#{sep}+/, sep)
349 when /^(.*#{sep})?(.+?)$/
352 raise "william screwed up completion: #{partial.inspect}"
355 completions.select { |x| x =~ /^#{target}/i }.map { |x| [prefix + x, x] }
359 ## returns an ARRAY of filenames!
360 def ask_for_filenames domain, question, default=nil
361 answer = ask domain, question, default do |s|
362 if s =~ /(~([^\s\/]*))/ # twiddle directory expansion
364 name = $2.empty? ? Etc.getlogin : $2
365 dir = Etc.getpwnam(name).dir rescue nil
367 [[s.sub(full, dir), "~#{name}"]]
369 users.select { |u| u =~ /^#{name}/ }.map do |u|
370 [s.sub("~#{name}", "~#{u}"), "~#{u}"]
373 else # regular filename completion
374 Dir["#{s}*"].sort.map do |fn|
375 suffix = File.directory?(fn) ? "/" : ""
376 [fn + suffix, File.basename(fn) + suffix]
384 spawn_modal "file browser", FileBrowserMode.new
385 elsif File.directory?(answer)
386 spawn_modal "file browser", FileBrowserMode.new(answer)
395 def ask domain, question, default=nil, &block
396 raise "impossible!" if @asking
399 @textfields[domain] ||= TextField.new Ncurses.stdscr, Ncurses.rows - 1, 0, Ncurses.cols
400 tf = @textfields[domain]
403 ## this goddamn ncurses form shit is a fucking 1970's nightmare.
404 ## jesus christ. the exact sequence of ncurses events that needs
405 ## to happen in order to display a form and have the entire screen
406 ## not disappear and have the cursor in the right place is TOO
407 ## FUCKING COMPLICATED.
409 tf.activate question, default, &block
411 draw_screen :skip_minibuf => true, :sync => false
416 Ncurses.sync { Ncurses.refresh }
419 c = Ncurses.nonblocking_getch
420 next unless c # getch timeout
421 break unless tf.handle_input c # process keystroke
423 if tf.new_completions?
424 kill_buffer completion_buf if completion_buf
426 shorts = tf.completions.map { |full, short| short }
427 prefix_len = shorts.shared_prefix.length
429 mode = CompletionMode.new shorts, :header => "Possible completions for \"#{tf.value}\": ", :prefix_len => prefix_len
430 completion_buf = spawn "<completions>", mode, :height => 10
432 draw_screen :skip_minibuf => true
434 elsif tf.roll_completions?
435 completion_buf.mode.roll
436 draw_screen :skip_minibuf => true
440 Ncurses.sync { Ncurses.refresh }
443 Ncurses.sync { tf.deactivate }
444 kill_buffer completion_buf if completion_buf
451 ## some pretty lame code in here!
452 def ask_getch question, accept=nil
453 accept = accept.split(//).map { |x| x[0] } if accept
458 Ncurses.move Ncurses.rows - 1, question.length + 1
466 key = Ncurses.nonblocking_getch or next
467 if key == Ncurses::KEY_CANCEL
469 elsif (accept && accept.member?(key)) || !accept
480 draw_screen :sync => false
487 ## returns true (y), false (n), or nil (ctrl-g / cancel)
488 def ask_yes_or_no question
489 case(r = ask_getch question, "ynYN")
500 @minibuf_mutex.synchronize do
503 @minibuf_stack.compact.size, 1].max
507 def draw_minibuf opts={}
509 @minibuf_mutex.synchronize do
510 m = @minibuf_stack.compact
511 m << @flash if @flash
515 Ncurses.mutex.lock unless opts[:sync] == false
516 Ncurses.attrset Colormap.color_for(:none)
517 adj = @asking ? 2 : 1
518 m.each_with_index do |s, i|
519 Ncurses.mvaddstr Ncurses.rows - i - adj, 0, s + (" " * [Ncurses.cols - s.length, 0].max)
521 Ncurses.refresh if opts[:refresh]
522 Ncurses.mutex.unlock unless opts[:sync] == false
528 @minibuf_mutex.synchronize do
530 id ||= @minibuf_stack.length
531 @minibuf_stack[id] = s
535 draw_screen :refresh => true
537 draw_minibuf :refresh => true
550 def erase_flash; @flash = nil; end
554 draw_screen :refresh => true
557 ## a little tricky because we can't just delete_at id because ids
558 ## are relative (they're positions into the array).
560 @minibuf_mutex.synchronize do
561 @minibuf_stack[id] = nil
562 if id == @minibuf_stack.length - 1
564 break if @minibuf_stack[i]
565 @minibuf_stack.delete_at i
570 draw_screen :refresh => true
573 def shell_out command
589 while(u = Etc.getpwent)