mirror of
https://github.com/red-data-tools/YouPlot.git
synced 2025-06-24 10:01:12 +08:00
Add command.rb
This commit is contained in:
parent
2217396d04
commit
e69d1caed7
43
lib/uplot.rb
43
lib/uplot.rb
@ -1,46 +1,7 @@
|
||||
require 'uplot/version'
|
||||
require 'unicode_plot'
|
||||
require 'optparse'
|
||||
require 'uplot/version'
|
||||
require 'uplot/command.rb'
|
||||
|
||||
module Uplot
|
||||
class Command
|
||||
def initialize(argv)
|
||||
@params = {}
|
||||
@ptype = nil
|
||||
parse_options(argv)
|
||||
end
|
||||
|
||||
def parse_options(argv)
|
||||
parser = OptionParser.new
|
||||
parser.order!(argv)
|
||||
@ptype = argv.shift
|
||||
|
||||
subparsers = Hash.new do |_h, k|
|
||||
warn "no such subcommand: #{k}"
|
||||
exit 1
|
||||
end
|
||||
|
||||
subparsers['hist'] = OptionParser.new.tap do |sub|
|
||||
sub.on('--nbins VAL') { |v| @params[:nbins] = v.to_i }
|
||||
sub.on('-p') { |v| @params[:p] = v }
|
||||
end
|
||||
|
||||
subparsers[@ptype].parse!(argv) unless argv.empty?
|
||||
end
|
||||
|
||||
def run
|
||||
input_lines = readlines.map(&:chomp)
|
||||
case @ptype
|
||||
when 'hist', 'histogram'
|
||||
histogram(input_lines).render
|
||||
end
|
||||
|
||||
puts input_lines if @params[:p]
|
||||
end
|
||||
|
||||
def histogram(input_lines)
|
||||
series = input_lines.map(&:to_f)
|
||||
UnicodePlot.histogram(series, nbins: @params[:nbins])
|
||||
end
|
||||
end
|
||||
end
|
||||
|
42
lib/uplot/command.rb
Normal file
42
lib/uplot/command.rb
Normal file
@ -0,0 +1,42 @@
|
||||
module Uplot
|
||||
class Command
|
||||
def initialize(argv)
|
||||
@params = {}
|
||||
@ptype = nil
|
||||
parse_options(argv)
|
||||
end
|
||||
|
||||
def parse_options(argv)
|
||||
parser = OptionParser.new
|
||||
parser.order!(argv)
|
||||
@ptype = argv.shift
|
||||
|
||||
subparsers = Hash.new do |_h, k|
|
||||
warn "no such subcommand: #{k}"
|
||||
exit 1
|
||||
end
|
||||
|
||||
subparsers['hist'] = OptionParser.new.tap do |sub|
|
||||
sub.on('--nbins VAL') { |v| @params[:nbins] = v.to_i }
|
||||
sub.on('-p') { |v| @params[:p] = v }
|
||||
end
|
||||
|
||||
subparsers[@ptype].parse!(argv) unless argv.empty?
|
||||
end
|
||||
|
||||
def run
|
||||
input_lines = readlines.map(&:chomp)
|
||||
case @ptype
|
||||
when 'hist', 'histogram'
|
||||
histogram(input_lines).render
|
||||
end
|
||||
|
||||
puts input_lines if @params[:p]
|
||||
end
|
||||
|
||||
def histogram(input_lines)
|
||||
series = input_lines.map(&:to_f)
|
||||
UnicodePlot.histogram(series, nbins: @params[:nbins])
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue
Block a user