Class: YouPlot::Command

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

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
22
23
# File 'lib/youplot/command.rb', line 16

def initialize(argv = ARGV)
  @argv    = argv
  @parser  = Parser.new
  @command = nil
  @params  = nil
  @options = nil
  @backend = YouPlot::Backends::UnicodePlot
end

Instance Attribute Details

#commandObject

Returns the value of attribute command.



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

def command
  @command
end

#dataObject (readonly)

Returns the value of attribute data.



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

def data
  @data
end

#optionsObject

Returns the value of attribute options.



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

def options
  @options
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



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
# File 'lib/youplot/command.rb', line 30

def run
  parser.parse_options(@argv)
  @command ||= parser.command
  @options ||= parser.options
  @params  ||= parser.params

  # color command
  if %i[colors color colours colour].include? @command
    plot = create_plot
    output_plot(plot)
    return
  end

  # progressive mode
  if options[:progressive]
    stop = false
    Signal.trap(:INT) { stop = true }

    # make cursor invisible
    options[:output].print "\e[?25l"

    # mainloop
    while (input = Kernel.gets)
      n = main_progressive(input)
      break if stop

      options[:output].print "\e[#{n}F"
    end

    options[:output].print "\e[0J"
    # make cursor visible
    options[:output].print "\e[?25h"

  # normal mode
  else
    # Sometimes the input file does not end with a newline code.
    begin
      begin
        input = Kernel.gets(nil)
      rescue Errno::ENOENT => e
        warn e.message
        next
      end
      main(input)
    end until input
  end
end

#run_as_executableObject



25
26
27
28
# File 'lib/youplot/command.rb', line 25

def run_as_executable
  YouPlot.run_as_executable = true
  run
end