Move pkg type logic into search_name

Now the search_name method takes the command line
args and only returns package types that line up
with those args.

That means it will only return casks if casks are valid
and same with formulae.
This commit is contained in:
apainintheneck 2022-12-13 20:38:00 -08:00
parent 0afc41ceef
commit cb64bc4df3
3 changed files with 26 additions and 32 deletions

View File

@ -303,15 +303,9 @@ module Homebrew
# Don't treat formula/cask name as a regex # Don't treat formula/cask name as a regex
query = string_or_regex = name query = string_or_regex = name
all_formulae, all_casks = search_names(query, string_or_regex) all_formulae, all_casks = search_names(query, string_or_regex, args)
print_formulae = args.formula? if all_formulae.any?
print_casks = args.cask?
print_formulae = print_casks = true if !print_formulae && !print_casks
print_formulae &&= all_formulae.any?
print_casks &&= all_casks.any?
if print_formulae
ohai "Formulae", Formatter.columns(all_formulae) ohai "Formulae", Formatter.columns(all_formulae)
first_formula = all_formulae.first.to_s first_formula = all_formulae.first.to_s
puts <<~EOS puts <<~EOS
@ -320,8 +314,8 @@ module Homebrew
brew install #{first_formula} brew install #{first_formula}
EOS EOS
end end
puts if print_formulae && print_casks puts if all_formulae.any? && all_casks.any?
if print_casks if all_casks.any?
ohai "Casks", Formatter.columns(all_casks) ohai "Casks", Formatter.columns(all_casks)
first_cask = all_casks.first.to_s first_cask = all_casks.first.to_s
puts <<~EOS puts <<~EOS
@ -330,7 +324,8 @@ module Homebrew
brew install --cask #{first_cask} brew install --cask #{first_cask}
EOS EOS
end end
return if all_formulae.any? || all_casks.any?
odie "No formulae or casks found for #{name}." if !print_formulae && !print_casks odie "No formulae or casks found for #{name}."
end end
end end

View File

@ -86,8 +86,8 @@ module Homebrew
elsif args.pull_request? elsif args.pull_request?
search_pull_requests(query, args) search_pull_requests(query, args)
else else
formulae, casks = search_names(query, string_or_regex) formulae, casks = search_names(query, string_or_regex, args)
print_results(formulae, casks, query, args) print_results(formulae, casks, query)
end end
puts "Use `brew desc` to list packages with a short description." if args.verbose? puts "Use `brew desc` to list packages with a short description." if args.verbose?
@ -130,30 +130,23 @@ module Homebrew
GitHub.print_pull_requests_matching(query, only) GitHub.print_pull_requests_matching(query, only)
end end
def print_results(all_formulae, all_casks, query, args) def print_results(all_formulae, all_casks, query)
print_formulae = args.formula? count = all_formulae.size + all_casks.size
print_casks = args.cask?
print_formulae = print_casks = true if !print_formulae && !print_casks
print_formulae &&= all_formulae.any?
print_casks &&= all_casks.any?
count = 0 if all_formulae.any?
if all_formulae
if $stdout.tty? if $stdout.tty?
ohai "Formulae", Formatter.columns(all_formulae) ohai "Formulae", Formatter.columns(all_formulae)
else else
puts all_formulae puts all_formulae
end end
count += all_formulae.count
end end
puts if print_formulae && print_casks puts if all_formulae.any? && all_casks.any?
if print_casks if all_casks.any?
if $stdout.tty? if $stdout.tty?
ohai "Casks", Formatter.columns(all_casks) ohai "Casks", Formatter.columns(all_casks)
else else
puts all_casks puts all_casks
end end
count += all_casks.count
end end
print_missing_formula_help(query, count.positive?) if all_casks.exclude?(query) print_missing_formula_help(query, count.positive?) if all_casks.exclude?(query)

View File

@ -115,16 +115,22 @@ module Homebrew
[] []
end end
def search_names(query, string_or_regex) def search_names(query, string_or_regex, args)
both = !args.formula? && !args.cask?
remote_results = search_taps(query, silent: true) remote_results = search_taps(query, silent: true)
local_formulae = search_formulae(string_or_regex) all_formulae = if args.formula? || both
remote_formulae = remote_results[:formulae] search_formulae(string_or_regex) + remote_results[:formulae]
all_formulae = local_formulae + remote_formulae else
[]
end
local_casks = search_casks(string_or_regex) all_casks = if args.cask? || both
remote_casks = remote_results[:casks] search_casks(string_or_regex) + remote_results[:casks]
all_casks = local_casks + remote_casks else
[]
end
[all_formulae, all_casks] [all_formulae, all_casks]
end end