module Toys::Utils::CompletionEngine
Implementations of tab completion.
This module is not loaded by default. Before using it directly, you must `require “toys/utils/completion_engine”`
Public Class Methods
format_candidate(candidate, quote_type)
click to toggle source
@private
# File lib/toys/utils/completion_engine.rb, line 101 def format_candidate(candidate, quote_type) str = candidate.to_s partial = candidate.is_a?(Completion::Candidate) ? candidate.partial? : false quote_type = nil if candidate.string.include?("'") && quote_type == :single case quote_type when :single partial ? "'#{str}" : "'#{str}' " when :double str = str.gsub(/[$`"\\\n]/, '\\\\\\1') partial ? "\"#{str}" : "\"#{str}\" " else str = ::Shellwords.escape(str) partial ? str : "#{str} " end end
split(line)
click to toggle source
@private
# File lib/toys/utils/completion_engine.rb, line 82 def split(line) words = [] field = ::String.new quote_type = nil line.scan(split_regex) do |word, sqw, dqw, esc, garbage, sep| raise ArgumentError, "Didn't expect garbage: #{line.inspect}" if garbage field << field_str(word, sqw, dqw, esc) quote_type = update_quote_type(quote_type, sqw, dqw) if sep words << [quote_type, field] quote_type = nil field = sep.empty? ? nil : ::String.new end end words << [quote_type, field] if field words end
Private Class Methods
field_str(word, sqw, dqw, esc)
click to toggle source
# File lib/toys/utils/completion_engine.rb, line 128 def field_str(word, sqw, dqw, esc) word || sqw || dqw&.gsub(/\\([$`"\\\n])/, '\\1') || esc&.gsub(/\\(.)/, '\\1') || "" end
split_regex()
click to toggle source
# File lib/toys/utils/completion_engine.rb, line 119 def split_regex word_re = "([^\\s\\\\\\'\\\"]+)" sq_re = "'([^\\']*)(?:'|\\z)" dq_re = "\"((?:[^\\\"\\\\]|\\\\.)*)(?:\"|\\z)" esc_re = "(\\\\.?)" sep_re = "(\\s|\\z)" /\G\s*(?>#{word_re}|#{sq_re}|#{dq_re}|#{esc_re}|(\S))#{sep_re}?/m end
update_quote_type(quote_type, sqw, dqw)
click to toggle source
# File lib/toys/utils/completion_engine.rb, line 136 def update_quote_type(quote_type, sqw, dqw) if quote_type :multi elsif sqw :single elsif dqw :double else :bare end end