sup

A curses threads-with-tags style email client

sup.git

git clone https://supmua.dev/git/sup/
commit e5fed61def6b358044cf1b64382361568c684bf6
parent ae46a4b892a79ab77c3d748a7c42967247bf2a67
Author: William Morgan <wmorgan-sup@masanjin.net>
Date:   Sat, 22 Dec 2007 10:02:28 -0800

move pipe-to-process functionality to a helper method in Mode
and away from thread-view-mode!

Diffstat:
M lib/sup/mode.rb | 38 +++++++++++++++++++++++++++++++++++++-
M lib/sup/modes/thread-view-mode.rb | 39 ++++++++-------------------------------
2 files changed, 45 insertions(+), 32 deletions(-)
diff --git a/lib/sup/mode.rb b/lib/sup/mode.rb
@@ -75,7 +75,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?"
@@ -87,6 +88,41 @@ EOS
       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*$/
+          Redwood::log "error running #{command} (but no error message)"
+          BufferManager.flash "Error running #{command}!"
+        else
+          Redwood::log "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
diff --git a/lib/sup/modes/thread-view-mode.rb b/lib/sup/modes/thread-view-mode.rb
@@ -356,41 +356,18 @@ EOS
     command = BufferManager.ask(:shell, "pipe command: ")
     return if command.nil? || command.empty?
 
-    Open3.popen3(command) do |input, output, error|
-      err, data, * = IO.select [error], [input], nil
-
-      unless err.empty?
-        message = err.first.read
-        if message =~ /^\s*$/
-          Redwood::log "error running #{command} (but no error message)"
-          BufferManager.flash "Error running #{command}!"
-        else
-          Redwood::log "error running #{command}: #{message}"
-          BufferManager.flash "Error: #{message}"
-        end
-        return
-      end
-
-      data = data.first
-      data.sync = false # buffer input
-
+    output = pipe_to_process(command) do |stream|
       if chunk
-        data.print chunk.raw_content
+        stream.print chunk.raw_content
       else
-        message.each_raw_message_line { |l| data.print l }
+        message.each_raw_message_line { |l| stream.print l }
       end
+    end
 
-      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!"
-      else
-        BufferManager.spawn "Output of '#{command}'", TextMode.new(data.read)
-      end
+    if output
+      BufferManager.spawn "Output of '#{command}'", TextMode.new(output)
+    else
+      BufferManager.flash "'#{command}' done!"
     end
   end