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 ## returns an array of labels
396 def ask_for_labels domain, question, default_labels, forbidden_labels=[]
397 default = default_labels.join(" ")
398 default += " " unless default.empty?
400 applyable_labels = (LabelManager.applyable_labels - forbidden_labels).map { |l| LabelManager.string_for l }.sort_by { |s| s.downcase }
402 answer = BufferManager.ask_many_with_completions domain, question, applyable_labels, default
406 user_labels = answer.split(/\s+/).map { |l| l.intern }
407 user_labels.each do |l|
408 if forbidden_labels.include?(l) || LabelManager::RESERVED_LABELS.include?(l)
409 BufferManager.flash "'#{l}' is a reserved label!"
417 def ask domain, question, default=nil, &block
418 raise "impossible!" if @asking
421 @textfields[domain] ||= TextField.new Ncurses.stdscr, Ncurses.rows - 1, 0, Ncurses.cols
422 tf = @textfields[domain]
425 ## this goddamn ncurses form shit is a fucking 1970's nightmare.
426 ## jesus christ. the exact sequence of ncurses events that needs
427 ## to happen in order to display a form and have the entire screen
428 ## not disappear and have the cursor in the right place is TOO
429 ## FUCKING COMPLICATED.
431 tf.activate question, default, &block
433 draw_screen :skip_minibuf => true, :sync => false
438 Ncurses.sync { Ncurses.refresh }
441 c = Ncurses.nonblocking_getch
442 next unless c # getch timeout
443 break unless tf.handle_input c # process keystroke
445 if tf.new_completions?
446 kill_buffer completion_buf if completion_buf
448 shorts = tf.completions.map { |full, short| short }
449 prefix_len = shorts.shared_prefix.length
451 mode = CompletionMode.new shorts, :header => "Possible completions for \"#{tf.value}\": ", :prefix_len => prefix_len
452 completion_buf = spawn "<completions>", mode, :height => 10
454 draw_screen :skip_minibuf => true
456 elsif tf.roll_completions?
457 completion_buf.mode.roll
458 draw_screen :skip_minibuf => true
462 Ncurses.sync { Ncurses.refresh }
465 Ncurses.sync { tf.deactivate }
466 kill_buffer completion_buf if completion_buf
473 ## some pretty lame code in here!
474 def ask_getch question, accept=nil
475 accept = accept.split(//).map { |x| x[0] } if accept
480 Ncurses.move Ncurses.rows - 1, question.length + 1
488 key = Ncurses.nonblocking_getch or next
489 if key == Ncurses::KEY_CANCEL
491 elsif (accept && accept.member?(key)) || !accept
502 draw_screen :sync => false
509 ## returns true (y), false (n), or nil (ctrl-g / cancel)
510 def ask_yes_or_no question
511 case(r = ask_getch question, "ynYN")
522 @minibuf_mutex.synchronize do
525 @minibuf_stack.compact.size, 1].max
529 def draw_minibuf opts={}
531 @minibuf_mutex.synchronize do
532 m = @minibuf_stack.compact
533 m << @flash if @flash
537 Ncurses.mutex.lock unless opts[:sync] == false
538 Ncurses.attrset Colormap.color_for(:none)
539 adj = @asking ? 2 : 1
540 m.each_with_index do |s, i|
541 Ncurses.mvaddstr Ncurses.rows - i - adj, 0, s + (" " * [Ncurses.cols - s.length, 0].max)
543 Ncurses.refresh if opts[:refresh]
544 Ncurses.mutex.unlock unless opts[:sync] == false
550 @minibuf_mutex.synchronize do
552 id ||= @minibuf_stack.length
553 @minibuf_stack[id] = s
557 draw_screen :refresh => true
559 draw_minibuf :refresh => true
572 def erase_flash; @flash = nil; end
576 draw_screen :refresh => true
579 ## a little tricky because we can't just delete_at id because ids
580 ## are relative (they're positions into the array).
582 @minibuf_mutex.synchronize do
583 @minibuf_stack[id] = nil
584 if id == @minibuf_stack.length - 1
586 break if @minibuf_stack[i]
587 @minibuf_stack.delete_at i
592 draw_screen :refresh => true
595 def shell_out command
611 while(u = Etc.getpwent)