mirror of
				https://github.com/red-data-tools/YouPlot.git
				synced 2025-11-04 03:28:10 +08:00 
			
		
		
		
	Allows you to specify the output file
This commit is contained in:
		@@ -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
 | 
			
		||||
 
 | 
			
		||||
@@ -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|
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user