Method: Harp::REPL#complete_term

Defined in:
lib/harp/repl.rb

#complete_term(str) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/harp/repl.rb', line 54

def complete_term(str)
  # Terms can be either commands or indexes into the configuration
  # data structure.  No command contains a ".", so that's the test
  # we use to distinguish.
  bits = str.split(".")
  if bits.size > 1
    # An attempt to allow completion of either full configuration index
    # strings, or of component parts.  E.g., if the configuration contains
    # foo.bar.baz, this code will offer both "foo" and "foo.bar.baz"
    # as completions for "fo".
    v1 = @completions.grep(/^#{Regexp.escape(str)}/)
    v2 = @completions.grep(/^#{Regexp.escape(bits.last)}/)
    (v1 + v2.map {|x| (bits.slice(0..-2) << x).join(".") }).uniq
  else
    self.command_complete(str) +
      @completions.grep(/^#{Regexp.escape(str)}/)
  end
end