Files
YouPlot/lib/uplot/command.rb

68 lines
1.7 KiB
Ruby
Raw Normal View History

2020-08-16 00:04:00 +09:00
require_relative 'preprocessing'
2020-09-17 10:06:31 +09:00
require_relative 'command/parser'
2020-07-29 17:01:39 +09:00
module Uplot
2020-08-15 22:12:42 +09:00
Data = Struct.new(:headers, :series)
2020-07-29 17:01:39 +09:00
class Command
2020-09-17 10:06:31 +09:00
attr_accessor :params
attr_reader :raw_inputs, :data, :fmt, :parser
2020-08-15 17:10:41 +09:00
def initialize
2020-08-15 16:46:03 +09:00
@params = Params.new
2020-08-15 19:41:53 +09:00
@raw_inputs = []
2020-08-15 17:48:25 +09:00
2020-09-17 10:06:31 +09:00
@parser = Parser.new
2020-09-15 18:51:32 +09:00
end
2020-07-29 17:01:39 +09:00
def run
2020-09-17 10:06:31 +09:00
parser.parse_options
2020-09-17 10:38:59 +09:00
command = parser.command
params = parser.params
2020-09-17 10:28:01 +09:00
delimiter = parser.delimiter
transpose = parser.transpose
2020-09-17 10:38:59 +09:00
headers = parser.headers
output = parser.output
count = parser.count
fmt = parser.fmt
debug = parser.debug
2020-09-15 18:58:34 +09:00
2020-08-24 20:17:33 +09:00
if command == :colors
Plot.colors
exit
end
2020-07-30 10:37:20 +09:00
# Sometimes the input file does not end with a newline code.
while input = Kernel.gets(nil)
input.freeze
2020-08-15 19:41:53 +09:00
@raw_inputs << input
2020-09-17 10:28:01 +09:00
@data = Preprocessing.input(input, delimiter, headers, transpose)
2020-08-16 13:20:52 +09:00
pp @data if @debug
2020-08-24 20:17:12 +09:00
case command
2020-08-15 17:10:41 +09:00
when :bar, :barplot
2020-08-16 12:46:45 +09:00
Plot.barplot(data, params, @count)
2020-08-15 17:10:41 +09:00
when :count, :c
2020-08-16 12:46:45 +09:00
Plot.barplot(data, params, count = true)
2020-08-15 17:10:41 +09:00
when :hist, :histogram
2020-08-16 12:46:45 +09:00
Plot.histogram(data, params)
2020-08-15 17:10:41 +09:00
when :line, :lineplot
2020-08-16 12:46:45 +09:00
Plot.line(data, params)
2020-08-15 17:10:41 +09:00
when :lines, :lineplots
2020-08-16 12:46:45 +09:00
Plot.lines(data, params, fmt)
2020-08-16 16:08:14 +09:00
when :scatter, :s
2020-08-16 12:46:45 +09:00
Plot.scatter(data, params, fmt)
2020-08-16 16:08:14 +09:00
when :density, :d
2020-08-16 12:46:45 +09:00
Plot.density(data, params, fmt)
2020-08-15 17:10:41 +09:00
when :box, :boxplot
2020-08-16 12:46:45 +09:00
Plot.boxplot(data, params)
2020-08-15 17:10:41 +09:00
else
2020-08-24 20:17:12 +09:00
raise "unrecognized plot_type: #{command}"
2020-07-30 10:37:20 +09:00
end.render($stderr)
2020-07-29 17:01:39 +09:00
2020-09-17 10:38:59 +09:00
print input if output
2020-07-30 10:37:20 +09:00
end
2020-07-29 17:01:39 +09:00
end
end
end