Remove exact match from brew cask search.

This commit is contained in:
Markus Reiter 2018-06-02 02:47:23 +02:00
parent e786bb08c9
commit 14b3b82fca
3 changed files with 14 additions and 28 deletions

View File

@ -49,24 +49,18 @@ module Hbc
casks = args.empty? ? alternative.call : args casks = args.empty? ? alternative.call : args
@casks = casks.map { |cask| CaskLoader.load(cask) } @casks = casks.map { |cask| CaskLoader.load(cask) }
rescue CaskUnavailableError => e 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) raise e.class.new(e.token, reason)
end end
def suggestion_message(cask_token) 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 if matches.one?
exact_match = partial_matches.first "Did you mean “#{matches.first}”?"
end elsif !matches.empty?
if exact_match
"Did you mean “#{exact_match}”?"
elsif !partial_matches.empty?
"Did you mean one of these?\n" "Did you mean one of these?\n"
.concat(Formatter.columns(partial_matches.take(20))) .concat(Formatter.columns(matches.take(20)))
else
""
end end
end end
end end

View File

@ -21,7 +21,6 @@ module Hbc
end end
def self.search(*arguments) def self.search(*arguments)
exact_match = nil
partial_matches = [] partial_matches = []
search_term = arguments.join(" ") search_term = arguments.join(" ")
search_regexp = extract_regexp arguments.first search_regexp = extract_regexp arguments.first
@ -32,36 +31,30 @@ module Hbc
else else
simplified_tokens = all_tokens.map { |t| t.sub(%r{^.*\/}, "").gsub(/[^a-z0-9]+/i, "") } 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, "") 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 = simplified_tokens.grep(/#{simplified_search_term}/i) { |t| all_tokens[simplified_tokens.index(t)] }
partial_matches.delete(exact_match)
end end
_, remote_matches = Homebrew.search_taps(search_term, silent: true) _, remote_matches = Homebrew.search_taps(search_term, silent: true)
[exact_match, partial_matches, remote_matches, search_term] [partial_matches, remote_matches, search_term]
end 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? unless $stdout.tty?
puts [*exact_match, *partial_matches, *remote_matches] puts [*partial_matches, *remote_matches]
return return
end end
if !exact_match && partial_matches.empty? && remote_matches.empty? if partial_matches.empty? && remote_matches.empty?
puts "No Cask found for \"#{search_term}\"." puts "No Cask found for \"#{search_term}\"."
return return
end end
if exact_match
ohai "Exact Match"
puts highlight_installed exact_match
end
unless partial_matches.empty? unless partial_matches.empty?
if extract_regexp search_term if extract_regexp search_term
ohai "Regexp Matches" ohai "Regexp Matches"
else else
ohai "Partial Matches" ohai "Matches"
end end
puts Formatter.columns(partial_matches.map(&method(:highlight_installed))) puts Formatter.columns(partial_matches.map(&method(:highlight_installed)))
end end

View File

@ -13,7 +13,7 @@ describe Hbc::CLI::Search, :cask do
expect { expect {
Hbc::CLI::Search.run("local") Hbc::CLI::Search.run("local")
}.to output(<<~EOS).to_stdout.as_tty }.to output(<<~EOS).to_stdout.as_tty
==> Partial Matches ==> Matches
local-caffeine local-caffeine
local-transmission local-transmission
EOS EOS
@ -94,9 +94,8 @@ describe Hbc::CLI::Search, :cask do
expect { expect {
Hbc::CLI::Search.run("test-opera") Hbc::CLI::Search.run("test-opera")
}.to output(<<~EOS).to_stdout.as_tty }.to output(<<~EOS).to_stdout.as_tty
==> Exact Match ==> Matches
test-opera test-opera
==> Partial Matches
test-opera-mail test-opera-mail
EOS EOS
end end