From 1de2a15aaf80a0ce7c56caa4fe8d063a95631a74 Mon Sep 17 00:00:00 2001 From: akr Date: Sun, 7 Dec 2008 02:49:18 +0000 Subject: * lib/open3.rb (Open3.poutput3): :binmode option implemented. (Open3.poutput2): ditto. (Open3.poutput2e): ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@20567 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- lib/open3.rb | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 62 insertions(+), 6 deletions(-) (limited to 'lib/open3.rb') diff --git a/lib/open3.rb b/lib/open3.rb index 6d57c9f4f1..a094773aad 100644 --- a/lib/open3.rb +++ b/lib/open3.rb @@ -209,6 +209,8 @@ module Open3 # # If opts[:stdin_data] is specified, it is sent to the command's standard input. # + # If opts[:binmode] is true, internal pipes are set to binary mode. + # # Example: # # # dot is a command of graphviz. @@ -224,6 +226,19 @@ module Open3 # p e #=> "bar\nbaz\nfoo\n" # p s #=> # # + # # generate a thumnail image using the convert command of ImageMagick. + # # However, if the image stored really in a file, + # # system("convert", "-thumbnail", "80", filename, "png:-") is better + # # because memory consumption. + # # But if the image is stored in a DB or generated by gnuplot Open3.poutput2 example, + # # Open3.poutput3 is considerable. + # # + # image = File.read("/usr/share/openclipart/png/animals/mammals/sheep-md-v0.1.png", :binmode=>true) + # thumnail, err, s = Open3.poutput3("convert -thumbnail 80 - png:-", :stdin_data=>image, :binmode=>true) + # if s.success? + # STDOUT.binmode; print thumnail + # end + # def poutput3(*cmd, &block) if Hash === cmd.last opts = cmd.pop.dup @@ -232,8 +247,14 @@ module Open3 end stdin_data = opts.delete(:stdin_data) || '' + binmode = opts.delete(:binmode) popen3(*cmd, opts) {|i, o, e, t| + if binmode + i.binmode + o.binmode + e.binmode + end out_reader = Thread.new { o.read } err_reader = Thread.new { e.read } i.write stdin_data @@ -251,10 +272,24 @@ module Open3 # # If opts[:stdin_data] is specified, it is sent to the command's standard input. # - # # factor is a command for integer factorization + # If opts[:binmode] is true, internal pipes are set to binary mode. + # + # # factor is a command for integer factorization. # o, s = Open3.poutput2("factor", :stdin_data=>"42") # p o #=> "42: 2 3 7\n" # + # # generate x**2 graph in png using gnuplot. + # gnuplot_commands = <<"End" + # set terminal png + # plot x**2, "-" with lines + # 1 14 + # 2 1 + # 3 8 + # 4 5 + # e + # End + # image, s = Open3.poutput2("gnuplot", :stdin_data=>gnuplot_commands, :binmode=>true) + # def poutput2(*cmd, &block) if Hash === cmd.last opts = cmd.pop.dup @@ -263,8 +298,13 @@ module Open3 end stdin_data = opts.delete(:stdin_data) || '' + binmode = opts.delete(:binmode) popen2(*cmd, opts) {|i, o, t| + if binmode + i.binmode + o.binmode + end out_reader = Thread.new { o.read } i.write stdin_data i.close @@ -281,6 +321,13 @@ module Open3 # # If opts[:stdin_data] is specified, it is sent to the command's standard input. # + # If opts[:binmode] is true, internal pipes are set to binary mode. + # + # Example: + # + # # capture make log + # make_log, s = Open3.poutput2e("make") + # def poutput2e(*cmd, &block) if Hash === cmd.last opts = cmd.pop.dup @@ -289,8 +336,13 @@ module Open3 end stdin_data = opts.delete(:stdin_data) || '' + binmode = opts.delete(:binmode) popen2e(*cmd, opts) {|i, oe, t| + if binmode + i.binmode + oe.binmode + end outerr_reader = Thread.new { oe.read } i.write stdin_data i.close @@ -364,11 +416,6 @@ module Open3 # # Example: # - # fname = "/usr/share/man/man1/ls.1.gz" - # Open3.pipeline_r(["zcat", fname], "nroff -man", "colcrt") {|r, ts| - # IO.copy_stream(r, STDOUT) - # } - # # Open3.pipeline_r("zcat /var/log/apache2/access.log.*.gz", # [{"LANG"=>"C"}, "grep", "GET /favicon.ico"], # "logresolve") {|r, ts| @@ -476,6 +523,15 @@ module Open3 # # #, # # #] # + # fname = "/usr/share/man/man1/ls.1.gz" + # Open3.pipeline(["zcat", fname], "nroff -man", "colcrt") + # + # # convert PDF to PS and send to a printer by lpr + # pdf_file = "paper.pdf" + # printer = "printer-name" + # Open3.pipeline(["pdftops", pdf_file, "-"], + # ["lpr", "-P#{printer}"]) + # # # count lines # Open3.pipeline("sort", "uniq -c", :in=>"names.txt", :out=>"count") # -- cgit v1.2.3