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