YouPlot/lib/uplot/command.rb

80 lines
2.1 KiB
Ruby
Raw Normal View History

2020-09-18 23:08:09 +08:00
# frozen_string_literal: true
2020-08-15 23:04:00 +08:00
require_relative 'preprocessing'
2020-09-17 09:06:31 +08:00
require_relative 'command/parser'
2020-07-29 16:01:39 +08:00
module Uplot
2020-08-15 21:12:42 +08:00
Data = Struct.new(:headers, :series)
2020-07-29 16:01:39 +08:00
class Command
2020-09-17 09:06:31 +08:00
attr_accessor :params
2020-11-10 22:09:41 +08:00
attr_reader :data, :fmt, :parser
2020-08-15 16:10:41 +08:00
def initialize
2020-08-15 15:46:03 +08:00
@params = Params.new
2020-09-17 09:06:31 +08:00
@parser = Parser.new
2020-09-15 17:51:32 +08:00
end
2020-07-29 16:01:39 +08:00
def run
2020-09-17 09:06:31 +08:00
parser.parse_options
2020-09-17 09:38:59 +08:00
command = parser.command
params = parser.params
2020-09-17 09:28:01 +08:00
delimiter = parser.delimiter
transpose = parser.transpose
2020-09-17 09:38:59 +08:00
headers = parser.headers
2020-09-29 17:13:03 +08:00
pass = parser.pass
2020-09-17 09:38:59 +08:00
output = parser.output
fmt = parser.fmt
2020-10-10 22:58:05 +08:00
@debug = parser.debug
2020-09-15 17:58:34 +08:00
2020-08-24 19:17:33 +08:00
if command == :colors
2020-11-06 09:21:36 +08:00
Plot.colors(parser.color_names)
2020-08-24 19:17:33 +08:00
exit
end
2020-07-30 09:37:20 +08:00
# Sometimes the input file does not end with a newline code.
2020-10-10 22:16:15 +08:00
while (input = Kernel.gets(nil))
input.freeze
2020-09-17 09:28:01 +08:00
@data = Preprocessing.input(input, delimiter, headers, transpose)
2020-08-16 12:20:52 +08:00
pp @data if @debug
2020-09-29 17:13:03 +08:00
plot = case command
when :bar, :barplot
2020-10-10 22:07:57 +08:00
Plot.barplot(data, params)
2020-09-29 17:13:03 +08:00
when :count, :c
2020-10-10 22:21:55 +08:00
Plot.barplot(data, params, count: true)
2020-09-29 17:13:03 +08:00
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)
2020-08-15 16:10:41 +08:00
else
2020-09-29 17:13:03 +08:00
File.open(output, 'w') do |f|
plot.render(f)
end
end
2020-07-29 16:01:39 +08:00
2020-09-29 17:13:03 +08:00
if pass.is_a?(IO)
print input
elsif pass
File.open(pass, 'w') do |f|
f.print(input)
end
end
2020-07-30 09:37:20 +08:00
end
2020-07-29 16:01:39 +08:00
end
end
end