class Commando::IOHandler

Handle the prompt/input for the command line interface

Attributes

config[R]

Public Class Methods

new(config:) click to toggle source
# File lib/commando/io_handler.rb, line 8
def initialize(config:)
  @config = config

  configure_readline
  load_history
end

Public Instance Methods

readline() click to toggle source
# File lib/commando/io_handler.rb, line 15
def readline
  line = Readline.readline(config.prompt, true)
  if line.nil?
    # When the user presses <CMD>+D, this comes through as nil. In that
    # case we want to exit
    config.output.puts
    raise QuitException
  elsif line.strip == ''
    # If the user just hit enter without typing a command, remove that line
    # from history.
    Readline::HISTORY.pop
    nil
  else
    save_history(line)
    line
  end
end

Private Instance Methods

configure_readline() click to toggle source
# File lib/commando/io_handler.rb, line 37
def configure_readline
  Readline.output = config.output
  Readline.completion_proc =
    proc { |s| config.commands.grep(/^#{Regexp.escape(s)}/) }
end
history_file() click to toggle source
# File lib/commando/io_handler.rb, line 55
def history_file
  config.history_file
end
load_history() click to toggle source
# File lib/commando/io_handler.rb, line 43
def load_history
  if history_file && File.exists?(history_file)
    File.readlines(history_file).each do |line|
      Readline::HISTORY.push(line.chomp)
    end
  end
end
save_history(line) click to toggle source
# File lib/commando/io_handler.rb, line 51
def save_history(line)
  File.open(history_file, 'a') { |f| f.puts(line) } if history_file
end