2020-07-30 12:42:51 +08:00
|
|
|
require 'optparse'
|
2020-08-15 23:04:00 +08:00
|
|
|
require_relative 'preprocessing'
|
2020-07-30 12:42:51 +08:00
|
|
|
|
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-08-24 19:17:12 +08:00
|
|
|
attr_accessor :params, :command
|
2020-08-16 11:46:45 +08:00
|
|
|
attr_reader :raw_inputs, :data, :fmt
|
2020-08-15 16:10:41 +08:00
|
|
|
|
2020-08-15 22:06:55 +08:00
|
|
|
def initialize
|
2020-08-15 15:46:03 +08:00
|
|
|
@params = Params.new
|
|
|
|
|
2020-08-24 19:17:12 +08:00
|
|
|
@command = nil
|
|
|
|
@headers = nil
|
|
|
|
@delimiter = "\t"
|
|
|
|
@transpose = false
|
|
|
|
@output = false
|
|
|
|
@count = false
|
|
|
|
@fmt = 'xyy'
|
2020-08-15 18:41:53 +08:00
|
|
|
|
|
|
|
@raw_inputs = []
|
|
|
|
@debug = false
|
2020-07-29 16:01:39 +08:00
|
|
|
end
|
|
|
|
|
2020-09-15 17:42:38 +08:00
|
|
|
def create_default_parser
|
2020-08-14 12:47:04 +08:00
|
|
|
OptionParser.new do |opt|
|
|
|
|
opt.program_name = 'uplot'
|
|
|
|
opt.version = Uplot::VERSION
|
2020-08-15 16:48:25 +08:00
|
|
|
opt.on('-o', '--output', TrueClass) do |v|
|
|
|
|
@output = v
|
|
|
|
end
|
|
|
|
.on('-d', '--delimiter VAL', String) do |v|
|
|
|
|
@delimiter = v
|
|
|
|
end
|
|
|
|
.on('-H', '--headers', TrueClass) do |v|
|
|
|
|
@headers = v
|
|
|
|
end
|
|
|
|
.on('-T', '--transpose', TrueClass) do |v|
|
|
|
|
@transpose = v
|
|
|
|
end
|
|
|
|
.on('-t', '--title VAL', String) do |v|
|
|
|
|
params.title = v
|
|
|
|
end
|
|
|
|
.on('-w', '--width VAL', Numeric) do |v|
|
|
|
|
params.width = v
|
|
|
|
end
|
|
|
|
.on('-h', '--height VAL', Numeric) do |v|
|
|
|
|
params.height = v
|
|
|
|
end
|
|
|
|
.on('-b', '--border VAL', Numeric) do |v|
|
|
|
|
params.border = v
|
|
|
|
end
|
|
|
|
.on('-m', '--margin VAL', Numeric) do |v|
|
|
|
|
params.margin = v
|
|
|
|
end
|
|
|
|
.on('-p', '--padding VAL', Numeric) do |v|
|
|
|
|
params.padding = v
|
|
|
|
end
|
|
|
|
.on('-c', '--color VAL', String) do |v|
|
2020-08-24 13:43:32 +08:00
|
|
|
params.color = v =~ /\A[0-9]+\z/ ? v.to_i : v.to_sym
|
2020-08-15 16:48:25 +08:00
|
|
|
end
|
|
|
|
.on('-x', '--xlabel VAL', String) do |v|
|
|
|
|
params.xlabel = v
|
|
|
|
end
|
|
|
|
.on('-y', '--ylabel VAL', String) do |v|
|
|
|
|
params.ylabel = v
|
|
|
|
end
|
|
|
|
.on('-l', '--labels', TrueClass) do |v|
|
|
|
|
params.labels = v
|
|
|
|
end
|
|
|
|
.on('--fmt VAL', String) do |v|
|
|
|
|
@fmt = v
|
|
|
|
end
|
|
|
|
.on('--debug', TrueClass) do |v|
|
|
|
|
@debug = v
|
|
|
|
end
|
2020-08-14 12:47:04 +08:00
|
|
|
end
|
2020-07-30 11:44:31 +08:00
|
|
|
end
|
2020-07-29 16:01:39 +08:00
|
|
|
|
2020-09-15 17:51:32 +08:00
|
|
|
def create_sub_parser
|
2020-09-15 17:42:38 +08:00
|
|
|
parsers = Hash.new { |h, k| h[k] = create_default_parser }
|
2020-08-15 16:48:25 +08:00
|
|
|
|
|
|
|
parsers[:barplot] = \
|
|
|
|
parsers[:bar]
|
|
|
|
.on('--symbol VAL', String) do |v|
|
|
|
|
params.symbol = v
|
|
|
|
end
|
|
|
|
.on('--xscale VAL', String) do |v|
|
|
|
|
params.xscale = v
|
|
|
|
end
|
|
|
|
.on('--count', TrueClass) do |v|
|
|
|
|
@count = v
|
|
|
|
end
|
|
|
|
|
|
|
|
parsers[:count] = \
|
|
|
|
parsers[:c] # barplot -c
|
|
|
|
.on('--symbol VAL', String) do |v|
|
|
|
|
params.symbol = v
|
|
|
|
end
|
|
|
|
|
|
|
|
parsers[:histogram] = \
|
|
|
|
parsers[:hist]
|
|
|
|
.on('-n', '--nbins VAL', Numeric) do |v|
|
|
|
|
params.nbins = v
|
|
|
|
end
|
|
|
|
.on('--closed VAL', String) do |v|
|
|
|
|
params.closed = v
|
|
|
|
end
|
|
|
|
.on('--symbol VAL', String) do |v|
|
|
|
|
params.symbol = v
|
|
|
|
end
|
|
|
|
|
|
|
|
parsers[:lineplot] = \
|
|
|
|
parsers[:line]
|
|
|
|
.on('--canvas VAL', String) do |v|
|
|
|
|
params.canvas = v
|
|
|
|
end
|
|
|
|
.on('--xlim VAL', String) do |v|
|
|
|
|
params.xlim = get_lim(v)
|
|
|
|
end
|
|
|
|
.on('--ylim VAL', String) do |v|
|
|
|
|
params.ylim = get_lim(v)
|
|
|
|
end
|
|
|
|
|
|
|
|
parsers[:lineplots] = \
|
|
|
|
parsers[:lines]
|
|
|
|
.on('--canvas VAL', String) do |v|
|
|
|
|
params.canvas = v
|
|
|
|
end
|
|
|
|
.on('--xlim VAL', String) do |v|
|
|
|
|
params.xlim = get_lim(v)
|
|
|
|
end
|
|
|
|
.on('--ylim VAL', String) do |v|
|
|
|
|
params.ylim = get_lim(v)
|
|
|
|
end
|
|
|
|
|
|
|
|
parsers[:scatter] = \
|
|
|
|
parsers[:s]
|
|
|
|
.on('--canvas VAL', String) do |v|
|
|
|
|
params.canvas = v
|
|
|
|
end
|
|
|
|
.on('--xlim VAL', String) do |v|
|
|
|
|
params.xlim = get_lim(v)
|
|
|
|
end
|
|
|
|
.on('--ylim VAL', String) do |v|
|
|
|
|
params.ylim = get_lim(v)
|
|
|
|
end
|
|
|
|
|
|
|
|
parsers[:density] = \
|
|
|
|
parsers[:d]
|
|
|
|
.on('--grid', TrueClass) do |v|
|
|
|
|
params.grid = v
|
|
|
|
end
|
|
|
|
.on('--xlim VAL', String) do |v|
|
|
|
|
params.xlim = get_lim(v)
|
|
|
|
end
|
|
|
|
.on('--ylim VAL', String) do |v|
|
|
|
|
params.ylim = get_lim(v)
|
|
|
|
end
|
|
|
|
|
|
|
|
parsers[:boxplot] = \
|
|
|
|
parsers[:box]
|
|
|
|
.on('--xlim VAL', String) do |v|
|
|
|
|
params.xlim = get_lim(v)
|
|
|
|
end
|
|
|
|
|
2020-08-24 19:17:33 +08:00
|
|
|
parsers[:colors]
|
|
|
|
.on('-n', '--names', TrueClass) do |v|
|
|
|
|
@color_names = v
|
|
|
|
end
|
|
|
|
|
2020-08-15 16:48:25 +08:00
|
|
|
# Preventing the generation of new sub-commands
|
|
|
|
parsers.default = nil
|
2020-09-15 17:51:32 +08:00
|
|
|
parsers
|
|
|
|
end
|
|
|
|
|
|
|
|
def parse_options(argv = ARGV)
|
|
|
|
main_parser = create_default_parser
|
|
|
|
sub_parsers = create_sub_parser
|
2020-08-15 16:48:25 +08:00
|
|
|
|
|
|
|
# Usage and help messages
|
|
|
|
main_parser.banner = \
|
|
|
|
<<~MSG
|
|
|
|
Program: uplot (Tools for plotting on the terminal)
|
|
|
|
Version: #{Uplot::VERSION} (using unicode_plot #{UnicodePlot::VERSION})
|
|
|
|
|
|
|
|
Usage: uplot <command> [options]
|
|
|
|
|
2020-09-15 17:51:32 +08:00
|
|
|
Command: #{sub_parsers.keys.join(' ')}
|
2020-08-15 16:48:25 +08:00
|
|
|
|
|
|
|
Options:
|
|
|
|
MSG
|
2020-08-14 10:45:47 +08:00
|
|
|
|
|
|
|
begin
|
|
|
|
main_parser.order!(argv)
|
|
|
|
rescue OptionParser::ParseError => e
|
|
|
|
warn "uplot: #{e.message}"
|
|
|
|
exit 1
|
|
|
|
end
|
|
|
|
|
2020-08-24 19:17:12 +08:00
|
|
|
@command = argv.shift&.to_sym
|
2020-07-29 16:01:39 +08:00
|
|
|
|
2020-09-15 17:51:32 +08:00
|
|
|
unless sub_parsers.has_key?(command)
|
2020-08-24 19:17:12 +08:00
|
|
|
if command.nil?
|
2020-08-14 10:45:47 +08:00
|
|
|
warn main_parser.help
|
|
|
|
else
|
2020-08-24 19:17:12 +08:00
|
|
|
warn "uplot: unrecognized command '#{command}'"
|
2020-08-14 10:45:47 +08:00
|
|
|
end
|
2020-07-30 11:44:31 +08:00
|
|
|
exit 1
|
|
|
|
end
|
2020-09-15 17:51:32 +08:00
|
|
|
parser = sub_parsers[command]
|
2020-08-14 10:45:47 +08:00
|
|
|
|
|
|
|
begin
|
|
|
|
parser.parse!(argv) unless argv.empty?
|
|
|
|
rescue OptionParser::ParseError => e
|
|
|
|
warn "uplot: #{e.message}"
|
|
|
|
exit 1
|
|
|
|
end
|
2020-07-30 10:48:27 +08:00
|
|
|
end
|
|
|
|
|
2020-08-15 21:17:44 +08:00
|
|
|
def get_lim(str)
|
|
|
|
str.split(/-|:|\.\./)[0..1].map(&:to_f)
|
|
|
|
end
|
|
|
|
|
2020-07-29 16:01:39 +08:00
|
|
|
def run
|
2020-08-15 22:06:55 +08:00
|
|
|
parse_options
|
2020-08-24 19:17:33 +08:00
|
|
|
|
|
|
|
if command == :colors
|
|
|
|
Plot.colors
|
|
|
|
exit
|
|
|
|
end
|
|
|
|
|
2020-07-30 09:37:20 +08:00
|
|
|
# Sometimes the input file does not end with a newline code.
|
|
|
|
while input = Kernel.gets(nil)
|
2020-07-30 12:42:51 +08:00
|
|
|
input.freeze
|
2020-08-15 18:41:53 +08:00
|
|
|
@raw_inputs << input
|
2020-08-15 22:18:33 +08:00
|
|
|
@data = Preprocessing.input(input, @delimiter, @headers, @transpose)
|
2020-08-16 12:20:52 +08:00
|
|
|
pp @data if @debug
|
2020-08-24 19:17:12 +08:00
|
|
|
case command
|
2020-08-15 16:10:41 +08:00
|
|
|
when :bar, :barplot
|
2020-08-16 11:46:45 +08:00
|
|
|
Plot.barplot(data, params, @count)
|
2020-08-15 16:10:41 +08:00
|
|
|
when :count, :c
|
2020-08-16 11:46:45 +08:00
|
|
|
Plot.barplot(data, params, count = true)
|
2020-08-15 16:10:41 +08:00
|
|
|
when :hist, :histogram
|
2020-08-16 11:46:45 +08:00
|
|
|
Plot.histogram(data, params)
|
2020-08-15 16:10:41 +08:00
|
|
|
when :line, :lineplot
|
2020-08-16 11:46:45 +08:00
|
|
|
Plot.line(data, params)
|
2020-08-15 16:10:41 +08:00
|
|
|
when :lines, :lineplots
|
2020-08-16 11:46:45 +08:00
|
|
|
Plot.lines(data, params, fmt)
|
2020-08-16 15:08:14 +08:00
|
|
|
when :scatter, :s
|
2020-08-16 11:46:45 +08:00
|
|
|
Plot.scatter(data, params, fmt)
|
2020-08-16 15:08:14 +08:00
|
|
|
when :density, :d
|
2020-08-16 11:46:45 +08:00
|
|
|
Plot.density(data, params, fmt)
|
2020-08-15 16:10:41 +08:00
|
|
|
when :box, :boxplot
|
2020-08-16 11:46:45 +08:00
|
|
|
Plot.boxplot(data, params)
|
2020-08-15 16:10:41 +08:00
|
|
|
else
|
2020-08-24 19:17:12 +08:00
|
|
|
raise "unrecognized plot_type: #{command}"
|
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
|
|
|
|
end
|
|
|
|
end
|