Class: JSCommander::Shell::ReadlineConsole

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

Constant Summary collapse

HISTORY_FILE =
".jscmd_history"
MAX_HISTORY =
200

Instance Method Summary collapse

Constructor Details

#initialize(shell) ⇒ ReadlineConsole

Returns a new instance of ReadlineConsole.



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/jscmd/shell.rb', line 38

def initialize(shell)
  @shell = shell
  if File.exist?(history_path)
    hist = File.readlines(history_path).map{|line| line.chomp}
    Readline::HISTORY.push(*hist)
  end
  if Readline.methods.include?("basic_word_break_characters=")
    Readline.basic_word_break_characters = " \t\n\\`@><=;|&{([+-*/%"
  end
  Readline.completion_append_character = nil
  Readline.completion_proc = @shell.method(:complete_property).to_proc
end

Instance Method Details

#closeObject



55
56
57
58
59
60
61
62
63
# File 'lib/jscmd/shell.rb', line 55

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



34
35
36
# File 'lib/jscmd/shell.rb', line 34

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

#readlineObject



65
66
67
68
69
# File 'lib/jscmd/shell.rb', line 65

def readline
  line = Readline.readline("#{@shell.clients}> ", true)
  Readline::HISTORY.pop if /^\s*$/ =~ line
  line
end

#show_bannerObject



51
52
53
# File 'lib/jscmd/shell.rb', line 51

def show_banner
  $stderr.puts "Press Ctrl+D to exit."
end