2020-09-18 23:08:09 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-09-17 15:48:03 +08:00
|
|
|
require 'optparse'
|
|
|
|
require_relative 'params'
|
|
|
|
|
2020-09-17 09:06:31 +08:00
|
|
|
module Uplot
|
|
|
|
class Command
|
|
|
|
class Parser
|
2020-09-23 20:32:11 +08:00
|
|
|
attr_reader :command, :params,
|
2020-10-10 22:07:57 +08:00
|
|
|
:delimiter, :transpose, :headers, :pass, :output, :fmt, :debug
|
2020-09-17 09:06:31 +08:00
|
|
|
|
|
|
|
def initialize
|
2020-10-12 15:14:51 +08:00
|
|
|
@command = nil
|
|
|
|
@params = Params.new
|
2020-09-17 09:28:01 +08:00
|
|
|
|
2020-10-12 15:14:51 +08:00
|
|
|
@delimiter = "\t"
|
|
|
|
@transpose = false
|
|
|
|
@headers = nil
|
|
|
|
@pass = false
|
|
|
|
@output = $stderr
|
|
|
|
@fmt = 'xyy'
|
|
|
|
@debug = false
|
2020-09-17 09:45:44 +08:00
|
|
|
end
|
2020-09-17 09:06:31 +08:00
|
|
|
|
|
|
|
def create_default_parser
|
|
|
|
OptionParser.new do |opt|
|
2020-10-12 15:14:51 +08:00
|
|
|
opt.program_name = 'uplot'
|
|
|
|
opt.version = Uplot::VERSION
|
|
|
|
opt.summary_width = 24
|
|
|
|
opt.on_tail('') # Add a blank line at the end
|
2020-10-12 15:45:28 +08:00
|
|
|
opt.separator('')
|
|
|
|
opt.on('Options:')
|
2020-10-11 07:29:02 +08:00
|
|
|
opt.on('-O', '--pass [VAL]', 'file to output standard input data to [stdout]',
|
2020-10-11 08:23:57 +08:00
|
|
|
'for inserting uplot in the middle of Unix pipes') do |v|
|
2020-09-29 17:13:03 +08:00
|
|
|
@pass = v || $stdout
|
|
|
|
end
|
|
|
|
opt.on('-o', '--output VAL', 'file to output results to [stderr]') do |v|
|
2020-09-17 09:06:31 +08:00
|
|
|
@output = v
|
|
|
|
end
|
2020-10-10 23:11:54 +08:00
|
|
|
opt.on('-d', '--delimiter VAL', String, 'use DELIM instead of TAB for field delimiter') do |v|
|
2020-09-17 09:06:31 +08:00
|
|
|
@delimiter = v
|
|
|
|
end
|
2020-10-10 23:11:54 +08:00
|
|
|
opt.on('-H', '--headers', TrueClass, 'specify that the input has header row') do |v|
|
2020-09-17 09:06:31 +08:00
|
|
|
@headers = v
|
|
|
|
end
|
2020-11-06 08:56:35 +08:00
|
|
|
opt.on('-T', '--transpose', TrueClass, 'transpose the axes of the input data') do |v|
|
2020-09-17 09:06:31 +08:00
|
|
|
@transpose = v
|
|
|
|
end
|
2020-10-10 23:11:54 +08:00
|
|
|
opt.on('-t', '--title VAL', String, 'print string on the top of plot') do |v|
|
2020-09-17 09:06:31 +08:00
|
|
|
params.title = v
|
|
|
|
end
|
2020-10-10 23:11:54 +08:00
|
|
|
opt.on('-x', '--xlabel VAL', String, 'print string on the bottom of the plot') do |v|
|
2020-09-17 09:06:31 +08:00
|
|
|
params.xlabel = v
|
|
|
|
end
|
2020-10-10 23:11:54 +08:00
|
|
|
opt.on('-y', '--ylabel VAL', String, 'print string on the far left of the plot') do |v|
|
2020-09-17 09:06:31 +08:00
|
|
|
params.ylabel = v
|
|
|
|
end
|
2020-10-10 23:11:54 +08:00
|
|
|
opt.on('-w', '--width VAL', Integer, 'number of characters per row') do |v|
|
2020-09-17 09:06:31 +08:00
|
|
|
params.width = v
|
|
|
|
end
|
2020-10-10 23:11:54 +08:00
|
|
|
opt.on('-h', '--height VAL', Numeric, 'number of rows') do |v|
|
2020-09-17 09:06:31 +08:00
|
|
|
params.height = v
|
|
|
|
end
|
2020-10-10 23:11:54 +08:00
|
|
|
opt.on('-b', '--border VAL', String, 'specify the style of the bounding box') do |v|
|
2020-09-17 09:06:31 +08:00
|
|
|
params.border = v.to_sym
|
|
|
|
end
|
2020-10-10 23:11:54 +08:00
|
|
|
opt.on('-m', '--margin VAL', Numeric, 'number of spaces to the left of the plot') do |v|
|
2020-09-17 09:06:31 +08:00
|
|
|
params.margin = v
|
|
|
|
end
|
2020-10-10 23:11:54 +08:00
|
|
|
opt.on('-p', '--padding VAL', Numeric, 'space of the left and right of the plot') do |v|
|
2020-09-17 09:06:31 +08:00
|
|
|
params.padding = v
|
|
|
|
end
|
2020-10-10 23:11:54 +08:00
|
|
|
opt.on('-c', '--color VAL', String, 'color of the drawing') do |v|
|
2020-09-17 09:45:57 +08:00
|
|
|
params.color = v =~ /\A[0-9]+\z/ ? v.to_i : v.to_sym
|
2020-09-17 09:06:31 +08:00
|
|
|
end
|
2020-10-10 23:11:54 +08:00
|
|
|
opt.on('--[no-]labels', TrueClass, 'hide the labels') do |v|
|
2020-09-17 09:06:31 +08:00
|
|
|
params.labels = v
|
|
|
|
end
|
2020-10-10 23:11:54 +08:00
|
|
|
opt.on('--fmt VAL', String, 'xyxy : header is like x1, y1, x2, y2, x3, y3...', 'xyy : header is like x, y1, y2, y2, y3...') do |v|
|
2020-09-17 09:06:31 +08:00
|
|
|
@fmt = v
|
|
|
|
end
|
|
|
|
opt.on('--debug', TrueClass) do |v|
|
|
|
|
@debug = v
|
|
|
|
end
|
|
|
|
yield opt if block_given?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-09-23 20:32:11 +08:00
|
|
|
def main_parser
|
|
|
|
@main_parser ||= create_default_parser do |main_parser|
|
2020-09-17 09:06:31 +08:00
|
|
|
# Usage and help messages
|
|
|
|
main_parser.banner = \
|
|
|
|
<<~MSG
|
2020-10-12 13:52:56 +08:00
|
|
|
|
2020-09-17 09:06:31 +08:00
|
|
|
Program: uplot (Tools for plotting on the terminal)
|
2020-10-11 08:23:57 +08:00
|
|
|
Version: #{Uplot::VERSION} (using UnicodePlot #{UnicodePlot::VERSION})
|
|
|
|
Source: https://github.com/kojix2/uplot
|
2020-09-17 09:06:31 +08:00
|
|
|
|
2020-11-05 09:41:38 +08:00
|
|
|
Usage: uplot <command> [options] <in.tsv>
|
2020-09-17 09:06:31 +08:00
|
|
|
|
2020-10-12 15:14:51 +08:00
|
|
|
Commands:
|
|
|
|
barplot bar
|
|
|
|
histogram hist
|
|
|
|
lineplot line
|
|
|
|
lineplots lines
|
|
|
|
scatter s
|
|
|
|
density d
|
|
|
|
boxplot box
|
2020-11-06 08:56:35 +08:00
|
|
|
colors show the list of available colors
|
|
|
|
|
|
|
|
count c baplot based on the number of occurrences
|
|
|
|
(slower than `sort | uniq -c | sort -n -k1`)
|
2020-09-17 09:06:31 +08:00
|
|
|
MSG
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-09-23 20:32:11 +08:00
|
|
|
def sub_parser
|
|
|
|
@sub_parser ||= create_default_parser do |parser|
|
|
|
|
parser.banner = <<~MSG
|
2020-10-12 15:14:51 +08:00
|
|
|
|
2020-11-05 09:41:38 +08:00
|
|
|
Usage: uplot #{command} [options] <in.tsv>
|
2020-09-23 20:32:11 +08:00
|
|
|
|
2020-10-12 15:45:28 +08:00
|
|
|
Options for #{command}:
|
2020-09-23 20:32:11 +08:00
|
|
|
MSG
|
|
|
|
|
|
|
|
case command
|
|
|
|
when nil
|
|
|
|
warn main_parser.help
|
|
|
|
exit 1
|
|
|
|
|
|
|
|
when :barplot, :bar
|
2020-10-12 15:45:28 +08:00
|
|
|
parser.on_head('--symbol VAL', String, 'character to be used to plot the bars') do |v|
|
2020-09-23 20:32:11 +08:00
|
|
|
params.symbol = v
|
|
|
|
end
|
2020-10-12 15:45:28 +08:00
|
|
|
parser.on_head('--xscale VAL', String, 'axis scaling') do |v|
|
2020-09-23 20:32:11 +08:00
|
|
|
params.xscale = v
|
|
|
|
end
|
|
|
|
|
|
|
|
when :count, :c
|
2020-10-12 15:45:28 +08:00
|
|
|
parser.on_head('--symbol VAL', String, 'character to be used to plot the bars') do |v|
|
2020-09-23 20:32:11 +08:00
|
|
|
params.symbol = v
|
|
|
|
end
|
|
|
|
|
|
|
|
when :histogram, :hist
|
2020-10-12 15:45:28 +08:00
|
|
|
parser.on_head('-n', '--nbins VAL', Numeric, 'approximate number of bins') do |v|
|
2020-09-23 20:32:11 +08:00
|
|
|
params.nbins = v
|
|
|
|
end
|
2020-10-12 15:45:28 +08:00
|
|
|
parser.on_head('--closed VAL', String) do |v|
|
2020-09-23 20:32:11 +08:00
|
|
|
params.closed = v
|
|
|
|
end
|
2020-10-12 15:45:28 +08:00
|
|
|
parser.on_head('--symbol VAL', String, 'character to be used to plot the bars') do |v|
|
2020-09-23 20:32:11 +08:00
|
|
|
params.symbol = v
|
|
|
|
end
|
|
|
|
|
|
|
|
when :lineplot, :line
|
2020-10-12 15:45:28 +08:00
|
|
|
parser.on_head('--canvas VAL', String, 'type of canvas') do |v|
|
2020-09-23 20:32:11 +08:00
|
|
|
params.canvas = v
|
|
|
|
end
|
2020-10-12 15:45:28 +08:00
|
|
|
parser.on_head('--xlim VAL', Array, 'plotting range for the x coordinate') do |v|
|
2020-09-23 20:32:11 +08:00
|
|
|
params.xlim = v.take(2)
|
|
|
|
end
|
2020-10-12 15:45:28 +08:00
|
|
|
parser.on_head('--ylim VAL', Array, 'plotting range for the y coordinate') do |v|
|
2020-09-23 20:32:11 +08:00
|
|
|
params.ylim = v.take(2)
|
|
|
|
end
|
|
|
|
|
|
|
|
when :lineplots, :lines
|
2020-10-12 15:45:28 +08:00
|
|
|
parser.on_head('--canvas VAL', String) do |v|
|
2020-09-23 20:32:11 +08:00
|
|
|
params.canvas = v
|
|
|
|
end
|
2020-10-12 15:45:28 +08:00
|
|
|
parser.on_head('--xlim VAL', Array, 'plotting range for the x coordinate') do |v|
|
2020-09-23 20:32:11 +08:00
|
|
|
params.xlim = v.take(2)
|
|
|
|
end
|
2020-10-12 15:45:28 +08:00
|
|
|
parser.on_head('--ylim VAL', Array, 'plotting range for the y coordinate') do |v|
|
2020-09-23 20:32:11 +08:00
|
|
|
params.ylim = v.take(2)
|
|
|
|
end
|
|
|
|
|
|
|
|
when :scatter, :s
|
2020-10-12 15:45:28 +08:00
|
|
|
parser.on_head('--canvas VAL', String) do |v|
|
2020-09-23 20:32:11 +08:00
|
|
|
params.canvas = v
|
|
|
|
end
|
2020-10-12 15:45:28 +08:00
|
|
|
parser.on_head('--xlim VAL', Array, 'plotting range for the x coordinate') do |v|
|
2020-09-23 20:32:11 +08:00
|
|
|
params.xlim = v.take(2)
|
|
|
|
end
|
2020-10-12 15:45:28 +08:00
|
|
|
parser.on_head('--ylim VAL', Array, 'plotting range for the y coordinate') do |v|
|
2020-09-23 20:32:11 +08:00
|
|
|
params.ylim = v.take(2)
|
|
|
|
end
|
|
|
|
|
|
|
|
when :density, :d
|
2020-10-12 15:45:28 +08:00
|
|
|
parser.on_head('--grid', TrueClass) do |v|
|
2020-09-23 20:32:11 +08:00
|
|
|
params.grid = v
|
|
|
|
end
|
2020-10-12 15:45:28 +08:00
|
|
|
parser.on_head('--xlim VAL', Array, 'plotting range for the x coordinate') do |v|
|
2020-09-23 20:32:11 +08:00
|
|
|
params.xlim = v.take(2)
|
|
|
|
end
|
2020-10-12 15:45:28 +08:00
|
|
|
parser.on_head('--ylim VAL', Array, 'plotting range for the y coordinate') do |v|
|
2020-09-23 20:32:11 +08:00
|
|
|
params.ylim = v.take(2)
|
|
|
|
end
|
|
|
|
|
|
|
|
when :boxplot, :box
|
2020-10-12 15:45:28 +08:00
|
|
|
parser.on_head('--xlim VAL', Array, 'plotting range for the x coordinate') do |v|
|
2020-09-23 20:32:11 +08:00
|
|
|
params.xlim = v.take(2)
|
|
|
|
end
|
|
|
|
|
|
|
|
when :colors
|
2020-10-12 15:45:28 +08:00
|
|
|
parser.on_head('-n', '--names', TrueClass) do |v|
|
2020-09-23 20:32:11 +08:00
|
|
|
@color_names = v
|
|
|
|
end
|
|
|
|
|
|
|
|
else
|
|
|
|
warn "uplot: unrecognized command '#{command}'"
|
|
|
|
exit 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-09-17 09:06:31 +08:00
|
|
|
def parse_options(argv = ARGV)
|
|
|
|
begin
|
|
|
|
main_parser.order!(argv)
|
|
|
|
rescue OptionParser::ParseError => e
|
|
|
|
warn "uplot: #{e.message}"
|
|
|
|
exit 1
|
|
|
|
end
|
|
|
|
|
|
|
|
@command = argv.shift&.to_sym
|
|
|
|
|
|
|
|
begin
|
2020-09-23 20:32:11 +08:00
|
|
|
sub_parser.parse!(argv)
|
2020-09-17 09:06:31 +08:00
|
|
|
rescue OptionParser::ParseError => e
|
|
|
|
warn "uplot: #{e.message}"
|
|
|
|
exit 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-10-10 22:07:57 +08:00
|
|
|
end
|