YouPlot/lib/youplot/command.rb
2020-11-23 13:23:59 +09:00

80 lines
2.1 KiB
Ruby

# frozen_string_literal: true
require_relative 'preprocessing'
require_relative 'command/parser'
module YouPlot
Data = Struct.new(:headers, :series)
class Command
attr_accessor :params
attr_reader :data, :fmt, :parser
def initialize
@params = Params.new
@parser = Parser.new
end
def run
parser.parse_options
command = parser.command
params = parser.params
delimiter = parser.delimiter
transpose = parser.transpose
headers = parser.headers
pass = parser.pass
output = parser.output
fmt = parser.fmt
@debug = parser.debug
if command == :colors
Plot.colors(parser.color_names)
exit
end
# Sometimes the input file does not end with a newline code.
while (input = Kernel.gets(nil))
input.freeze
@data = Preprocessing.input(input, delimiter, headers, transpose)
pp @data if @debug
plot = case command
when :bar, :barplot
Plot.barplot(data, params)
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
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
end