Class: BaseShell::ReadlineConsole

Inherits:
Object
  • Object
show all
Defined in:
lib/vop/shell/base_shell.rb

Constant Summary collapse

HISTORY_FILE =
".vop_history"
MAX_HISTORY =
200

Instance Method Summary collapse

Constructor Details

#initialize(shell) ⇒ ReadlineConsole

Returns a new instance of ReadlineConsole.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/vop/shell/base_shell.rb', line 48

def initialize(shell)
  @shell = shell

  if File.exist?(history_path)
    hist = File.readlines(history_path).map{|line| line.chomp}
    Readline::HISTORY.push(*hist)
  end

  #Readline.basic_word_break_characters = " \t\n\\`@><=;|&{([+*%"

  # see http://stackoverflow.com/questions/13876024/how-to-write-a-ruby-command-line-app-that-supports-tab-completion#13876556
  Readline.completer_word_break_characters = ""

  Readline.completion_append_character = nil
  Readline.completion_proc = @shell.backend.method(:complete).to_proc
end

Instance Method Details

#closeObject



65
66
67
68
69
70
71
72
73
# File 'lib/vop/shell/base_shell.rb', line 65

def close
  open(history_path, "wb") do |f|
    history = Readline::HISTORY.to_a
    if history.size > MAX_HISTORY
      history = history[history.size - MAX_HISTORY, MAX_HISTORY]
    end
    history.each{|line| f.puts(line)}
  end
end

#history_pathObject



44
45
46
# File 'lib/vop/shell/base_shell.rb', line 44

def history_path
  File.join(ENV['HOME'] || ENV['USERPROFILE'], HISTORY_FILE)
end

#readlineObject



75
76
77
78
79
# File 'lib/vop/shell/base_shell.rb', line 75

def readline
  line = Readline.readline(@shell.backend.prompt, true)
  Readline::HISTORY.pop if /^\s*$/ =~ line
  line
end