9 stdscr.getmaxyx lame, lamer
15 stdscr.getmaxyx lame, lamer
21 stdscr.getyx lame, lamer
25 def mutex; @mutex ||= Mutex.new; end
26 def sync &b; mutex.synchronize(&b); end
28 ## magically, this stuff seems to work now. i could swear it didn't
31 if IO.select([$stdin], nil, nil, 1)
38 module_function :rows, :cols, :curx, :nonblocking_getch, :mutex, :sync
40 remove_const :KEY_ENTER
41 remove_const :KEY_CANCEL
44 KEY_CANCEL = 7 # ctrl-g
52 attr_reader :mode, :x, :y, :width, :height, :title
54 bool_accessor :force_to_top
56 def initialize window, mode, width, height, opts={}
61 @title = opts[:title] || ""
62 @force_to_top = opts[:force_to_top] || false
63 @x, @y, @width, @height = 0, 0, width, height
66 def content_height; @height - 1; end
67 def content_width; @width; end
70 return if cols == @width && rows == @height
74 mode.resize rows, cols
83 def mark_dirty; @dirty = true; end
96 ## s nil means a blank line!
97 def write y, x, s, opts={}
98 return if x >= @width || y >= @height
100 @w.attrset Colormap.color_for(opts[:color] || :none, opts[:highlight])
103 @w.mvaddstr y, x, s[0 ... maxl]
104 unless s.length >= maxl || opts[:no_fill]
105 @w.mvaddstr(y, x + s.length, " " * (maxl - s.length))
114 write @height - 1, 0, " [#{mode.name}] #{title} #{mode.status}",
115 :color => :status_color
134 attr_reader :focus_buf
142 @minibuf_mutex = Mutex.new
145 @shelled = @asking = false
147 self.class.i_am_the_instance self
150 def buffers; @name_map.to_a; end
153 raise ArgumentError, "buffer not on stack: #{buf.inspect}" unless @buffers.member? buf
154 return if buf == @focus_buf
155 @focus_buf.blur if @focus_buf
160 def raise_to_front buf
161 raise ArgumentError, "buffer not on stack: #{buf.inspect}" unless @buffers.member? buf
164 if @buffers.length > 0 && @buffers.last.force_to_top?
165 @buffers.insert(-2, buf)
173 ## we reset force_to_top when rolling buffers. this is so that the
174 ## human can actually still move buffers around, while still
175 ## programmatically being able to pop stuff up in the middle of
176 ## drawing a window without worrying about covering it up.
178 ## if we ever start calling roll_buffers programmatically, we will
179 ## have to change this. but it's not clear that we will ever actually
182 @buffers.last.force_to_top = false
183 raise_to_front @buffers.first
186 def roll_buffers_backwards
187 return unless @buffers.length > 1
188 @buffers.last.force_to_top = false
189 raise_to_front @buffers[@buffers.length - 2]
193 @focus_buf && @focus_buf.mode.handle_input(c)
196 def exists? n; @name_map.member? n; end
197 def [] n; @name_map[n]; end
199 raise ArgumentError, "duplicate buffer name" if b && @name_map.member?(n)
200 raise ArgumentError, "title must be a string" unless n.is_a? String
204 def completely_redraw_screen
210 draw_screen :sync => false
214 def draw_screen opts={}
217 Ncurses.mutex.lock unless opts[:sync] == false
219 ## disabling this for the time being, to help with debugging
220 ## (currently we only have one buffer visible at a time).
221 ## TODO: reenable this if we allow multiple buffers
222 false && @buffers.inject(@dirty) do |dirty, buf|
223 buf.resize Ncurses.rows - minibuf_lines, Ncurses.cols
224 #dirty ? buf.draw : buf.redraw
232 buf.resize Ncurses.rows - minibuf_lines, Ncurses.cols
233 @dirty ? buf.draw : buf.redraw
236 draw_minibuf :sync => false unless opts[:skip_minibuf]
240 Ncurses.refresh if opts[:refresh]
241 Ncurses.mutex.unlock unless opts[:sync] == false
244 ## gets the mode from the block, which is only called if the buffer
245 ## doesn't already exist. this is useful in the case that generating
246 ## the mode is expensive, as it often is.
247 def spawn_unless_exists title, opts={}
248 if @name_map.member? title
249 raise_to_front @name_map[title] unless opts[:hidden]
252 spawn title, mode, opts
257 def spawn title, mode, opts={}
258 raise ArgumentError, "title must be a string" unless title.is_a? String
261 while @name_map.member? realtitle
262 realtitle = "#{title} <#{num}>"
266 width = opts[:width] || Ncurses.cols
267 height = opts[:height] || Ncurses.rows - 1
269 ## since we are currently only doing multiple full-screen modes,
270 ## use stdscr for each window. once we become more sophisticated,
271 ## we may need to use a new Ncurses::WINDOW
273 ## w = Ncurses::WINDOW.new(height, width, (opts[:top] || 0),
274 ## (opts[:left] || 0))
276 b = Buffer.new w, mode, width, height, :title => realtitle, :force_to_top => (opts[:force_to_top] || false)
278 @name_map[realtitle] = b
282 focus_on b unless @focus_buf
289 ## requires the mode to have #done? and #value methods
290 def spawn_modal title, mode, opts={}
291 b = spawn title, mode, opts
295 c = Ncurses.nonblocking_getch
296 next unless c # getch timeout
297 break if c == Ncurses::KEY_CANCEL
307 def kill_all_buffers_safely
308 until @buffers.empty?
309 ## inbox mode always claims it's unkillable. we'll ignore it.
310 return false unless @buffers.last.mode.is_a?(InboxMode) || @buffers.last.mode.killable?
311 kill_buffer @buffers.last
316 def kill_buffer_safely buf
317 return false unless buf.mode.killable?
323 kill_buffer @buffers.first until @buffers.empty?
327 raise ArgumentError, "buffer not on stack: #{buf.inspect}" unless @buffers.member? buf
331 @name_map.delete buf.title
332 @focus_buf = nil if @focus_buf == buf
334 ## TODO: something intelligent here
335 ## for now I will simply prohibit killing the inbox buffer.
337 raise_to_front @buffers.last
341 def ask_with_completions domain, question, completions, default=nil
342 ask domain, question, default do |s|
343 completions.select { |x| x =~ /^#{s}/i }.map { |x| [x.downcase, x] }
347 def ask_many_with_completions domain, question, completions, default=nil, sep=" "
348 ask domain, question, default do |partial|
350 case partial#.gsub(/#{sep}+/, sep)
355 when /^(.*#{sep})?(.+?)$/
358 raise "william screwed up completion: #{partial.inspect}"
361 completions.select { |x| x =~ /^#{target}/i }.map { |x| [prefix + x, x] }
365 def ask_for_filename domain, question, default=nil
366 answer = ask domain, question, default do |s|
367 if s =~ /(~([^\s\/]*))/ # twiddle directory expansion
369 name = $2.empty? ? Etc.getlogin : $2
370 dir = Etc.getpwnam(name).dir rescue nil
372 [[s.sub(full, dir), "~#{name}"]]
374 users.select { |u| u =~ /^#{name}/ }.map do |u|
375 [s.sub("~#{name}", "~#{u}"), "~#{u}"]
378 else # regular filename completion
379 Dir["#{s}*"].sort.map do |fn|
380 suffix = File.directory?(fn) ? "/" : ""
381 [fn + suffix, File.basename(fn) + suffix]
389 spawn_modal "file browser", FileBrowserMode.new
390 elsif File.directory?(answer)
391 spawn_modal "file browser", FileBrowserMode.new(answer)
400 ## returns an array of labels
401 def ask_for_labels domain, question, default_labels, forbidden_labels=[]
402 default_labels = default_labels - forbidden_labels - LabelManager::RESERVED_LABELS
403 default = default_labels.join(" ")
404 default += " " unless default.empty?
406 applyable_labels = (LabelManager.applyable_labels - forbidden_labels).map { |l| LabelManager.string_for l }.sort_by { |s| s.downcase }
408 answer = ask_many_with_completions domain, question, applyable_labels, default
412 user_labels = answer.split(/\s+/).map { |l| l.intern }
413 user_labels.each do |l|
414 if forbidden_labels.include?(l) || LabelManager::RESERVED_LABELS.include?(l)
415 BufferManager.flash "'#{l}' is a reserved label!"
422 def ask_for_contacts domain, question, default_contacts=[]
423 default = default_contacts.map { |s| s.to_s }.join(" ")
424 default += " " unless default.empty?
426 recent = Index.load_contacts(AccountManager.user_emails, :num => 10).map { |c| [c.longname, c.email] }
427 contacts = ContactManager.contacts.map { |c| [ContactManager.alias_for(c), c.longname, c.email] }
429 Redwood::log "recent: #{recent.inspect}"
431 completions = (recent + contacts).flatten.uniq.sort
432 answer = BufferManager.ask_many_with_completions domain, question, completions, default, /\s*,\s*/
435 answer.split_on_commas.map { |x| ContactManager.contact_for(x.downcase) || PersonManager.person_for(x) }
440 def ask domain, question, default=nil, &block
441 raise "impossible!" if @asking
444 @textfields[domain] ||= TextField.new Ncurses.stdscr, Ncurses.rows - 1, 0, Ncurses.cols
445 tf = @textfields[domain]
448 ## this goddamn ncurses form shit is a fucking 1970's nightmare.
449 ## jesus christ. the exact sequence of ncurses events that needs
450 ## to happen in order to display a form and have the entire screen
451 ## not disappear and have the cursor in the right place is TOO
452 ## FUCKING COMPLICATED.
454 tf.activate question, default, &block
456 draw_screen :skip_minibuf => true, :sync => false
461 Ncurses.sync { Ncurses.refresh }
464 c = Ncurses.nonblocking_getch
465 next unless c # getch timeout
466 break unless tf.handle_input c # process keystroke
468 if tf.new_completions?
469 kill_buffer completion_buf if completion_buf
471 shorts = tf.completions.map { |full, short| short }
472 prefix_len = shorts.shared_prefix.length
474 mode = CompletionMode.new shorts, :header => "Possible completions for \"#{tf.value}\": ", :prefix_len => prefix_len
475 completion_buf = spawn "<completions>", mode, :height => 10
477 draw_screen :skip_minibuf => true
479 elsif tf.roll_completions?
480 completion_buf.mode.roll
481 draw_screen :skip_minibuf => true
485 Ncurses.sync { Ncurses.refresh }
488 Ncurses.sync { tf.deactivate }
489 kill_buffer completion_buf if completion_buf
496 ## some pretty lame code in here!
497 def ask_getch question, accept=nil
498 accept = accept.split(//).map { |x| x[0] } if accept
503 Ncurses.move Ncurses.rows - 1, question.length + 1
511 key = Ncurses.nonblocking_getch or next
512 if key == Ncurses::KEY_CANCEL
514 elsif (accept && accept.member?(key)) || !accept
525 draw_screen :sync => false
532 ## returns true (y), false (n), or nil (ctrl-g / cancel)
533 def ask_yes_or_no question
534 case(r = ask_getch question, "ynYN")
545 @minibuf_mutex.synchronize do
548 @minibuf_stack.compact.size, 1].max
552 def draw_minibuf opts={}
554 @minibuf_mutex.synchronize do
555 m = @minibuf_stack.compact
556 m << @flash if @flash
560 Ncurses.mutex.lock unless opts[:sync] == false
561 Ncurses.attrset Colormap.color_for(:none)
562 adj = @asking ? 2 : 1
563 m.each_with_index do |s, i|
564 Ncurses.mvaddstr Ncurses.rows - i - adj, 0, s + (" " * [Ncurses.cols - s.length, 0].max)
566 Ncurses.refresh if opts[:refresh]
567 Ncurses.mutex.unlock unless opts[:sync] == false
573 @minibuf_mutex.synchronize do
575 id ||= @minibuf_stack.length
576 @minibuf_stack[id] = s
580 draw_screen :refresh => true
582 draw_minibuf :refresh => true
595 def erase_flash; @flash = nil; end
599 draw_screen :refresh => true
602 ## a little tricky because we can't just delete_at id because ids
603 ## are relative (they're positions into the array).
605 @minibuf_mutex.synchronize do
606 @minibuf_stack[id] = nil
607 if id == @minibuf_stack.length - 1
609 break if @minibuf_stack[i]
610 @minibuf_stack.delete_at i
615 draw_screen :refresh => true
618 def shell_out command
634 while(u = Etc.getpwent)