Class: TTY::Shell::Suggestion

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Suggestion

Initialize a Suggestion



38
39
40
41
42
# File 'lib/tty/shell/suggestion.rb', line 38

def initialize(options={})
  @indent      = options.fetch(:indent) { DEFAULT_INDENT }
  @single_text = options.fetch(:single_text) { SINGLE_TEXT }
  @plural_text = options.fetch(:plural_text) { PLURAL_TEXT }
end

Instance Attribute Details

#indentObject (readonly)

Number of spaces



17
18
19
# File 'lib/tty/shell/suggestion.rb', line 17

def indent
  @indent
end

#plural_textObject (readonly)

Text for multiple suggestions



27
28
29
# File 'lib/tty/shell/suggestion.rb', line 27

def plural_text
  @plural_text
end

#single_textObject (readonly)

Text for a single suggestion



22
23
24
# File 'lib/tty/shell/suggestion.rb', line 22

def single_text
  @single_text
end

Instance Method Details

#evaluate(suggestions) ⇒ 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

Parameters:

  • suggestions (Array[String])

Returns:

  • (String)


89
90
91
92
93
94
95
96
97
98
# File 'lib/tty/shell/suggestion.rb', line 89

def evaluate(suggestions)
  suggestion = ""
  if suggestions.one?
    suggestion << single_text + "\n"
    suggestion << (" " * indent + suggestions.first)
  else
    suggestion << plural_text + "\n"
    suggestion << suggestions.map { |suggestion| " " * indent + suggestion }.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

Parameters:

  • message (String)
  • possibilities (Array[String])

Returns:

  • (Hash)


73
74
75
76
77
78
79
80
# File 'lib/tty/shell/suggestion.rb', line 73

def measure_distances(message, possibilities)
  distances = Hash.new { |hash, key| hash[key] = [] }

  possibilities.each do |possibility|
    distances[Text.distance(message, possibility)] << possibility
  end
  distances
end

#suggest(message, possibilities) ⇒ Object

Suggest matches out of possibile strings

Parameters:

  • message (String)
  • possibilities (Array[String])


51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/tty/shell/suggestion.rb', line 51

def suggest(message, possibilities)
  distances        = measure_distances(message, possibilities)
  minimum_distance = distances.keys.min
  max_distance     = distances.keys.max

  if minimum_distance < max_distance
    suggestions = distances[minimum_distance].sort
    evaluate(suggestions)
  else
    nil
  end
end