mirror of
				https://github.com/red-data-tools/YouPlot.git
				synced 2025-11-04 03:28:10 +08:00 
			
		
		
		
	Switch to more object-oriented structure
This commit is contained in:
		
							
								
								
									
										45
									
								
								lib/uplot.rb
									
									
									
									
									
								
							
							
						
						
									
										45
									
								
								lib/uplot.rb
									
									
									
									
									
								
							@@ -1 +1,46 @@
 | 
			
		||||
require 'uplot/version'
 | 
			
		||||
require 'unicode_plot'
 | 
			
		||||
require 'optparse'
 | 
			
		||||
 | 
			
		||||
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
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user