Class: ConsoleCmdr

Inherits:
Cmdr
  • Object
show all
Defined in:
lib/console_cmdr.rb

Instance Method Summary collapse

Constructor Details

#initialize(pxindex: nil, debug: false) ⇒ ConsoleCmdr

Returns a new instance of ConsoleCmdr.



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/console_cmdr.rb', line 14

def initialize(pxindex: nil, debug: false)
  
  super()
  @pxindex_filepath = pxindex
  @pxi = pxindex ? PxIndex.new(pxindex) : nil
  
  @keys = []
  @item_selected = ''
  @input_selection = []
  @running = true
  @debug = debug
  
end

Instance Method Details

#clearObject



28
29
30
31
32
33
# File 'lib/console_cmdr.rb', line 28

def clear()
  @linebuffer = ''
  print "\e[H\e[2J"
  ''
  #cli_banner()
end

#cli_bannerObject



35
36
37
38
# File 'lib/console_cmdr.rb', line 35

def cli_banner()
  puts "welcome, this code is powered by the cmdr gem\n\n"
  print "> "
end

#reloadObject



40
41
42
43
# File 'lib/console_cmdr.rb', line 40

def reload()
  @pxi = @pxindex_filepath ? PxIndex.new(@pxindex_filepath) : nil
  'reloaded'
end

#start(&blk) ⇒ Object



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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/console_cmdr.rb', line 45

def start(&blk)
  
  @running = true
  cli_banner()

  seq = []
  
  while @running do
    
    c = $stdin.getch
    #puts 'c:'  + c.inspect
    #puts
    #puts c.ord
    
    # [27, 91, 65] = up_arrow
    # [27, 91, 66] = down_arrow
    # [27, 91, 67] = right_arrow
    # [27, 91, 68] = left_arrow
    # [27, 91, 49, 53] = ctrl+right_arrow
    # 13 = enter
    #puts 'seq: ' + seq.inspect
    
    if c.ord == 27 then
      seq << 27
      #print "\b\b"
    elsif c.ord == 91 and seq.first == 27
      seq << 91
    elsif c.ord == 65 and seq[1] == 91
      seq << 65
      c = :arrow_up
      on_keypress(c)
      input c
    elsif c.ord == 66 and seq[1] == 91
      seq << 66
      c = :arrow_down
      seq = []
      on_keypress(c)
      input c        
      
    elsif c.ord == 68 and seq[1] == 91
      seq << 68
      c = :arrow_left
      seq = []
      on_keypress(c)
      input c         
    elsif c.ord == 49 and seq[1] == 91
      seq << 49
      #puts 'ctrl + right arrow'
    elsif c.ord == 59 and seq[2] == 49
      seq << 59
    elsif c.ord == 53 and seq[3] == 59
      seq << 53        
    elsif c.ord == 67 and seq[4] == 53
      seq << 67
      seq = []
      c = :ctrl_arrow_right
      on_keypress(c)
      input c        
    elsif c.ord == 67 and seq[1] == 91
      seq << 67
      c = :arrow_right
      #puts 'right arrow'
      seq = []
      on_keypress(c)
      input c         
    elsif c.ord == 79 and seq.first == 27
      seq << 79

    elsif c.ord == 72 and seq[1] == 79
      c = :home_key
      seq = []
      on_keypress(c)
    elsif c == "\u0003"  # CTRL+C        
      c = :ctrl_c
      on_keypress(c)
      puts 
      @linebuffer = ''
      display_output()
    elsif c == "\u0012" # CTR+R
      #repeat command
      c = :ctrl_r
      new_c = on_keypress(c)
      input(new_c, &blk)
    else
      if block_given? then
        char = on_keypress(c)

        unless (@linebuffer[0] == ':' and char == ':')
          input(char, &blk) if char
        end
        
      else
        input(c) do |command|
          
          puts 'command: ' + command.inspect if @debug
          
          case command
          when 'time'
            Time.now.to_s
          else
            on_enter(command)
          end
        end
      end
    end
    
  end #/ end of loop

end

#stopObject Also known as: quit



155
156
157
158
159
# File 'lib/console_cmdr.rb', line 155

def stop()
  
  @running = false    
  'bye'
end