]> git.cworth.org Git - sup/blobdiff - lib/sup/mode.rb
Merge branch 'master' into next
[sup] / lib / sup / mode.rb
index 640fbbecbd03f3b0f91df84b23119707d96b7a0d..03deacb53b01ee145c0bf4325b9c347056950578 100644 (file)
@@ -1,3 +1,4 @@
+require 'open3'
 module Redwood
 
 class Mode
@@ -24,26 +25,24 @@ class Mode
   end
 
   def killable?; true; end
+  def unsaved?; false end
   def draw; end
   def focus; end
   def blur; end
+  def cancel_search!; end
+  def in_search?; false end
   def status; ""; end
   def resize rows, cols; end
   def cleanup
     @buffer = nil
   end
 
-  ## turns an input keystroke into an action symbol
   def resolve_input c
-    ## try all keymaps in order of age
-    action = nil
-    klass = self.class
-
-    ancestors.each do |klass|
-      action = @@keymaps.member?(klass) && @@keymaps[klass].action_for(c)
+    ancestors.each do |klass| # try all keymaps in order of ancestry
+      next unless @@keymaps.member?(klass)
+      action = BufferManager.resolve_input_with_keymap c, @@keymaps[klass]
       return action if action
     end
-
     nil
   end
 
@@ -60,7 +59,7 @@ class Mode
       title = "Keybindings from #{Mode.make_name klass.name}"
       s = <<EOS
 #{title}
-#{'-' * title.length}
+#{'-' * title.display_length}
 
 #{km.help_text used_keys}
 EOS
@@ -73,7 +72,8 @@ EOS
     end.compact.join "\n"
   end
 
-  ## helper function
+### helper functions
+
   def save_to_file fn
     if File.exists? fn
       return unless BufferManager.ask_yes_or_no "File exists. Overwrite?"
@@ -81,10 +81,45 @@ EOS
     begin
       File.open(fn, "w") { |f| yield f }
       BufferManager.flash "Successfully wrote #{fn}."
-    rescue SystemCallError => e
+    rescue SystemCallError, IOError => e
       BufferManager.flash "Error writing to file: #{e.message}"
     end
   end
+
+  def pipe_to_process command
+    Open3.popen3(command) do |input, output, error|
+      err, data, * = IO.select [error], [input], nil
+
+      unless err.empty?
+        message = err.first.read
+        if message =~ /^\s*$/
+          warn "error running #{command} (but no error message)"
+          BufferManager.flash "Error running #{command}!"
+        else
+          warn "error running #{command}: #{message}"
+          BufferManager.flash "Error: #{message}"
+        end
+        return
+      end
+
+      data = data.first
+      data.sync = false # buffer input
+
+      yield data
+      data.close # output will block unless input is closed
+
+      ## BUG?: shows errors or output but not both....
+      data, * = IO.select [output, error], nil, nil
+      data = data.first
+
+      if data.eof
+        BufferManager.flash "'#{command}' done!"
+        nil
+      else
+        data.read
+      end
+    end
+  end
 end
 
 end