From: William Morgan Date: Sat, 22 Dec 2007 18:02:28 +0000 (-0800) Subject: move pipe-to-process functionality to a helper method in Mode X-Git-Url: https://git.cworth.org/git?a=commitdiff_plain;h=e5fed61def6b358044cf1b64382361568c684bf6;p=sup move pipe-to-process functionality to a helper method in Mode and away from thread-view-mode! --- diff --git a/lib/sup/mode.rb b/lib/sup/mode.rb index 4f2c28c..217a617 100644 --- 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 index e4c72e5..a280440 100644 --- 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