From e69d1caed7dc0d01474dae427880c5e4c2a7f55f Mon Sep 17 00:00:00 2001 From: kojix2 <2xijok@gmail.com> Date: Wed, 29 Jul 2020 17:01:39 +0900 Subject: [PATCH] Add command.rb --- lib/uplot.rb | 43 ++----------------------------------------- lib/uplot/command.rb | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 41 deletions(-) create mode 100644 lib/uplot/command.rb diff --git a/lib/uplot.rb b/lib/uplot.rb index 59137a5..52b612b 100644 --- a/lib/uplot.rb +++ b/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 diff --git a/lib/uplot/command.rb b/lib/uplot/command.rb new file mode 100644 index 0000000..ade404e --- /dev/null +++ b/lib/uplot/command.rb @@ -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