search: split up command into multiple methods

This commit is contained in:
fn ⌃ ⌥ 2021-11-05 11:22:29 -07:00
parent e38e25dfd1
commit 896aff0cf2

View File

@ -69,11 +69,7 @@ module Homebrew
def search
args = search_args.parse
if (package_manager = PACKAGE_MANAGERS.find { |name,| args[:"#{name}?"] })
_, url = package_manager
exec_browser url.call(URI.encode_www_form_component(args.named.join(" ")))
return
end
return if search_package_manager(args)
query = args.named.join(" ")
string_or_regex = query_regexp(query)
@ -81,6 +77,40 @@ module Homebrew
if args.desc?
search_descriptions(string_or_regex, args)
elsif args.pull_request?
search_pull_requests(query, args)
else
search_names(query, string_or_regex, args)
end
print_regex_help(args)
end
def print_regex_help(args)
return unless $stdout.tty?
metacharacters = %w[\\ | ( ) [ ] { } ^ $ * + ?].freeze
return unless metacharacters.any? do |char|
args.named.any? do |arg|
arg.include?(char) && !arg.start_with?("/")
end
end
opoo <<~EOS
Did you mean to perform a regular expression search?
Surround your query with /slashes/ to search locally by regex.
EOS
end
def search_package_manager(args)
package_manager = PACKAGE_MANAGERS.find { |name,| args[:"#{name}?"] }
return false if package_manager.nil?
_, url = package_manager
exec_browser url.call(URI.encode_www_form_component(args.named.join(" ")))
true
end
def search_pull_requests(query, args)
only = if args.open? && !args.closed?
"open"
elsif args.closed? && !args.open?
@ -88,7 +118,9 @@ module Homebrew
end
GitHub.print_pull_requests_matching(query, only)
else
end
def search_names(query, string_or_regex, args)
remote_results = search_taps(query, silent: true)
local_formulae = search_formulae(string_or_regex)
@ -121,19 +153,4 @@ module Homebrew
odie "No formulae or casks found for #{query.inspect}." if count.zero?
end
return unless $stdout.tty?
metacharacters = %w[\\ | ( ) [ ] { } ^ $ * + ?].freeze
return unless metacharacters.any? do |char|
args.named.any? do |arg|
arg.include?(char) && !arg.start_with?("/")
end
end
opoo <<~EOS
Did you mean to perform a regular expression search?
Surround your query with /slashes/ to search locally by regex.
EOS
end
end