Class: BaseShell::ReadlineConsole
- Inherits:
-
Object
- Object
- BaseShell::ReadlineConsole
- Defined in:
- lib/vop/shell/base_shell.rb
Constant Summary collapse
- HISTORY_FILE =
".vop_history"
- MAX_HISTORY =
200
Instance Method Summary collapse
- #close ⇒ Object
- #history_path ⇒ Object
-
#initialize(shell) ⇒ ReadlineConsole
constructor
A new instance of ReadlineConsole.
- #readline ⇒ Object
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
#close ⇒ Object
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_path ⇒ Object
44 45 46 |
# File 'lib/vop/shell/base_shell.rb', line 44 def history_path File.join(ENV['HOME'] || ENV['USERPROFILE'], HISTORY_FILE) end |
#readline ⇒ Object
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 |