YouPlot/lib/uplot/command.rb

116 lines
3.3 KiB
Ruby
Raw Normal View History

require 'optparse'
require 'csv'
2020-07-29 16:01:39 +08:00
module Uplot
class Command
def initialize(argv)
@params = {}
@ptype = nil
2020-07-30 14:08:09 +08:00
@headers = nil
@delimiter = "\t"
@output = false
2020-07-29 16:01:39 +08:00
parse_options(argv)
end
2020-07-30 11:44:31 +08:00
def opt_new
2020-07-30 14:08:09 +08:00
opt = OptionParser.new do |opt|
2020-07-30 11:47:02 +08:00
opt.on('-o', '--output', TrueClass) { |v| @output = v }
2020-07-30 14:08:09 +08:00
opt.on('-d', '--delimiter VAL', String) { |v| @delimiter = v }
opt.on('-H', '--headers', TrueClass) { |v| @headers = v }
2020-07-30 11:44:31 +08:00
opt.on('-t', '--title VAL', String) { |v| @params[:title] = v }
opt.on('-w', '--width VAL', Numeric) { |v| @params[:width] = v }
opt.on('-h', '--height VAL', Numeric) { |v| @params[:height] = v }
opt.on('-b', '--border VAL', Numeric) { |v| @params[:border] = v }
opt.on('-m', '--margin VAL', Numeric) { |v| @params[:margin] = v }
opt.on('-p', '--padding VAL', Numeric) { |v| @params[:padding] = v }
opt.on('-l', '--labels', TrueClass) { |v| @params[:labels] = v }
2020-07-29 18:01:10 +08:00
end
2020-07-30 11:44:31 +08:00
end
2020-07-29 16:01:39 +08:00
2020-07-30 11:44:31 +08:00
def parse_options(argv)
main_parser = opt_new
parsers = {}
parsers['hist'] = opt_new.on('--nbins VAL', Numeric) { |v| @params[:nbins] = v }
parsers['histogram'] = parsers['hist']
parsers['line'] = opt_new
parsers['lineplot'] = parsers['line']
parsers['lines'] = opt_new
main_parser.banner = <<~MSG
2020-07-30 10:48:27 +08:00
Usage:\tuplot <command> [options]
2020-07-30 11:44:31 +08:00
Command:\t#{parsers.keys.join(' ')}
2020-07-30 10:48:27 +08:00
MSG
2020-07-30 11:44:31 +08:00
main_parser.order!(argv)
2020-07-30 10:48:27 +08:00
@ptype = argv.shift
2020-07-29 16:01:39 +08:00
2020-07-30 11:44:31 +08:00
unless parsers.has_key?(@ptype)
warn "unrecognized command '#{@ptype}'"
exit 1
end
parser = parsers[@ptype]
parser.parse!(argv) unless argv.empty?
2020-07-30 10:48:27 +08:00
end
2020-07-29 16:01:39 +08:00
def run
2020-07-30 09:37:20 +08:00
# Sometimes the input file does not end with a newline code.
while input = Kernel.gets(nil)
input.freeze
2020-07-30 14:08:09 +08:00
data, headers = preprocess(input)
2020-07-30 09:37:20 +08:00
case @ptype
when 'hist', 'histogram'
2020-07-30 14:08:09 +08:00
histogram(data, headers)
2020-07-30 09:37:20 +08:00
when 'line', 'lineplot'
2020-07-30 14:08:09 +08:00
line(data, headers)
2020-07-30 09:37:20 +08:00
when 'lines'
2020-07-30 14:08:09 +08:00
lines(data, headers)
2020-07-30 09:37:20 +08:00
end.render($stderr)
2020-07-29 16:01:39 +08:00
2020-07-30 11:47:02 +08:00
print input if @output
2020-07-30 09:37:20 +08:00
end
2020-07-29 16:01:39 +08:00
end
2020-07-30 14:08:09 +08:00
def preprocess(input)
data = CSV.parse(input, col_sep: @delimiter)
if @headers
headers = data.shift
data = data.transpose
[data, headers]
else
data = data.transpose
[data, nil]
end
end
def histogram(data, headers)
@params[:title] ||= headers[0] if headers # labels?
series = data[0].map(&:to_f)
2020-07-30 10:08:36 +08:00
UnicodePlot.histogram(series, **@params.compact)
2020-07-29 16:01:39 +08:00
end
2020-07-29 17:24:08 +08:00
2020-07-30 14:08:09 +08:00
def line(data, headers)
if data.size == 1
2020-07-30 14:08:09 +08:00
@params[:name] ||= headers[0] if headers
y = data[0]
x = (1..y.size).to_a
else
2020-07-30 14:08:09 +08:00
@params[:name] ||= headers[1] if headers
x = data[0]
y = data[1]
2020-07-29 17:24:08 +08:00
end
x = x.map(&:to_f)
y = y.map(&:to_f)
2020-07-30 10:08:36 +08:00
UnicodePlot.lineplot(x, y, **@params.compact)
2020-07-29 17:24:08 +08:00
end
2020-07-30 14:08:09 +08:00
def lines(data, headers)
data.map { |series| series.map(&:to_f) }
2020-07-30 14:08:09 +08:00
@params[:name] ||= headers[1] if headers
plot = UnicodePlot.lineplot(data[0], data[1], **@params.compact)
2.upto(data.size - 1) do |i|
2020-07-30 14:08:09 +08:00
UnicodePlot.lineplot!(plot, data[0], data[i], name: headers[i])
2020-07-29 17:24:08 +08:00
end
plot
end
2020-07-29 16:01:39 +08:00
end
end