Class: TTY::Shell::Suggestion
- Inherits:
-
Object
- Object
- TTY::Shell::Suggestion
- Defined in:
- lib/tty/shell/suggestion.rb
Overview
A class representing a suggestion
Constant Summary collapse
- DEFAULT_INDENT =
8
- SINGLE_TEXT =
'Did you mean this?'
- PLURAL_TEXT =
'Did you mean one of these?'
Instance Attribute Summary collapse
-
#indent ⇒ Object
readonly
Number of spaces.
-
#plural_text ⇒ Object
readonly
Text for multiple suggestions.
-
#single_text ⇒ Object
readonly
Text for a single suggestion.
-
#suggestions ⇒ Object
readonly
Possible suggestions.
Instance Method Summary collapse
-
#evaluate ⇒ String
private
Build up a suggestion string.
-
#initialize(options = {}) ⇒ Suggestion
constructor
Initialize a Suggestion.
-
#measure_distances(message, possibilities) ⇒ Hash
private
Measure distances between messag and possibilities.
-
#suggest(message, possibilities) ⇒ Object
Suggest matches out of possibile strings.
Constructor Details
#initialize(options = {}) ⇒ Suggestion
Initialize a Suggestion
41 42 43 44 45 46 |
# File 'lib/tty/shell/suggestion.rb', line 41 def initialize( = {}) @indent = .fetch(:indent) { DEFAULT_INDENT } @single_text = .fetch(:single_text) { SINGLE_TEXT } @plural_text = .fetch(:plural_text) { PLURAL_TEXT } @suggestions = [] end |
Instance Attribute Details
#indent ⇒ Object (readonly)
Number of spaces
15 16 17 |
# File 'lib/tty/shell/suggestion.rb', line 15 def indent @indent end |
#plural_text ⇒ Object (readonly)
Text for multiple suggestions
25 26 27 |
# File 'lib/tty/shell/suggestion.rb', line 25 def plural_text @plural_text end |
#single_text ⇒ Object (readonly)
Text for a single suggestion
20 21 22 |
# File 'lib/tty/shell/suggestion.rb', line 20 def single_text @single_text end |
#suggestions ⇒ Object (readonly)
Possible suggestions
30 31 32 |
# File 'lib/tty/shell/suggestion.rb', line 30 def suggestions @suggestions end |
Instance Method Details
#evaluate ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Build up a suggestion string
91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/tty/shell/suggestion.rb', line 91 def evaluate return suggestions if suggestions.empty? suggestion = '' if suggestions.one? suggestion << single_text + "\n" suggestion << (' ' * indent + @suggestions.first) else suggestion << plural_text + "\n" suggestion << suggestions.map do |sugest| ' ' * indent + sugest end.join("\n") end end |
#measure_distances(message, possibilities) ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Measure distances between messag and possibilities
75 76 77 78 79 80 81 82 |
# File 'lib/tty/shell/suggestion.rb', line 75 def measure_distances(, possibilities) distances = Hash.new { |hash, key| hash[key] = [] } possibilities.each do |possibility| distances[Text.distance(, possibility)] << possibility end distances end |
#suggest(message, possibilities) ⇒ Object
Suggest matches out of possibile strings
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/tty/shell/suggestion.rb', line 55 def suggest(, possibilities) distances = measure_distances(, possibilities) minimum_distance = distances.keys.min max_distance = distances.keys.max if minimum_distance < max_distance @suggestions = distances[minimum_distance].sort end evaluate end |