]> git.cworth.org Git - sup/commitdiff
move pipe-to-process functionality to a helper method in Mode
authorWilliam Morgan <wmorgan-sup@masanjin.net>
Sat, 22 Dec 2007 18:02:28 +0000 (10:02 -0800)
committerWilliam Morgan <wmorgan-sup@masanjin.net>
Sat, 22 Dec 2007 18:02:28 +0000 (10:02 -0800)
and away from thread-view-mode!

lib/sup/mode.rb
lib/sup/modes/thread-view-mode.rb

index 4f2c28c70d42d797c78dccfb40c40b3e7cac103a..217a617bd05c4b5653c6bf90fd78052d26ec32e1 100644 (file)
@@ -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
index e4c72e5cab1a4f476aa1ce466b416399300e42e0..a280440c73ba69b83f3da84ba42f58ec31033aea 100644 (file)
@@ -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