class Toys::Completion::Candidate

A candidate for completing a string fragment.

A candidate includes a string representing the potential completed word, as well as a flag indicating whether it is a partial completion (i.e. a prefix that could still be added to) versus a final word. Generally, tab completion systems should add a trailing space after a final completion but not after a partial completion.

Attributes

string[R]

Get the candidate string. @return [String]

to_s[R]

Get the candidate string. @return [String]

Public Class Methods

new(string, partial: false) click to toggle source

Create a new candidate @param string [String] The candidate string @param partial [Boolean] Whether the candidate is partial. Defaults

to `false`.
# File lib/toys/completion.rb, line 150
def initialize(string, partial: false)
  @string = string.to_s
  @partial = partial ? true : false
end
new_multi(array, partial: false) click to toggle source

Create an array of candidates given an array of strings.

@param array [Array<String>] @return [Array<Toys::Completion::Candidate]

# File lib/toys/completion.rb, line 199
def self.new_multi(array, partial: false)
  array.map { |s| new(s, partial: partial) }
end

Public Instance Methods

<=>(other) click to toggle source

@private

# File lib/toys/completion.rb, line 184
def <=>(other)
  string <=> other.string
end
eql?(other) click to toggle source

@private

# File lib/toys/completion.rb, line 179
def eql?(other)
  other.is_a?(Candidate) && other.string.eql?(string) && other.partial? == @partial
end
final?() click to toggle source

Determine whether the candidate is a final completion. @return [Boolean]

# File lib/toys/completion.rb, line 174
def final?
  !@partial
end
hash() click to toggle source

@private

# File lib/toys/completion.rb, line 189
def hash
  string.hash ^ (partial? ? 1 : 0)
end
partial?() click to toggle source

Determine whether the candidate is partial completion. @return [Boolean]

# File lib/toys/completion.rb, line 166
def partial?
  @partial
end