Class: ConsoleCmdr

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

Instance Method Summary collapse

Constructor Details

#initialize(pxindex: nil) ⇒ ConsoleCmdr

Returns a new instance of ConsoleCmdr.



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

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

Instance Method Details

#clearObject



26
27
28
29
30
31
# File 'lib/console_cmdr.rb', line 26

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

#cli_bannerObject



33
34
35
36
# File 'lib/console_cmdr.rb', line 33

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

#reloadObject



38
39
40
41
# File 'lib/console_cmdr.rb', line 38

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

#start(&blk) ⇒ Object



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
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
# File 'lib/console_cmdr.rb', line 43

def start(&blk)
  
  cli_banner()

  seq = []
  
  while @running do
    
    c = $stdin.getch
    #puts 'c:'  + c.inspect
    #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        
      puts 
      @linebuffer = ''
      display_output()
    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|

          case command
          when 'time'
            Time.now.to_s
          end
        end
      end
    end
    
  end #/ end of loop

end

#stopObject Also known as: quit



140
141
142
143
144
# File 'lib/console_cmdr.rb', line 140

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