Class: Cmdr

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

Instance Method Summary collapse

Constructor Details

#initializeCmdr

Returns a new instance of Cmdr.



9
10
11
12
13
14
15
# File 'lib/cmdr.rb', line 9

def initialize()

  @linebuffer = ''
  @history = []
  @history_index = -1

end

Instance Method Details

#cli_bannerObject

display the cli banner upon initialisation



89
90
91
# File 'lib/cmdr.rb', line 89

def cli_banner()
  print "> "
end

#historyObject



93
94
95
# File 'lib/cmdr.rb', line 93

def history()
  cli_update @history.map.with_index {|x,i| " %s %s" % [i, x]}.join("\n")    
end

#input(c, enter: "\r", backspace: "\u007F", arrowup: :arrow_up, arrowdown: :arrow_down) ⇒ Object



17
18
19
20
21
22
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
# File 'lib/cmdr.rb', line 17

def input(c, enter: "\r", backspace: "\u007F", arrowup: :arrow_up, arrowdown: :arrow_down)
  
  key = c
  #return 'var r="e";'
  reveal(c)
  
  case key
  when enter
     
    command = @linebuffer 
    
    @history << command unless @history.last == command or command.empty?
    
    if @linebuffer.length < 1 then
      return clear_cli()
    end      
    
    
    result = yield(@linebuffer.sub(/^\:/,''))

    if result then
      display_output("\n" + result)
    else
      display_output "\n" + 'command not found >> '  + @linebuffer.inspect
    end
   
    result = false

    @linebuffer = ''
    clear_cli()
    @history_index = -1

  when backspace
    @linebuffer.chop!
    cli_update()
  when arrowup

    return if @history.empty? 

    
    clear_cli()
    command = @history[@history_index]
    @linebuffer = command
    @history_index -= 1 if @history_index > -(@history.length)
    cli_update command

  when arrowdown
    
    return if @history.empty? or @history_index == -1
    
    clear_cli()
    @history_index += 1      
    command = @history[@history_index]
    @linebuffer = command
    cli_update command

  else
  
    if key.length < 2  then
  
      @linebuffer << key
      key
    end

  end

end