class Cmdr

file: cmdr.rb

Public Class Methods

new() click to toggle source
# File lib/cmdr.rb, line 9
def initialize()

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

end

Public Instance Methods

cli_banner() click to toggle source

display the cli banner upon initialisation

# File lib/cmdr.rb, line 89
def cli_banner()
  print "> "
end
history() click to toggle source
# 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) { |sub(/^\:/,'')| ... } click to toggle source
# 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

Protected Instance Methods

clear_cli() click to toggle source
# File lib/cmdr.rb, line 105
def clear_cli()
end
cli_update(s='') click to toggle source

display the current input

# File lib/cmdr.rb, line 101
def cli_update(s='') 
  print s
end
display_output(s='') click to toggle source

display the output

# File lib/cmdr.rb, line 110
def display_output(s='') 
  print s
end
reveal(c) click to toggle source

display the key pressed

# File lib/cmdr.rb, line 116
def reveal(c)
  print c
end