summaryrefslogtreecommitdiff
path: root/lib/open3.rb
diff options
context:
space:
mode:
authorakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-12-07 02:49:18 +0000
committerakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-12-07 02:49:18 +0000
commit1de2a15aaf80a0ce7c56caa4fe8d063a95631a74 (patch)
treec74a2baea027c1e80335e9fd630589783158f548 /lib/open3.rb
parent0b314ffd8764f1d493c4b7246a5437557b323a12 (diff)
* 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
Diffstat (limited to 'lib/open3.rb')
-rw-r--r--lib/open3.rb68
1 files changed, 62 insertions, 6 deletions
diff --git a/lib/open3.rb b/lib/open3.rb
index 6d57c9f4f19..a094773aad6 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 #=> #<Process::Status: pid 32682 exit 0>
#
+ # # 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
# # #<Process::Status: pid 11820 exit 0>,
# # #<Process::Status: pid 11828 exit 0>]
#
+ # 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")
#