From 14b3b82fcaa5c3e44c5e89de6b5ca802218f4a0e Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Sat, 2 Jun 2018 02:47:23 +0200 Subject: [PATCH] Remove exact match from `brew cask search`. --- .../cask/lib/hbc/cli/abstract_command.rb | 20 +++++++------------ Library/Homebrew/cask/lib/hbc/cli/search.rb | 17 +++++----------- Library/Homebrew/test/cask/cli/search_spec.rb | 5 ++--- 3 files changed, 14 insertions(+), 28 deletions(-) diff --git a/Library/Homebrew/cask/lib/hbc/cli/abstract_command.rb b/Library/Homebrew/cask/lib/hbc/cli/abstract_command.rb index 001a9623b2..2cfe3c467d 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/abstract_command.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/abstract_command.rb @@ -9,7 +9,7 @@ module Hbc option "--debug", :debug, false option "--verbose", :verbose, false option "--outdated", :outdated_only, false - option "--require-sha", :require_sha, false + option "--require-sha", :require_sha, false def self.command_name @command_name ||= name.sub(/^.*:/, "").gsub(/(.)([A-Z])/, '\1_\2').downcase @@ -49,24 +49,18 @@ module Hbc casks = args.empty? ? alternative.call : args @casks = casks.map { |cask| CaskLoader.load(cask) } rescue CaskUnavailableError => e - reason = [e.reason, suggestion_message(e.token)].join(" ") + reason = [e.reason, *suggestion_message(e.token)].join(" ") raise e.class.new(e.token, reason) end def suggestion_message(cask_token) - exact_match, partial_matches = Search.search(cask_token) + matches, = Search.search(cask_token) - if exact_match.nil? && partial_matches.count == 1 - exact_match = partial_matches.first - end - - if exact_match - "Did you mean “#{exact_match}”?" - elsif !partial_matches.empty? + if matches.one? + "Did you mean “#{matches.first}”?" + elsif !matches.empty? "Did you mean one of these?\n" - .concat(Formatter.columns(partial_matches.take(20))) - else - "" + .concat(Formatter.columns(matches.take(20))) end end end diff --git a/Library/Homebrew/cask/lib/hbc/cli/search.rb b/Library/Homebrew/cask/lib/hbc/cli/search.rb index 9256317413..4fbecccbf6 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/search.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/search.rb @@ -21,7 +21,6 @@ module Hbc end def self.search(*arguments) - exact_match = nil partial_matches = [] search_term = arguments.join(" ") search_regexp = extract_regexp arguments.first @@ -32,36 +31,30 @@ module Hbc else simplified_tokens = all_tokens.map { |t| t.sub(%r{^.*\/}, "").gsub(/[^a-z0-9]+/i, "") } simplified_search_term = search_term.sub(/\.rb$/i, "").gsub(/[^a-z0-9]+/i, "") - exact_match = simplified_tokens.grep(/^#{simplified_search_term}$/i) { |t| all_tokens[simplified_tokens.index(t)] }.first partial_matches = simplified_tokens.grep(/#{simplified_search_term}/i) { |t| all_tokens[simplified_tokens.index(t)] } - partial_matches.delete(exact_match) end _, remote_matches = Homebrew.search_taps(search_term, silent: true) - [exact_match, partial_matches, remote_matches, search_term] + [partial_matches, remote_matches, search_term] end - def self.render_results(exact_match, partial_matches, remote_matches, search_term) + def self.render_results(partial_matches, remote_matches, search_term) unless $stdout.tty? - puts [*exact_match, *partial_matches, *remote_matches] + puts [*partial_matches, *remote_matches] return end - if !exact_match && partial_matches.empty? && remote_matches.empty? + if partial_matches.empty? && remote_matches.empty? puts "No Cask found for \"#{search_term}\"." return end - if exact_match - ohai "Exact Match" - puts highlight_installed exact_match - end unless partial_matches.empty? if extract_regexp search_term ohai "Regexp Matches" else - ohai "Partial Matches" + ohai "Matches" end puts Formatter.columns(partial_matches.map(&method(:highlight_installed))) end diff --git a/Library/Homebrew/test/cask/cli/search_spec.rb b/Library/Homebrew/test/cask/cli/search_spec.rb index 5d8ad30e4f..5500500b81 100644 --- a/Library/Homebrew/test/cask/cli/search_spec.rb +++ b/Library/Homebrew/test/cask/cli/search_spec.rb @@ -13,7 +13,7 @@ describe Hbc::CLI::Search, :cask do expect { Hbc::CLI::Search.run("local") }.to output(<<~EOS).to_stdout.as_tty - ==> Partial Matches + ==> Matches local-caffeine local-transmission EOS @@ -94,9 +94,8 @@ describe Hbc::CLI::Search, :cask do expect { Hbc::CLI::Search.run("test-opera") }.to output(<<~EOS).to_stdout.as_tty - ==> Exact Match + ==> Matches test-opera - ==> Partial Matches test-opera-mail EOS end