Class: Toys::Completion::Candidate

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/toys/completion.rb

Overview

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.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string, partial: false) ⇒ Candidate

Create a new candidate

Parameters:

  • string (String)

    The candidate string

  • partial (Boolean) (defaults to: false)

    Whether the candidate is partial. Defaults to false.



152
153
154
155
# File 'lib/toys/completion.rb', line 152

def initialize(string, partial: false)
  @string = string.to_s
  @partial = partial ? true : false
end

Instance Attribute Details

#stringString (readonly) Also known as: to_s

Get the candidate string.

Returns:

  • (String)


161
162
163
# File 'lib/toys/completion.rb', line 161

def string
  @string
end

Class Method Details

.new_multi(array, partial: false) ⇒ Array<Toys::Completion::Candidate]

Create an array of candidates given an array of strings.

Parameters:

  • array (Array<String>)

Returns:



207
208
209
# File 'lib/toys/completion.rb', line 207

def self.new_multi(array, partial: false)
  array.map { |s| new(s, partial: partial) }
end

Instance Method Details

#final?Boolean

Determine whether the candidate is a final completion.

Returns:

  • (Boolean)


176
177
178
# File 'lib/toys/completion.rb', line 176

def final?
  !@partial
end

#partial?Boolean

Determine whether the candidate is partial completion.

Returns:

  • (Boolean)


168
169
170
# File 'lib/toys/completion.rb', line 168

def partial?
  @partial
end