Class: Toys::Utils::CompletionEngine::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/toys/utils/completion_engine.rb

Overview

Base class for shell completion engines that use a COMP_LINE / COMP_POINT protocol.

Completion candidates are always computed as whole words: the entire word under the cursor is presented to the completion as the fragment, and each candidate is a replacement for that entire word. A shell that replaces less than the whole word, because it breaks words at certain characters, declares those characters in the private method #word_break_chars, and the candidates are trimmed to match.

Subclasses must implement the private methods #shell_name and #output_completions, and may override #word_break_chars.

Direct Known Subclasses

Bash, Zsh

Instance Method Summary collapse

Constructor Details

#initialize(completion, loader) ⇒ Base

Create a completion engine.

Parameters:



43
44
45
46
# File 'lib/toys/utils/completion_engine.rb', line 43

def initialize(completion, loader)
  @completion = completion
  @loader = loader
end

Instance Method Details

#runInteger

Perform completion in the current shell environment, which must include settings for the COMP_LINE and COMP_POINT environment variables. Prints out completion candidates and returns a status code indicating the result.

  • 0 for success.
  • 1 if completion failed.
  • 2 if the environment is incorrect (e.g. expected environment variables not found)

Returns:

  • (Integer)

    status code



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/toys/utils/completion_engine.rb', line 61

def run
  return 2 if !::ENV.key?("COMP_LINE") || !::ENV.key?("COMP_POINT")
  line = ::ENV["COMP_LINE"].to_s
  point = ::ENV["COMP_POINT"].to_i
  point = line.length if point.negative?
  line = line[0, point]
  result = run_internal(line)
  if result
    output_completions(*result)
    0
  else
    1
  end
end