Skip to content
Browse files

prefixing of each line of output

  • Loading branch information...
1 parent 3af3410 commit 833a3cd7ccae3b56d5fd0182b8211bc973303a5e @lachie committed Dec 10, 2010
Showing with 29 additions and 5 deletions.
  1. +1 −1 examples/eg_helper.rb
  2. +12 −0 examples/shell.eg.rb
  3. +16 −4 lib/common_mob/shell.rb
View
2 examples/eg_helper.rb
@@ -3,7 +3,7 @@
require 'exemplor'
require 'angry_hash'
-root = Pathname(__FILE__).dirname.parent
+root = Pathname('../..').expand_path(__FILE__)
$LOAD_PATH << root+'examples'
$LOAD_PATH << root+'lib'
View
12 examples/shell.eg.rb
@@ -0,0 +1,12 @@
+require 'eg_helper'
+
+eg.helpers do
+ include CommonMob::ShellHelper
+end
+
+eg 'shelly' do
+ cmd = "echo hello; echo mars >&2; echo world"
+ Show( sh(cmd ).execute )
+ Show( sh(cmd, :prefix => "[OXX]" ).execute )
+ Show( sh(cmd, :prefix => {:err => "[OXX]", :out => "[MXX]"} ).execute )
+end
View
20 lib/common_mob/shell.rb
@@ -39,15 +39,27 @@ def initialize(*args,&block)
def execute
error,out = nil,nil
+ # XXX interleave out and err
rv = popen4(options) {|pid,stdin,stdout,stderr|
out = stdout.read
error = stderr.read
}
- # TODO print as debug
+ if prefix = options[:prefix]
+ if Hash === prefix
+ pre_err = prefix[:err]
+ pre_out = prefix[:out]
+ else
+ pre_err = prefix
+ pre_out = prefix
+ end
- rv.stderr = error
- rv.stdout = out
+ rv.stderr = error.gsub(/^(.*)$/, "#{pre_err}\\1")
+ rv.stdout = out.gsub(/^(.*)$/ , "#{pre_out}\\1")
+ else
+ rv.stderr = error
+ rv.stdout = out
+ end
rv
end
@@ -76,7 +88,7 @@ def ok?
def ensure_ok!
unless ok?
- ex = ShellError.new("unable to run options=#{options.inspect}\noutput=#{stdout}\nerror=#{stderr}")
+ ex = ShellError.new("unable to run\noptions=#{options.pretty_inspect}\noutput=#{stdout}\nerror=#{stderr}")
ex.result = self
raise(ex)
end

0 comments on commit 833a3cd

Please sign in to comment.
Something went wrong with that request. Please try again.