Module: Pray::CLI::Suggest
- Defined in:
- lib/pray/cli/suggest.rb
Constant Summary collapse
- TOP_LEVEL_COMMANDS =
%w[ add apply clean confess drift explain fmt format help init install list login manifest outdated package plan prayer publish remove render repo serve sync tree trust unlock update vendor verify version ].freeze
Class Method Summary collapse
- .levenshtein_distance(left, right) ⇒ Object
- .suggest_command(input, candidates) ⇒ Object
- .unknown_command_message(command) ⇒ Object
Class Method Details
.levenshtein_distance(left, right) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/pray/cli/suggest.rb', line 30 def levenshtein_distance(left, right) left_chars = left.chars right_chars = right.chars left_length = left_chars.length right_length = right_chars.length return right_length if left_length.zero? return left_length if right_length.zero? previous_row = (0..right_length).to_a current_row = Array.new(right_length + 1, 0) left_chars.each_with_index do |left_character, left_index| current_row[0] = left_index + 1 right_chars.each_with_index do |right_character, right_index| substitution_cost = (left_character == right_character) ? 0 : 1 current_row[right_index + 1] = [ previous_row[right_index + 1] + 1, current_row[right_index] + 1, previous_row[right_index] + substitution_cost ].min end previous_row, current_row = current_row, previous_row end previous_row[right_length] end |
.suggest_command(input, candidates) ⇒ Object
21 22 23 24 25 26 27 28 |
# File 'lib/pray/cli/suggest.rb', line 21 def suggest_command(input, candidates) maximum_distance = (input.length <= 3) ? 1 : 2 candidates .map { |candidate| [candidate, levenshtein_distance(input, candidate)] } .select { |(_, distance)| distance <= maximum_distance } .min_by { |(_, distance)| distance } &.first end |
.unknown_command_message(command) ⇒ Object
14 15 16 17 18 19 |
# File 'lib/pray/cli/suggest.rb', line 14 def (command) = "unknown command: #{command}" suggestion = suggest_command(command, TOP_LEVEL_COMMANDS) = "#{}\nDid you mean `#{suggestion}`?" if suggestion "#{}\nSee 'pray --help'." end |