From ccfbaa7bdef70ff2afc0666070b40448bb81e354 Mon Sep 17 00:00:00 2001 From: kojix2 <2xijok@gmail.com> Date: Tue, 29 Sep 2020 18:13:03 +0900 Subject: [PATCH] Allows you to specify the output file --- lib/uplot/command.rb | 57 +++++++++++++++++++++++-------------- lib/uplot/command/parser.rb | 10 +++++-- 2 files changed, 43 insertions(+), 24 deletions(-) diff --git a/lib/uplot/command.rb b/lib/uplot/command.rb index bb9b7a6..6e268bf 100644 --- a/lib/uplot/command.rb +++ b/lib/uplot/command.rb @@ -25,6 +25,7 @@ module Uplot delimiter = parser.delimiter transpose = parser.transpose headers = parser.headers + pass = parser.pass output = parser.output count = parser.count fmt = parser.fmt @@ -41,28 +42,42 @@ module Uplot @raw_inputs << input @data = Preprocessing.input(input, delimiter, headers, transpose) pp @data if @debug - case command - when :bar, :barplot - Plot.barplot(data, params, @count) - when :count, :c - Plot.barplot(data, params, count = true) - when :hist, :histogram - Plot.histogram(data, params) - when :line, :lineplot - Plot.line(data, params) - when :lines, :lineplots - Plot.lines(data, params, fmt) - when :scatter, :s - Plot.scatter(data, params, fmt) - when :density, :d - Plot.density(data, params, fmt) - when :box, :boxplot - Plot.boxplot(data, params) - else - raise "unrecognized plot_type: #{command}" - end.render($stderr) + plot = case command + when :bar, :barplot + Plot.barplot(data, params, @count) + when :count, :c + Plot.barplot(data, params, count = true) + when :hist, :histogram + Plot.histogram(data, params) + when :line, :lineplot + Plot.line(data, params) + when :lines, :lineplots + Plot.lines(data, params, fmt) + when :scatter, :s + Plot.scatter(data, params, fmt) + when :density, :d + Plot.density(data, params, fmt) + when :box, :boxplot + Plot.boxplot(data, params) + else + raise "unrecognized plot_type: #{command}" + end - print input if output + if output.is_a?(IO) + plot.render(output) + else + File.open(output, 'w') do |f| + plot.render(f) + end + end + + if pass.is_a?(IO) + print input + elsif pass + File.open(pass, 'w') do |f| + f.print(input) + end + end end end end diff --git a/lib/uplot/command/parser.rb b/lib/uplot/command/parser.rb index f79633c..30a376d 100644 --- a/lib/uplot/command/parser.rb +++ b/lib/uplot/command/parser.rb @@ -7,7 +7,7 @@ module Uplot class Command class Parser attr_reader :command, :params, - :delimiter, :transpose, :headers, :output, :count, :fmt, :debug + :delimiter, :transpose, :headers, :pass, :output, :count, :fmt, :debug def initialize @command = nil @@ -16,7 +16,8 @@ module Uplot @delimiter = "\t" @transpose = false @headers = nil - @output = false + @pass = false + @output = $stderr @count = false @fmt = 'xyy' @debug = false @@ -26,7 +27,10 @@ module Uplot OptionParser.new do |opt| opt.program_name = 'uplot' opt.version = Uplot::VERSION - opt.on('-O', 'outputs the standard input data to the standard output', TrueClass) do |v| + opt.on('-O', '--pass [VAL]', 'file to output standard input data to [stdout]') do |v| + @pass = v || $stdout + end + opt.on('-o', '--output VAL', 'file to output results to [stderr]') do |v| @output = v end opt.on('-d', '--delimiter VAL', 'use DELIM instead of TAB for field delimiter', String) do |v|