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

@ -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

View File

@ -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

View File

@ -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