Refactored comand class

This commit is contained in:
kojix2 2020-12-14 22:02:18 +09:00
parent 0aa6de0de5
commit 566cee883a

View File

@ -35,59 +35,74 @@ module YouPlot
# Sometimes the input file does not end with a newline code. # Sometimes the input file does not end with a newline code.
while (input = Kernel.gets(nil)) while (input = Kernel.gets(nil))
main(input)
# Pass the input to subsequent pipelines
case @options[:pass]
when IO
@options[:pass].print(input)
else
if @options[:pass]
File.open(@options[:pass], 'w') do |f|
f.print(input)
end
end end
end end
@data = if @options[:encoding] private
input2 = input.dup.force_encoding(@options[:encoding]).encode('utf-8')
DSVReader.input(input2, @options[:delimiter], @options[:headers], @options[:transpose]) def main(input)
else output_data(input)
DSVReader.input(input, @options[:delimiter], @options[:headers], @options[:transpose])
@data = read_dsv(input)
pp @data if options[:debug]
plot = create_plot
output_plot(plot)
end end
pp @data if @options[:debug] def read_dsv(input)
input = input.dup.force_encoding(options[:encoding]).encode('utf-8') if options[:encoding]
DSVReader.input(input, options[:delimiter], options[:headers], options[:transpose])
end
plot = case command def create_plot
case command
when :bar, :barplot when :bar, :barplot
@backend.barplot(data, params, @options[:fmt]) @backend.barplot(data, params, options[:fmt])
when :count, :c when :count, :c
@backend.barplot(data, params, count: true) @backend.barplot(data, params, count: true)
when :hist, :histogram when :hist, :histogram
@backend.histogram(data, params) @backend.histogram(data, params)
when :line, :lineplot when :line, :lineplot
@backend.line(data, params, @options[:fmt]) @backend.line(data, params, options[:fmt])
when :lines, :lineplots when :lines, :lineplots
@backend.lines(data, params, @options[:fmt]) @backend.lines(data, params, options[:fmt])
when :scatter, :s when :scatter, :s
@backend.scatter(data, params, @options[:fmt]) @backend.scatter(data, params, options[:fmt])
when :density, :d when :density, :d
@backend.density(data, params, @options[:fmt]) @backend.density(data, params, options[:fmt])
when :box, :boxplot when :box, :boxplot
@backend.boxplot(data, params) @backend.boxplot(data, params)
else else
raise "unrecognized plot_type: #{command}" raise "unrecognized plot_type: #{command}"
end end
end
case @options[:output] def output_data(input)
# Pass the input to subsequent pipelines
case options[:pass]
when IO when IO
plot.render(@options[:output]) options[:pass].print(input)
else else
File.open(@options[:output], 'w') do |f| if options[:pass]
File.open(options[:pass], 'w') do |f|
f.print(input)
end
end
end
end
def output_plot(plot)
case options[:output]
when IO
plot.render(options[:output])
else
File.open(options[:output], 'w') do |f|
plot.render(f) plot.render(f)
end end
end end
end
end end
end end
end end