class Toys::Utils::CompletionEngine::Bash
A completion engine for bash.
Public Class Methods
new(cli)
click to toggle source
Create a bash completion engine.
@param cli [Toys::CLI] The CLI
.
# File lib/toys/utils/completion_engine.rb, line 23 def initialize(cli) @cli = cli end
Public Instance Methods
run()
click to toggle source
Perform completion in the current shell environment, which must include settings for the `COMP_LINE` and `COMP_POINT` environment variables. Prints out completion candidates, one per line, 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)
@return [Integer] status code
# File lib/toys/utils/completion_engine.rb, line 40 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] completions = run_internal(line) if completions completions.each { |completion| puts completion } 0 else 1 end end
run_internal(line)
click to toggle source
Internal completion method designed for testing. @private
# File lib/toys/utils/completion_engine.rb, line 59 def run_internal(line) words = CompletionEngine.split(line) quote_type, last = words.pop return nil unless words.shift words.map! { |_type, word| word } prefix = "" if (match = /\A(.*[=:])(.*)\z/.match(last)) prefix = match[1] last = match[2] end context = Completion::Context.new( cli: @cli, previous_words: words, fragment_prefix: prefix, fragment: last, params: {shell: :bash, quote_type: quote_type} ) candidates = @cli.completion.call(context) candidates.uniq.sort.map do |candidate| CompletionEngine.format_candidate(candidate, quote_type) end end