Class: YouPlot::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/youplot/command.rb,
lib/youplot/command/params.rb,
lib/youplot/command/parser.rb

Defined Under Namespace

Classes: Params, Parser

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv = ARGV) ⇒ Command

Returns a new instance of Command.



16
17
18
19
20
21
# File 'lib/youplot/command.rb', line 16

def initialize(argv = ARGV)
  @argv    = argv
  @params  = Params.new
  @parser  = Parser.new
  @backend = YouPlot::Backends::UnicodePlotBackend
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



14
15
16
# File 'lib/youplot/command.rb', line 14

def data
  @data
end

#fmtObject (readonly)

Returns the value of attribute fmt.



14
15
16
# File 'lib/youplot/command.rb', line 14

def fmt
  @fmt
end

#paramsObject

Returns the value of attribute params.



13
14
15
# File 'lib/youplot/command.rb', line 13

def params
  @params
end

#parserObject (readonly)

Returns the value of attribute parser.



14
15
16
# File 'lib/youplot/command.rb', line 14

def parser
  @parser
end

Instance Method Details

#runObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/youplot/command.rb', line 23

def run
  parser.parse_options(@argv)
  command   = parser.command
  params    = parser.params
  delimiter = parser.delimiter
  transpose = parser.transpose
  headers   = parser.headers
  pass      = parser.pass
  output    = parser.output
  fmt       = parser.fmt
  @encoding = parser.encoding
  @debug    = parser.debug

  if command == :colors
    @backend.colors(parser.color_names)
    exit
  end

  # Sometimes the input file does not end with a newline code.
  while (input = Kernel.gets(nil))

    # Pass the input to subsequent pipelines
    case pass
    when IO
      pass.print(input)
    else
      if pass
        File.open(pass, 'w') do |f|
          f.print(input)
        end
      end
    end

    @data = if @encoding
              input2 = input.dup.force_encoding(@encoding).encode('utf-8')
              DSVReader.input(input2, delimiter, headers, transpose)
            else
              DSVReader.input(input, delimiter, headers, transpose)
            end

    pp @data if @debug

    plot = case command
           when :bar, :barplot
             @backend.barplot(data, params, fmt)
           when :count, :c
             @backend.barplot(data, params, count: true)
           when :hist, :histogram
             @backend.histogram(data, params)
           when :line, :lineplot
             @backend.line(data, params, fmt)
           when :lines, :lineplots
             @backend.lines(data, params, fmt)
           when :scatter, :s
             @backend.scatter(data, params, fmt)
           when :density, :d
             @backend.density(data, params, fmt)
           when :box, :boxplot
             @backend.boxplot(data, params)
           else
             raise "unrecognized plot_type: #{command}"
           end

    case output
    when IO
      plot.render(output)
    else
      File.open(output, 'w') do |f|
        plot.render(f)
      end
    end

  end
end