16 Commits

Author SHA1 Message Date
kojix2
c25d47d721 v0.2.3 2020-09-15 20:00:28 +09:00
kojix2
40c5d9fbdf Add create_main_parser method 2020-09-15 19:57:37 +09:00
kojix2
3e3b1cdfec Fix typo 2020-09-15 19:15:55 +09:00
kojix2
661e5048dd Rubocop auto correct 2020-09-15 18:58:34 +09:00
kojix2
3b8846efbe Add new file params.rb 2020-09-15 18:58:25 +09:00
kojix2
50cb8d7463 Add create_sub_parser method 2020-09-15 18:51:32 +09:00
kojix2
b1df2ed544 Rename create_parser -> create_default_parser 2020-09-15 18:42:38 +09:00
kojix2
05c3b14acd Add color command 2020-08-24 20:17:33 +09:00
kojix2
9e2f169e6c Rename plot_type -> command 2020-08-24 20:17:12 +09:00
kojix2
d6b6ae963d v0.2.2 2020-08-24 14:46:59 +09:00
kojix2
3492f52df9 Accept color numbers 2020-08-24 14:44:43 +09:00
kojix2
21626122eb Fix style 2020-08-19 23:50:47 +09:00
kojix2
38084c6db5 Add some tests... 2020-08-19 23:47:37 +09:00
kojix2
59d200cae0 Improved error messages 2020-08-19 23:47:28 +09:00
kojix2
cd8ee7f9d9 Introduce simplecov 2020-08-19 22:12:53 +09:00
kojix2
9144207c85 Rename read_csv -> parse_as_csv 2020-08-19 22:12:24 +09:00
10 changed files with 259 additions and 62 deletions

View File

@@ -1,56 +1,30 @@
require 'optparse'
require_relative 'preprocessing'
require_relative 'command/params'
module Uplot
Data = Struct.new(:headers, :series)
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
attr_accessor :params, :plot_type
attr_reader :raw_inputs, :data, :fmt
attr_accessor :params, :command
attr_reader :main_parser, :sub_parsers, :raw_inputs, :data, :fmt
def initialize
@params = Params.new
@plot_type = nil
@headers = nil
@delimiter = "\t"
@transpose = false
@output = false
@count = false
@fmt = 'xyy'
@command = nil
@headers = nil
@delimiter = "\t"
@transpose = false
@output = false
@count = false
@fmt = 'xyy'
@raw_inputs = []
@debug = false
end
def create_parser
def create_default_parser
OptionParser.new do |opt|
opt.program_name = 'uplot'
opt.version = Uplot::VERSION
@@ -85,7 +59,7 @@ module Uplot
params.padding = v
end
.on('-c', '--color VAL', String) do |v|
params.color = v.to_sym
params.color = v =~ /\A[0-9]+\z/ ? v.to_i : v.to_sym
end
.on('-x', '--xlabel VAL', String) do |v|
params.xlabel = v
@@ -102,12 +76,12 @@ module Uplot
.on('--debug', TrueClass) do |v|
@debug = v
end
yield opt if block_given?
end
end
def parse_options(argv = ARGV)
main_parser = create_parser
parsers = Hash.new { |h, k| h[k] = create_parser }
def create_sub_parsers
parsers = Hash.new { |h, k| h[k] = create_default_parser }
parsers[:barplot] = \
parsers[:bar]
@@ -193,21 +167,36 @@ module Uplot
params.xlim = get_lim(v)
end
parsers[:colors]
.on('-n', '--names', TrueClass) do |v|
@color_names = v
end
# Preventing the generation of new sub-commands
parsers.default = nil
parsers
end
# Usage and help messages
main_parser.banner = \
<<~MSG
Program: uplot (Tools for plotting on the terminal)
Version: #{Uplot::VERSION} (using unicode_plot #{UnicodePlot::VERSION})
def create_main_parser
create_default_parser do |main_parser|
# 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]
Usage: uplot <command> [options]
Command: #{parsers.keys.join(' ')}
Command: #{sub_parsers.keys.join(' ')}
Options:
MSG
Options:
MSG
end
end
def parse_options(argv = ARGV)
@sub_parsers = create_sub_parsers
@main_parser = create_main_parser
begin
main_parser.order!(argv)
@@ -216,17 +205,17 @@ module Uplot
exit 1
end
@plot_type = argv.shift&.to_sym
@command = argv.shift&.to_sym
unless parsers.has_key?(plot_type)
if plot_type.nil?
unless sub_parsers.has_key?(command)
if command.nil?
warn main_parser.help
else
warn "uplot: unrecognized command '#{plot_type}'"
warn "uplot: unrecognized command '#{command}'"
end
exit 1
end
parser = parsers[plot_type]
parser = sub_parsers[command]
begin
parser.parse!(argv) unless argv.empty?
@@ -242,13 +231,19 @@ module Uplot
def run
parse_options
if command == :colors
Plot.colors
exit
end
# Sometimes the input file does not end with a newline code.
while input = Kernel.gets(nil)
input.freeze
@raw_inputs << input
@data = Preprocessing.input(input, @delimiter, @headers, @transpose)
pp @data if @debug
case plot_type
case command
when :bar, :barplot
Plot.barplot(data, params, @count)
when :count, :c
@@ -266,7 +261,7 @@ module Uplot
when :box, :boxplot
Plot.boxplot(data, params)
else
raise "unrecognized plot_type: #{plot_type}"
raise "unrecognized plot_type: #{command}"
end.render($stderr)
print input if @output

View 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

View File

@@ -123,6 +123,18 @@ module Uplot
UnicodePlot.boxplot(headers, series, **params.to_hc)
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)
series = data.series
if series.size == 1

View File

@@ -5,13 +5,32 @@ module Uplot
module_function
def input(input, delimiter, headers, transpose)
arr = read_csv(input, delimiter)
arr = parse_as_csv(input, delimiter)
headers = get_headers(arr, headers, transpose)
series = get_series(arr, headers, transpose)
Data.new(headers, series)
if headers.nil?
Data.new(headers, series)
else
if headers.include?(nil)
warn "\e[35mHeaders contains nil in it.\e[0m"
elsif headers.include? ''
warn "\e[35mHeaders contains \"\" in it.\e[0m"
end
h_size = headers.size
s_size = series.size
if h_size == s_size
Data.new(headers, series)
elsif h_size > s_size
warn "\e[35mThe number of headers is greater than the number of series.\e[0m"
exit 1
elsif h_size < s_size
warn "\e[35mThe number of headers is less than the number of series.\e[0m"
exit 1
end
end
end
def read_csv(input, delimiter)
def parse_as_csv(input, delimiter)
CSV.parse(input, col_sep: delimiter)
.delete_if do |i|
i == [] or i.all? nil

View File

@@ -1,3 +1,3 @@
module Uplot
VERSION = '0.2.1'.freeze
VERSION = '0.2.3'.freeze
end

View File

@@ -1,4 +1,6 @@
$LOAD_PATH.unshift File.expand_path('../lib', __dir__)
require 'simplecov'
SimpleCov.start
require 'uplot'
require 'test/unit'

View File

@@ -0,0 +1,4 @@
require_relative '../test_helper.rb'
class UplotCommandTest < Test::Unit::TestCase
end

4
test/uplot/plot_test.rb Normal file
View File

@@ -0,0 +1,4 @@
require_relative '../test_helper.rb'
class UplotPlotTest < Test::Unit::TestCase
end

View File

@@ -0,0 +1,130 @@
require_relative '../test_helper.rb'
class UplotPreprocessingTest < Test::Unit::TestCase
def setup
@m = Uplot::Preprocessing
end
test :transpose2 do
n = nil
assert_equal([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]], @m.transpose2([[1, 4, 7],
[2, 5, 8],
[3, 6, 9]]))
assert_equal([[1, 2, 3],
[4, 5, n],
[6, n, n]], @m.transpose2([[1, 4, 6],
[2, 5],
[3]]))
assert_equal([[1, 2, 3],
[n, 4, 5],
[n, n, 6]], @m.transpose2([[1],
[2, 4],
[3, 5, 6]]))
end
test :get_headers do
assert_equal([1, 4, 7], @m.get_headers([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]], true, true))
assert_equal([1, 2, 3], @m.get_headers([[1, 4, 6],
[2, 5],
[3]], true, true))
assert_equal([1, 2, 3], @m.get_headers([[1],
[2, 4],
[3, 5, 6]], true, true))
assert_equal([1, 2, 3], @m.get_headers([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]], true, false))
assert_equal([1, 4, 6], @m.get_headers([[1, 4, 6],
[2, 5],
[3]], true, false))
assert_equal([1], @m.get_headers([[1],
[2, 4],
[3, 5, 6]], true, false))
assert_equal(nil, @m.get_headers([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]], false, true))
assert_equal(nil, @m.get_headers([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]], false, false))
end
test :get_series do
n = nil
assert_equal([[2, 3], [5, 6], [8, 9]], @m.get_series([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]], true, true))
assert_equal([[4, 6], [5], []], @m.get_series([[1, 4, 6],
[2, 5],
[3]], true, true))
assert_equal([[], [4], [5, 6]], @m.get_series([[1],
[2, 4],
[3, 5, 6]], true, true))
assert_equal([[4, 7], [5, 8], [6, 9]], @m.get_series([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]], true, false))
assert_equal([[2, 3], [5, nil]], @m.get_series([[1, 4, 6],
[2, 5],
[3]], true, false))
assert_equal([[2, 3], [4, 5], [nil, 6]], @m.get_series([[1],
[2, 4],
[3, 5, 6]], true, false))
assert_equal([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]], @m.get_series([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]], false, true))
assert_equal([[1, 4, 6],
[2, 5],
[3]], @m.get_series([[1, 4, 6],
[2, 5],
[3]], false, true))
assert_equal([[1],
[2, 4],
[3, 5, 6]], @m.get_series([[1],
[2, 4],
[3, 5, 6]], false, true))
assert_equal([[1, 4, 7],
[2, 5, 8],
[3, 6, 9]], @m.get_series([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]], false, false))
assert_equal([[1, 2, 3],
[4, 5, n],
[6, n, n]], @m.get_series([[1, 4, 6],
[2, 5],
[3]], false, false))
assert_equal([[1, 2, 3],
[n, 4, 5],
[n, n, 6]], @m.get_series([[1],
[2, 4],
[3, 5, 6]], false, false))
end
test :count do
assert_equal([%i[a b c], [3, 2, 1]], @m.count(%i[a a a b b c]))
assert_equal([%i[c b a], [3, 2, 1]], @m.count(%i[a b b c c c]))
end
end

View File

@@ -24,5 +24,6 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'bundler'
spec.add_development_dependency 'rake'
spec.add_development_dependency 'rubocop'
spec.add_development_dependency 'simplecov'
spec.add_development_dependency 'test-unit'
end