8 stdscr.getmaxyx lame, lamer
14 stdscr.getmaxyx lame, lamer
20 stdscr.getyx lame, lamer
24 def mutex; @mutex ||= Mutex.new; end
25 def sync &b; mutex.synchronize(&b); end
27 ## magically, this stuff seems to work now. i could swear it didn't
30 if IO.select([$stdin], nil, nil, 1)
37 module_function :rows, :cols, :curx, :nonblocking_getch, :mutex, :sync
39 remove_const :KEY_ENTER
40 remove_const :KEY_CANCEL
43 KEY_CANCEL = 7 # ctrl-g
50 attr_reader :mode, :x, :y, :width, :height, :title
52 bool_accessor :force_to_top
54 def initialize window, mode, width, height, opts={}
59 @title = opts[:title] || ""
60 @force_to_top = opts[:force_to_top] || false
61 @x, @y, @width, @height = 0, 0, width, height
64 def content_height; @height - 1; end
65 def content_width; @width; end
68 return if cols == @width && rows == @height
72 mode.resize rows, cols
81 def mark_dirty; @dirty = true; end
94 ## s nil means a blank line!
95 def write y, x, s, opts={}
96 return if x >= @width || y >= @height
98 @w.attrset Colormap.color_for(opts[:color] || :none, opts[:highlight])
101 @w.mvaddstr y, x, s[0 ... maxl]
102 unless s.length >= maxl || opts[:no_fill]
103 @w.mvaddstr(y, x + s.length, " " * (maxl - s.length))
112 write @height - 1, 0, " [#{mode.name}] #{title} #{mode.status}",
113 :color => :status_color
132 attr_reader :focus_buf
140 @minibuf_mutex = Mutex.new
143 @shelled = @asking = false
145 self.class.i_am_the_instance self
148 def buffers; @name_map.to_a; end
151 raise ArgumentError, "buffer not on stack: #{buf.inspect}" unless @buffers.member? buf
152 return if buf == @focus_buf
153 @focus_buf.blur if @focus_buf
158 def raise_to_front buf
159 raise ArgumentError, "buffer not on stack: #{buf.inspect}" unless @buffers.member? buf
162 if @buffers.length > 0 && @buffers.last.force_to_top?
163 @buffers.insert(-2, buf)
171 ## we reset force_to_top when rolling buffers. this is so that the
172 ## human can actually still move buffers around, while still
173 ## programmatically being able to pop stuff up in the middle of
174 ## drawing a window without worrying about covering it up.
176 ## if we ever start calling roll_buffers programmatically, we will
177 ## have to change this. but it's not clear that we will ever actually
180 @buffers.last.force_to_top = false
181 raise_to_front @buffers.first
184 def roll_buffers_backwards
185 return unless @buffers.length > 1
186 @buffers.last.force_to_top = false
187 raise_to_front @buffers[@buffers.length - 2]
191 @focus_buf && @focus_buf.mode.handle_input(c)
194 def exists? n; @name_map.member? n; end
195 def [] n; @name_map[n]; end
197 raise ArgumentError, "duplicate buffer name" if b && @name_map.member?(n)
198 raise ArgumentError, "title must be a string" unless n.is_a? String
202 def completely_redraw_screen
208 draw_screen :sync => false
212 def draw_screen opts={}
215 Ncurses.mutex.lock unless opts[:sync] == false
217 ## disabling this for the time being, to help with debugging
218 ## (currently we only have one buffer visible at a time).
219 ## TODO: reenable this if we allow multiple buffers
220 false && @buffers.inject(@dirty) do |dirty, buf|
221 buf.resize Ncurses.rows - minibuf_lines, Ncurses.cols
222 #dirty ? buf.draw : buf.redraw
230 buf.resize Ncurses.rows - minibuf_lines, Ncurses.cols
231 @dirty ? buf.draw : buf.redraw
234 draw_minibuf :sync => false unless opts[:skip_minibuf]
238 Ncurses.refresh if opts[:refresh]
239 Ncurses.mutex.unlock unless opts[:sync] == false
242 ## gets the mode from the block, which is only called if the buffer
243 ## doesn't already exist. this is useful in the case that generating
244 ## the mode is expensive, as it often is.
245 def spawn_unless_exists title, opts={}
246 if @name_map.member? title
247 raise_to_front @name_map[title] unless opts[:hidden]
250 spawn title, mode, opts
255 def spawn title, mode, opts={}
256 raise ArgumentError, "title must be a string" unless title.is_a? String
259 while @name_map.member? realtitle
260 realtitle = "#{title} <#{num}>"
264 width = opts[:width] || Ncurses.cols
265 height = opts[:height] || Ncurses.rows - 1
267 ## since we are currently only doing multiple full-screen modes,
268 ## use stdscr for each window. once we become more sophisticated,
269 ## we may need to use a new Ncurses::WINDOW
271 ## w = Ncurses::WINDOW.new(height, width, (opts[:top] || 0),
272 ## (opts[:left] || 0))
274 b = Buffer.new w, mode, width, height, :title => realtitle, :force_to_top => (opts[:force_to_top] || false)
276 @name_map[realtitle] = b
280 focus_on b unless @focus_buf
287 ## requires the mode to have #done? and #value methods
288 def spawn_modal title, mode, opts={}
289 b = spawn title, mode, opts
293 c = Ncurses.nonblocking_getch
294 next unless c # getch timeout
295 break if c == Ncurses::KEY_CANCEL
305 def kill_all_buffers_safely
306 until @buffers.empty?
307 ## inbox mode always claims it's unkillable. we'll ignore it.
308 return false unless @buffers.last.mode.is_a?(InboxMode) || @buffers.last.mode.killable?
309 kill_buffer @buffers.last
314 def kill_buffer_safely buf
315 return false unless buf.mode.killable?
321 kill_buffer @buffers.first until @buffers.empty?
325 raise ArgumentError, "buffer not on stack: #{buf.inspect}" unless @buffers.member? buf
329 @name_map.delete buf.title
330 @focus_buf = nil if @focus_buf == buf
332 ## TODO: something intelligent here
333 ## for now I will simply prohibit killing the inbox buffer.
335 raise_to_front @buffers.last
339 def ask_with_completions domain, question, completions, default=nil
340 ask domain, question, default do |s|
341 completions.select { |x| x =~ /^#{s}/i }.map { |x| [x.downcase, x] }
345 def ask_many_with_completions domain, question, completions, default=nil, sep=" "
346 ask domain, question, default do |partial|
348 case partial#.gsub(/#{sep}+/, sep)
353 when /^(.*#{sep})?(.+?)$/
356 raise "william screwed up completion: #{partial.inspect}"
359 completions.select { |x| x =~ /^#{target}/i }.map { |x| [prefix + x, x] }
363 ## returns an ARRAY of filenames!
364 def ask_for_filenames domain, question, default=nil
365 answer = ask domain, question, default do |s|
366 if s =~ /(~([^\s\/]*))/ # twiddle directory expansion
368 name = $2.empty? ? Etc.getlogin : $2
369 dir = Etc.getpwnam(name).dir rescue nil
371 [[s.sub(full, dir), "~#{name}"]]
373 users.select { |u| u =~ /^#{name}/ }.map do |u|
374 [s.sub("~#{name}", "~#{u}"), "~#{u}"]
377 else # regular filename completion
378 Dir["#{s}*"].sort.map do |fn|
379 suffix = File.directory?(fn) ? "/" : ""
380 [fn + suffix, File.basename(fn) + suffix]
388 spawn_modal "file browser", FileBrowserMode.new
389 elsif File.directory?(answer)
390 spawn_modal "file browser", FileBrowserMode.new(answer)
399 ## returns an array of labels
400 def ask_for_labels domain, question, default_labels, forbidden_labels=[]
401 default_labels = default_labels - forbidden_labels - LabelManager::RESERVED_LABELS
402 default = default_labels.join(" ")
403 default += " " unless default.empty?
405 applyable_labels = (LabelManager.applyable_labels - forbidden_labels).map { |l| LabelManager.string_for l }.sort_by { |s| s.downcase }
407 answer = ask_many_with_completions domain, question, applyable_labels, default
411 user_labels = answer.split(/\s+/).map { |l| l.intern }
412 user_labels.each do |l|
413 if forbidden_labels.include?(l) || LabelManager::RESERVED_LABELS.include?(l)
414 BufferManager.flash "'#{l}' is a reserved label!"
421 def ask_for_contacts domain, question, default_contacts=[]
422 default = default_contacts.map { |s| s.to_s }.join(" ")
423 default += " " unless default.empty?
425 all_contacts = ContactManager.contacts.map { |c| [ContactManager.alias_for(c), c.longname, c.email] }.flatten.uniq.sort
427 answer = BufferManager.ask_many_with_completions domain, question, all_contacts, default, /\s*,\s*/
430 answer.split_on_commas.map { |x| ContactManager.contact_for(x.downcase) || PersonManager.person_for(x) }
435 def ask domain, question, default=nil, &block
436 raise "impossible!" if @asking
439 @textfields[domain] ||= TextField.new Ncurses.stdscr, Ncurses.rows - 1, 0, Ncurses.cols
440 tf = @textfields[domain]
443 ## this goddamn ncurses form shit is a fucking 1970's nightmare.
444 ## jesus christ. the exact sequence of ncurses events that needs
445 ## to happen in order to display a form and have the entire screen
446 ## not disappear and have the cursor in the right place is TOO
447 ## FUCKING COMPLICATED.
449 tf.activate question, default, &block
451 draw_screen :skip_minibuf => true, :sync => false
456 Ncurses.sync { Ncurses.refresh }
459 c = Ncurses.nonblocking_getch
460 next unless c # getch timeout
461 break unless tf.handle_input c # process keystroke
463 if tf.new_completions?
464 kill_buffer completion_buf if completion_buf
466 shorts = tf.completions.map { |full, short| short }
467 prefix_len = shorts.shared_prefix.length
469 mode = CompletionMode.new shorts, :header => "Possible completions for \"#{tf.value}\": ", :prefix_len => prefix_len
470 completion_buf = spawn "<completions>", mode, :height => 10
472 draw_screen :skip_minibuf => true
474 elsif tf.roll_completions?
475 completion_buf.mode.roll
476 draw_screen :skip_minibuf => true
480 Ncurses.sync { Ncurses.refresh }
483 Ncurses.sync { tf.deactivate }
484 kill_buffer completion_buf if completion_buf
491 ## some pretty lame code in here!
492 def ask_getch question, accept=nil
493 accept = accept.split(//).map { |x| x[0] } if accept
498 Ncurses.move Ncurses.rows - 1, question.length + 1
506 key = Ncurses.nonblocking_getch or next
507 if key == Ncurses::KEY_CANCEL
509 elsif (accept && accept.member?(key)) || !accept
520 draw_screen :sync => false
527 ## returns true (y), false (n), or nil (ctrl-g / cancel)
528 def ask_yes_or_no question
529 case(r = ask_getch question, "ynYN")
540 @minibuf_mutex.synchronize do
543 @minibuf_stack.compact.size, 1].max
547 def draw_minibuf opts={}
549 @minibuf_mutex.synchronize do
550 m = @minibuf_stack.compact
551 m << @flash if @flash
555 Ncurses.mutex.lock unless opts[:sync] == false
556 Ncurses.attrset Colormap.color_for(:none)
557 adj = @asking ? 2 : 1
558 m.each_with_index do |s, i|
559 Ncurses.mvaddstr Ncurses.rows - i - adj, 0, s + (" " * [Ncurses.cols - s.length, 0].max)
561 Ncurses.refresh if opts[:refresh]
562 Ncurses.mutex.unlock unless opts[:sync] == false
568 @minibuf_mutex.synchronize do
570 id ||= @minibuf_stack.length
571 @minibuf_stack[id] = s
575 draw_screen :refresh => true
577 draw_minibuf :refresh => true
590 def erase_flash; @flash = nil; end
594 draw_screen :refresh => true
597 ## a little tricky because we can't just delete_at id because ids
598 ## are relative (they're positions into the array).
600 @minibuf_mutex.synchronize do
601 @minibuf_stack[id] = nil
602 if id == @minibuf_stack.length - 1
604 break if @minibuf_stack[i]
605 @minibuf_stack.delete_at i
610 draw_screen :refresh => true
613 def shell_out command
629 while(u = Etc.getpwent)