]> git.cworth.org Git - sup/blobdiff - lib/sup/buffer.rb
hook system
[sup] / lib / sup / buffer.rb
index bb94fbe60ea87107c036c07732b8faf455af5a57..467675a92de35f8cd278c5953901d6681dc945ee 100644 (file)
@@ -1,5 +1,8 @@
+require 'etc'
 require 'thread'
+require 'ncurses'
 
+if defined? Ncurses
 module Ncurses
   def rows
     lame, lamer = [], []
@@ -13,6 +16,12 @@ module Ncurses
     lamer.first
   end
 
+  def curx
+    lame, lamer = [], []
+    stdscr.getyx lame, lamer
+    lamer.first
+  end
+
   def mutex; @mutex ||= Mutex.new; end
   def sync &b; mutex.synchronize(&b); end
 
@@ -26,9 +35,15 @@ module Ncurses
     end
   end
 
-  module_function :rows, :cols, :nonblocking_getch, :mutex, :sync
+  module_function :rows, :cols, :curx, :nonblocking_getch, :mutex, :sync
 
-  KEY_CANCEL = "\a"[0] # ctrl-g
+  remove_const :KEY_ENTER
+  remove_const :KEY_CANCEL
+
+  KEY_ENTER = 10
+  KEY_CANCEL = 7 # ctrl-g
+  KEY_TAB = 9
+end
 end
 
 module Redwood
@@ -206,7 +221,9 @@ class BufferManager
     ## TODO: reenable this if we allow multiple buffers
     false && @buffers.inject(@dirty) do |dirty, buf|
       buf.resize Ncurses.rows - minibuf_lines, Ncurses.cols
-      @dirty ? buf.draw : buf.redraw
+      #dirty ? buf.draw : buf.redraw
+      buf.draw
+      dirty
     end
 
     ## quick hack
@@ -217,6 +234,7 @@ class BufferManager
     end
 
     draw_minibuf :sync => false unless opts[:skip_minibuf]
+
     @dirty = false
     Ncurses.doupdate
     Ncurses.refresh if opts[:refresh]
@@ -268,11 +286,29 @@ class BufferManager
     b
   end
 
+  ## requires the mode to have #done? and #value methods
+  def spawn_modal title, mode, opts={}
+    b = spawn title, mode, opts
+    draw_screen
+
+    until mode.done?
+      c = Ncurses.nonblocking_getch
+      next unless c # getch timeout
+      break if c == Ncurses::KEY_CANCEL
+      mode.handle_input c
+      draw_screen
+      erase_flash
+    end
+
+    kill_buffer b
+    mode.value
+  end
+
   def kill_all_buffers_safely
     until @buffers.empty?
       ## inbox mode always claims it's unkillable. we'll ignore it.
-      return false unless @buffers.first.mode.is_a?(InboxMode) || @buffers.first.mode.killable?
-      kill_buffer @buffers.first
+      return false unless @buffers.last.mode.is_a?(InboxMode) || @buffers.last.mode.killable?
+      kill_buffer @buffers.last
     end
     true
   end
@@ -302,20 +338,117 @@ class BufferManager
     end
   end
 
-  ## not really thread safe.
-  def ask domain, question, default=nil
+  def ask_with_completions domain, question, completions, default=nil
+    ask domain, question, default do |s|
+      completions.select { |x| x =~ /^#{s}/i }.map { |x| [x.downcase, x] }
+    end
+  end
+
+  def ask_many_with_completions domain, question, completions, default=nil, sep=" "
+    ask domain, question, default do |partial|
+      prefix, target = 
+        case partial#.gsub(/#{sep}+/, sep)
+        when /^\s*$/
+          ["", ""]
+        when /^(.+#{sep})$/
+          [$1, ""]
+        when /^(.*#{sep})?(.+?)$/
+          [$1 || "", $2]
+        else
+          raise "william screwed up completion: #{partial.inspect}"
+        end
+
+      completions.select { |x| x =~ /^#{target}/i }.map { |x| [prefix + x, x] }
+    end
+  end
+
+  ## returns an ARRAY of filenames!
+  def ask_for_filenames domain, question, default=nil
+    answer = ask domain, question, default do |s|
+      if s =~ /(~([^\s\/]*))/ # twiddle directory expansion
+        full = $1
+        name = $2.empty? ? Etc.getlogin : $2
+        dir = Etc.getpwnam(name).dir rescue nil
+        if dir
+          [[s.sub(full, dir), "~#{name}"]]
+        else
+          users.select { |u| u =~ /^#{name}/ }.map do |u|
+            [s.sub("~#{name}", "~#{u}"), "~#{u}"]
+          end
+        end
+      else # regular filename completion
+        Dir["#{s}*"].sort.map do |fn|
+          suffix = File.directory?(fn) ? "/" : ""
+          [fn + suffix, File.basename(fn) + suffix]
+        end
+      end
+    end
+
+    if answer
+      answer = 
+        if answer.empty?
+          spawn_modal "file browser", FileBrowserMode.new
+        elsif File.directory?(answer)
+          spawn_modal "file browser", FileBrowserMode.new(answer)
+        else
+          [answer]
+        end
+    end
+
+    answer || []
+  end
+
+  ## returns an array of labels
+  def ask_for_labels domain, question, default_labels, forbidden_labels=[]
+    default_labels = default_labels - forbidden_labels - LabelManager::RESERVED_LABELS
+    default = default_labels.join(" ")
+    default += " " unless default.empty?
+
+    applyable_labels = (LabelManager.applyable_labels - forbidden_labels).map { |l| LabelManager.string_for l }.sort_by { |s| s.downcase }
+
+    answer = ask_many_with_completions domain, question, applyable_labels, default
+
+    return unless answer
+
+    user_labels = answer.split(/\s+/).map { |l| l.intern }
+    user_labels.each do |l|
+      if forbidden_labels.include?(l) || LabelManager::RESERVED_LABELS.include?(l)
+        BufferManager.flash "'#{l}' is a reserved label!"
+        return
+      end
+    end
+    user_labels
+  end
+
+  def ask_for_contacts domain, question, default_contacts=[]
+    default = default_contacts.map { |s| s.to_s }.join(" ")
+    default += " " unless default.empty?
+
+    all_contacts = ContactManager.contacts.map { |c| [ContactManager.alias_for(c), c.longname, c.email] }.flatten.uniq.sort
+
+    answer = BufferManager.ask_many_with_completions domain, question, all_contacts, default, /\s*,\s*/
+
+    if answer
+      answer.split_on_commas.map { |x| ContactManager.contact_for(x.downcase) || PersonManager.person_for(x) }
+    end
+  end
+
+
+  def ask domain, question, default=nil, &block
     raise "impossible!" if @asking
+    @asking = true
 
     @textfields[domain] ||= TextField.new Ncurses.stdscr, Ncurses.rows - 1, 0, Ncurses.cols
     tf = @textfields[domain]
+    completion_buf = nil
 
-    ## this goddamn ncurses form shit is a fucking 1970's
-    ## nightmare. jesus christ. the exact sequence of ncurses events
-    ## that needs to happen in order to display a form and have the
-    ## entire screen not disappear and have the cursor in the right
-    ## place is TOO FUCKING COMPLICATED.
+    ## this goddamn ncurses form shit is a fucking 1970's nightmare.
+    ## jesus christ. the exact sequence of ncurses events that needs
+    ## to happen in order to display a form and have the entire screen
+    ## not disappear and have the cursor in the right place is TOO
+    ## FUCKING COMPLICATED.
     Ncurses.sync do
-      tf.activate question, default
+      tf.activate question, default, &block
       @dirty = true
       draw_screen :skip_minibuf => true, :sync => false
     end
@@ -324,15 +457,37 @@ class BufferManager
     tf.position_cursor
     Ncurses.sync { Ncurses.refresh }
 
-    @asking = true
-    while tf.handle_input(Ncurses.nonblocking_getch); end
-    @asking = false
+    while true
+      c = Ncurses.nonblocking_getch
+      next unless c # getch timeout
+      break unless tf.handle_input c # process keystroke
+
+      if tf.new_completions?
+        kill_buffer completion_buf if completion_buf
+        
+        shorts = tf.completions.map { |full, short| short }
+        prefix_len = shorts.shared_prefix.length
+
+        mode = CompletionMode.new shorts, :header => "Possible completions for \"#{tf.value}\": ", :prefix_len => prefix_len
+        completion_buf = spawn "<completions>", mode, :height => 10
+
+        draw_screen :skip_minibuf => true
+        tf.position_cursor
+      elsif tf.roll_completions?
+        completion_buf.mode.roll
+        draw_screen :skip_minibuf => true
+        tf.position_cursor
+      end
 
-    ret = tf.value
+      Ncurses.sync { Ncurses.refresh }
+    end
+    
     Ncurses.sync { tf.deactivate }
+    kill_buffer completion_buf if completion_buf
     @dirty = true
-
-    ret
+    @asking = false
+    draw_screen
+    tf.value
   end
 
   ## some pretty lame code in here!
@@ -467,5 +622,17 @@ class BufferManager
     end
     @shelled = false
   end
+
+private
+
+  def users
+    unless @users
+      @users = []
+      while(u = Etc.getpwent)
+        @users << u.name
+      end
+    end
+    @users
+  end
 end
 end