mirror of
https://github.com/red-data-tools/YouPlot.git
synced 2025-05-06 06:46:11 +08:00
95 lines
2.6 KiB
Ruby
95 lines
2.6 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
require_relative 'preprocessing'
|
|
require_relative 'command/parser'
|
|
|
|
# FIXME
|
|
require_relative 'backends/unicode_plot_backend'
|
|
|
|
module YouPlot
|
|
Data = Struct.new(:headers, :series)
|
|
|
|
class Command
|
|
attr_accessor :params
|
|
attr_reader :data, :fmt, :parser
|
|
|
|
def initialize(argv = ARGV)
|
|
@argv = argv
|
|
@params = Params.new
|
|
@parser = Parser.new
|
|
@backend = YouPlot::Backends::UnicodePlotBackend
|
|
end
|
|
|
|
def run
|
|
parser.parse_options(@argv)
|
|
command = parser.command
|
|
params = parser.params
|
|
delimiter = parser.delimiter
|
|
transpose = parser.transpose
|
|
headers = parser.headers
|
|
pass = parser.pass
|
|
output = parser.output
|
|
fmt = parser.fmt
|
|
@encoding = parser.encoding
|
|
@debug = parser.debug
|
|
|
|
if command == :colors
|
|
@backend.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 = if @encoding
|
|
input2 = input.dup.force_encoding(@encoding).encode('utf-8')
|
|
Preprocessing.input(input2, delimiter, headers, transpose)
|
|
else
|
|
Preprocessing.input(input, delimiter, headers, transpose)
|
|
end
|
|
pp @data if @debug
|
|
plot = case command
|
|
when :bar, :barplot
|
|
@backend.barplot(data, params)
|
|
when :count, :c
|
|
@backend.barplot(data, params, count: true)
|
|
when :hist, :histogram
|
|
@backend.histogram(data, params)
|
|
when :line, :lineplot
|
|
@backend.line(data, params)
|
|
when :lines, :lineplots
|
|
@backend.lines(data, params, fmt)
|
|
when :scatter, :s
|
|
@backend.scatter(data, params, fmt)
|
|
when :density, :d
|
|
@backend.density(data, params, fmt)
|
|
when :box, :boxplot
|
|
@backend.boxplot(data, params)
|
|
else
|
|
raise "unrecognized plot_type: #{command}"
|
|
end
|
|
|
|
case output
|
|
when IO
|
|
plot.render(output)
|
|
else
|
|
File.open(output, 'w') do |f|
|
|
plot.render(f)
|
|
end
|
|
end
|
|
|
|
case pass
|
|
when IO
|
|
pass.print(input)
|
|
else
|
|
if pass
|
|
File.open(pass, 'w') do |f|
|
|
f.print(input)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|