mirror of
https://github.com/red-data-tools/YouPlot.git
synced 2025-09-19 02:18:08 +08:00
Compare commits
9 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
c25d47d721 | ||
![]() |
40c5d9fbdf | ||
![]() |
3e3b1cdfec | ||
![]() |
661e5048dd | ||
![]() |
3b8846efbe | ||
![]() |
50cb8d7463 | ||
![]() |
b1df2ed544 | ||
![]() |
05c3b14acd | ||
![]() |
9e2f169e6c |
@@ -1,44 +1,18 @@
|
|||||||
require 'optparse'
|
require 'optparse'
|
||||||
require_relative 'preprocessing'
|
require_relative 'preprocessing'
|
||||||
|
require_relative 'command/params'
|
||||||
|
|
||||||
module Uplot
|
module Uplot
|
||||||
Data = Struct.new(:headers, :series)
|
Data = Struct.new(:headers, :series)
|
||||||
|
|
||||||
class Command
|
class Command
|
||||||
Params = Struct.new(
|
attr_accessor :params, :command
|
||||||
# Sort me!
|
attr_reader :main_parser, :sub_parsers, :raw_inputs, :data, :fmt
|
||||||
:title,
|
|
||||||
:width,
|
|
||||||
:height,
|
|
||||||
:border,
|
|
||||||
:margin,
|
|
||||||
:padding,
|
|
||||||
:color,
|
|
||||||
:xlabel,
|
|
||||||
:ylabel,
|
|
||||||
:labels,
|
|
||||||
:symbol,
|
|
||||||
:xscale,
|
|
||||||
:nbins,
|
|
||||||
:closed,
|
|
||||||
:canvas,
|
|
||||||
:xlim,
|
|
||||||
:ylim,
|
|
||||||
:grid,
|
|
||||||
:name
|
|
||||||
) do
|
|
||||||
def to_hc
|
|
||||||
to_h.compact
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
attr_accessor :params, :plot_type
|
|
||||||
attr_reader :raw_inputs, :data, :fmt
|
|
||||||
|
|
||||||
def initialize
|
def initialize
|
||||||
@params = Params.new
|
@params = Params.new
|
||||||
|
|
||||||
@plot_type = nil
|
@command = nil
|
||||||
@headers = nil
|
@headers = nil
|
||||||
@delimiter = "\t"
|
@delimiter = "\t"
|
||||||
@transpose = false
|
@transpose = false
|
||||||
@@ -50,7 +24,7 @@ module Uplot
|
|||||||
@debug = false
|
@debug = false
|
||||||
end
|
end
|
||||||
|
|
||||||
def create_parser
|
def create_default_parser
|
||||||
OptionParser.new do |opt|
|
OptionParser.new do |opt|
|
||||||
opt.program_name = 'uplot'
|
opt.program_name = 'uplot'
|
||||||
opt.version = Uplot::VERSION
|
opt.version = Uplot::VERSION
|
||||||
@@ -102,12 +76,12 @@ module Uplot
|
|||||||
.on('--debug', TrueClass) do |v|
|
.on('--debug', TrueClass) do |v|
|
||||||
@debug = v
|
@debug = v
|
||||||
end
|
end
|
||||||
|
yield opt if block_given?
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def parse_options(argv = ARGV)
|
def create_sub_parsers
|
||||||
main_parser = create_parser
|
parsers = Hash.new { |h, k| h[k] = create_default_parser }
|
||||||
parsers = Hash.new { |h, k| h[k] = create_parser }
|
|
||||||
|
|
||||||
parsers[:barplot] = \
|
parsers[:barplot] = \
|
||||||
parsers[:bar]
|
parsers[:bar]
|
||||||
@@ -193,9 +167,18 @@ module Uplot
|
|||||||
params.xlim = get_lim(v)
|
params.xlim = get_lim(v)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
parsers[:colors]
|
||||||
|
.on('-n', '--names', TrueClass) do |v|
|
||||||
|
@color_names = v
|
||||||
|
end
|
||||||
|
|
||||||
# Preventing the generation of new sub-commands
|
# Preventing the generation of new sub-commands
|
||||||
parsers.default = nil
|
parsers.default = nil
|
||||||
|
parsers
|
||||||
|
end
|
||||||
|
|
||||||
|
def create_main_parser
|
||||||
|
create_default_parser do |main_parser|
|
||||||
# Usage and help messages
|
# Usage and help messages
|
||||||
main_parser.banner = \
|
main_parser.banner = \
|
||||||
<<~MSG
|
<<~MSG
|
||||||
@@ -204,10 +187,16 @@ module Uplot
|
|||||||
|
|
||||||
Usage: uplot <command> [options]
|
Usage: uplot <command> [options]
|
||||||
|
|
||||||
Command: #{parsers.keys.join(' ')}
|
Command: #{sub_parsers.keys.join(' ')}
|
||||||
|
|
||||||
Options:
|
Options:
|
||||||
MSG
|
MSG
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def parse_options(argv = ARGV)
|
||||||
|
@sub_parsers = create_sub_parsers
|
||||||
|
@main_parser = create_main_parser
|
||||||
|
|
||||||
begin
|
begin
|
||||||
main_parser.order!(argv)
|
main_parser.order!(argv)
|
||||||
@@ -216,17 +205,17 @@ module Uplot
|
|||||||
exit 1
|
exit 1
|
||||||
end
|
end
|
||||||
|
|
||||||
@plot_type = argv.shift&.to_sym
|
@command = argv.shift&.to_sym
|
||||||
|
|
||||||
unless parsers.has_key?(plot_type)
|
unless sub_parsers.has_key?(command)
|
||||||
if plot_type.nil?
|
if command.nil?
|
||||||
warn main_parser.help
|
warn main_parser.help
|
||||||
else
|
else
|
||||||
warn "uplot: unrecognized command '#{plot_type}'"
|
warn "uplot: unrecognized command '#{command}'"
|
||||||
end
|
end
|
||||||
exit 1
|
exit 1
|
||||||
end
|
end
|
||||||
parser = parsers[plot_type]
|
parser = sub_parsers[command]
|
||||||
|
|
||||||
begin
|
begin
|
||||||
parser.parse!(argv) unless argv.empty?
|
parser.parse!(argv) unless argv.empty?
|
||||||
@@ -242,13 +231,19 @@ module Uplot
|
|||||||
|
|
||||||
def run
|
def run
|
||||||
parse_options
|
parse_options
|
||||||
|
|
||||||
|
if command == :colors
|
||||||
|
Plot.colors
|
||||||
|
exit
|
||||||
|
end
|
||||||
|
|
||||||
# Sometimes the input file does not end with a newline code.
|
# Sometimes the input file does not end with a newline code.
|
||||||
while input = Kernel.gets(nil)
|
while input = Kernel.gets(nil)
|
||||||
input.freeze
|
input.freeze
|
||||||
@raw_inputs << input
|
@raw_inputs << input
|
||||||
@data = Preprocessing.input(input, @delimiter, @headers, @transpose)
|
@data = Preprocessing.input(input, @delimiter, @headers, @transpose)
|
||||||
pp @data if @debug
|
pp @data if @debug
|
||||||
case plot_type
|
case command
|
||||||
when :bar, :barplot
|
when :bar, :barplot
|
||||||
Plot.barplot(data, params, @count)
|
Plot.barplot(data, params, @count)
|
||||||
when :count, :c
|
when :count, :c
|
||||||
@@ -266,7 +261,7 @@ module Uplot
|
|||||||
when :box, :boxplot
|
when :box, :boxplot
|
||||||
Plot.boxplot(data, params)
|
Plot.boxplot(data, params)
|
||||||
else
|
else
|
||||||
raise "unrecognized plot_type: #{plot_type}"
|
raise "unrecognized plot_type: #{command}"
|
||||||
end.render($stderr)
|
end.render($stderr)
|
||||||
|
|
||||||
print input if @output
|
print input if @output
|
||||||
|
30
lib/uplot/command/params.rb
Normal file
30
lib/uplot/command/params.rb
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
module Uplot
|
||||||
|
class Command
|
||||||
|
Params = Struct.new(
|
||||||
|
# Sort me!
|
||||||
|
:title,
|
||||||
|
:width,
|
||||||
|
:height,
|
||||||
|
:border,
|
||||||
|
:margin,
|
||||||
|
:padding,
|
||||||
|
:color,
|
||||||
|
:xlabel,
|
||||||
|
:ylabel,
|
||||||
|
:labels,
|
||||||
|
:symbol,
|
||||||
|
:xscale,
|
||||||
|
:nbins,
|
||||||
|
:closed,
|
||||||
|
:canvas,
|
||||||
|
:xlim,
|
||||||
|
:ylim,
|
||||||
|
:grid,
|
||||||
|
:name
|
||||||
|
) do
|
||||||
|
def to_hc
|
||||||
|
to_h.compact
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
@@ -123,6 +123,18 @@ module Uplot
|
|||||||
UnicodePlot.boxplot(headers, series, **params.to_hc)
|
UnicodePlot.boxplot(headers, series, **params.to_hc)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def colors
|
||||||
|
UnicodePlot::StyledPrinter::TEXT_COLORS.each do |k, v|
|
||||||
|
print v
|
||||||
|
print k
|
||||||
|
print "\t"
|
||||||
|
print ' ●'
|
||||||
|
print "\033[0m"
|
||||||
|
print "\t"
|
||||||
|
end
|
||||||
|
puts
|
||||||
|
end
|
||||||
|
|
||||||
def check_series_size(data, fmt)
|
def check_series_size(data, fmt)
|
||||||
series = data.series
|
series = data.series
|
||||||
if series.size == 1
|
if series.size == 1
|
||||||
|
@@ -1,3 +1,3 @@
|
|||||||
module Uplot
|
module Uplot
|
||||||
VERSION = '0.2.2'.freeze
|
VERSION = '0.2.3'.freeze
|
||||||
end
|
end
|
||||||
|
Reference in New Issue
Block a user