Files
YouPlot/lib/youplot/command.rb

99 lines
2.6 KiB
Ruby
Raw Normal View History

2020-09-19 00:08:09 +09:00
# frozen_string_literal: true
require_relative 'dsv_reader'
2020-09-17 10:06:31 +09:00
require_relative 'command/parser'
# FIXME
require_relative 'backends/unicode_plot_backend'
2020-11-23 13:09:16 +09:00
module YouPlot
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
2020-11-10 23:09:41 +09:00
attr_reader :data, :fmt, :parser
2020-08-15 17:10:41 +09:00
def initialize(argv = ARGV)
@argv = argv
@params = Params.new
@parser = Parser.new
@backend = YouPlot::Backends::UnicodePlotBackend
2020-09-15 18:51:32 +09:00
end
2020-07-29 17:01:39 +09:00
def run
parser.parse_options(@argv)
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
2020-09-29 18:13:03 +09:00
pass = parser.pass
2020-09-17 10:38:59 +09:00
output = parser.output
fmt = parser.fmt
2020-11-23 23:52:14 +09:00
@encoding = parser.encoding
2020-10-10 23:58:05 +09:00
@debug = parser.debug
2020-09-15 18:58:34 +09:00
2020-08-24 20:17:33 +09:00
if command == :colors
@backend.colors(parser.color_names)
2020-08-24 20:17:33 +09:00
exit
end
2020-07-30 10:37:20 +09:00
# Sometimes the input file does not end with a newline code.
2020-10-10 23:16:15 +09:00
while (input = Kernel.gets(nil))
# Pass the input to subsequent pipelines
case pass
when IO
pass.print(input)
else
if pass
File.open(pass, 'w') do |f|
f.print(input)
end
end
end
2020-11-23 23:52:14 +09:00
@data = if @encoding
input2 = input.dup.force_encoding(@encoding).encode('utf-8')
DSVReader.input(input2, delimiter, headers, transpose)
2020-11-23 23:52:14 +09:00
else
DSVReader.input(input, delimiter, headers, transpose)
2020-11-23 23:52:14 +09:00
end
2020-08-16 13:20:52 +09:00
pp @data if @debug
2020-09-29 18:13:03 +09:00
plot = case command
when :bar, :barplot
@backend.barplot(data, params, fmt)
2020-09-29 18:13:03 +09:00
when :count, :c
@backend.barplot(data, params, count: true)
2020-09-29 18:13:03 +09:00
when :hist, :histogram
@backend.histogram(data, params)
2020-09-29 18:13:03 +09:00
when :line, :lineplot
@backend.line(data, params, fmt)
2020-09-29 18:13:03 +09:00
when :lines, :lineplots
@backend.lines(data, params, fmt)
2020-09-29 18:13:03 +09:00
when :scatter, :s
@backend.scatter(data, params, fmt)
2020-09-29 18:13:03 +09:00
when :density, :d
@backend.density(data, params, fmt)
2020-09-29 18:13:03 +09:00
when :box, :boxplot
@backend.boxplot(data, params)
2020-09-29 18:13:03 +09:00
else
raise "unrecognized plot_type: #{command}"
end
case output
when IO
2020-09-29 18:13:03 +09:00
plot.render(output)
2020-08-15 17:10:41 +09:00
else
2020-09-29 18:13:03 +09:00
File.open(output, 'w') do |f|
plot.render(f)
end
end
2020-07-29 17:01:39 +09:00
2020-07-30 10:37:20 +09:00
end
2020-07-29 17:01:39 +09:00
end
end
end