Guard::ReadlineInteractor

Interactor that used readline for getting the user input. This enables history support and auto-completion, but is broken on OS X without installing `rb-readline` or using JRuby.

@see bugs.ruby-lang.org/issues/5539

Constants

COMPLETION_ACTIONS

Public Class Methods

new() click to toggle source

Initialize the interactor.

# File lib/guard/interactors/readline.rb, line 15
def initialize
  require 'readline'

  unless defined?(RbReadline) || defined?(JRUBY_VERSION) || RbConfig::CONFIG['target_os'] =~ /linux/
    ::Guard::UI.info 'Please add rb-readline for proper Readline support.'
  end

  Readline.completion_proc = proc { |word| auto_complete(word) }

  begin
    Readline.completion_append_character = ' '
  rescue NotImplementedError
    # Ignore, we just don't support it then
  end
end

Public Instance Methods

auto_complete(word) click to toggle source

Auto complete the given word.

@param [String] word the partial word @return [Array<String>] the matching words

# File lib/guard/interactors/readline.rb, line 63
def auto_complete(word)
  completion_list.grep(/^#{ Regexp.escape(word) }/)
end
completion_list() click to toggle source

Get the auto completion list.

@return [Array<String>] the list of words

# File lib/guard/interactors/readline.rb, line 71
def completion_list
  groups = ::Guard.groups.map { |group| group.name.to_s }
  guards = ::Guard.guards.map { |guard| guard.class.to_s.downcase.sub('guard::', '') }

  COMPLETION_ACTIONS + groups + guards - ['default']
end
prompt() click to toggle source

The current interactor prompt

@return [String] the prompt to show

# File lib/guard/interactors/readline.rb, line 82
def prompt
  ::Guard.listener.paused? ? 'p> ' : '> '
end
read_line() click to toggle source

Read a line from stdin with Readline.

# File lib/guard/interactors/readline.rb, line 47
def read_line
  while line = Readline.readline(prompt, true)
    line.gsub!(/^\W*/, '')
    if line =~ /^\s*$/ or Readline::HISTORY.to_a[-2] == line
      Readline::HISTORY.pop
    end

    process_input(line)
  end
end
start() click to toggle source

Start the interactor.

# File lib/guard/interactors/readline.rb, line 33
def start
  store_terminal_settings if stty_exists?
  super
end
stop() click to toggle source

Stop the interactor.

# File lib/guard/interactors/readline.rb, line 40
def stop
  super
  restore_terminal_settings if stty_exists?
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.