Refactor brew cask search
.
This commit is contained in:
parent
42e34db562
commit
cb7f25ceb8
@ -1,10 +1,13 @@
|
||||
require "hbc/cask_loader"
|
||||
require "hbc/dsl"
|
||||
require "hbc/metadata"
|
||||
require "searchable"
|
||||
|
||||
module Hbc
|
||||
class Cask
|
||||
extend Enumerable
|
||||
extend Forwardable
|
||||
extend Searchable
|
||||
include Metadata
|
||||
|
||||
attr_reader :token, :sourcefile_path, :config
|
||||
|
@ -14,31 +14,14 @@ module Hbc
|
||||
end
|
||||
end
|
||||
|
||||
def self.extract_regexp(string)
|
||||
if string =~ %r{^/(.*)/$}
|
||||
Regexp.last_match[1]
|
||||
else
|
||||
false
|
||||
end
|
||||
end
|
||||
|
||||
def self.search(*arguments)
|
||||
partial_matches = []
|
||||
search_term = arguments.join(" ")
|
||||
search_regexp = extract_regexp arguments.first
|
||||
all_tokens = CLI.nice_listing(Cask.map(&:qualified_token))
|
||||
if search_regexp
|
||||
search_term = arguments.first
|
||||
partial_matches = all_tokens.grep(/#{search_regexp}/i)
|
||||
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, "")
|
||||
partial_matches = simplified_tokens.grep(/#{simplified_search_term}/i) { |t| all_tokens[simplified_tokens.index(t)] }
|
||||
end
|
||||
query = arguments.join(" ")
|
||||
string_or_regex = query_regexp(query)
|
||||
local_results = search_casks(string_or_regex)
|
||||
|
||||
remote_matches = search_taps(search_term, silent: true)[:casks]
|
||||
remote_matches = search_taps(query, silent: true)[:casks]
|
||||
|
||||
[partial_matches, remote_matches, search_term]
|
||||
[local_results, remote_matches, query]
|
||||
end
|
||||
|
||||
def self.render_results(partial_matches, remote_matches, search_term)
|
||||
@ -53,22 +36,13 @@ module Hbc
|
||||
end
|
||||
|
||||
unless partial_matches.empty?
|
||||
if extract_regexp search_term
|
||||
ohai "Regexp Matches"
|
||||
else
|
||||
ohai "Matches"
|
||||
end
|
||||
puts Formatter.columns(partial_matches.map(&method(:highlight_installed)))
|
||||
ohai "Matches"
|
||||
puts Formatter.columns(partial_matches)
|
||||
end
|
||||
|
||||
return if remote_matches.empty?
|
||||
ohai "Remote Matches"
|
||||
puts Formatter.columns(remote_matches.map(&method(:highlight_installed)))
|
||||
end
|
||||
|
||||
def self.highlight_installed(token)
|
||||
return token unless Cask.new(token).installed?
|
||||
pretty_installed token
|
||||
puts Formatter.columns(remote_matches)
|
||||
end
|
||||
|
||||
def self.help
|
||||
|
@ -1,4 +1,5 @@
|
||||
require "searchable"
|
||||
require "hbc/cask"
|
||||
|
||||
module Homebrew
|
||||
module Search
|
||||
@ -79,5 +80,17 @@ module Homebrew
|
||||
end
|
||||
end.compact
|
||||
end
|
||||
|
||||
def search_casks(string_or_regex)
|
||||
results = Hbc::Cask.search(string_or_regex, &:token).sort_by(&:token)
|
||||
|
||||
results.map do |cask|
|
||||
if cask.installed?
|
||||
pretty_installed(cask.token)
|
||||
else
|
||||
cask.token
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -87,7 +87,7 @@ describe Hbc::CLI::Search, :cask do
|
||||
expect {
|
||||
Hbc::CLI::Search.run("/^local-c[a-z]ffeine$/")
|
||||
}.to output(<<~EOS).to_stdout.as_tty
|
||||
==> Regexp Matches
|
||||
==> Matches
|
||||
local-caffeine
|
||||
EOS
|
||||
end
|
||||
@ -110,13 +110,14 @@ describe Hbc::CLI::Search, :cask do
|
||||
EOS
|
||||
end
|
||||
|
||||
it "doesn't highlight packages that aren't installed" do
|
||||
expect(Hbc::CLI::Search.highlight_installed("local-caffeine")).to eq("local-caffeine")
|
||||
end
|
||||
|
||||
it "highlights installed packages" do
|
||||
Hbc::CLI::Install.run("local-caffeine")
|
||||
|
||||
expect(Hbc::CLI::Search.highlight_installed("local-caffeine")).to eq(pretty_installed("local-caffeine"))
|
||||
expect {
|
||||
Hbc::CLI::Search.run("local-caffeine")
|
||||
}.to output(<<~EOS).to_stdout.as_tty
|
||||
==> Matches
|
||||
local-caffeine ✔
|
||||
EOS
|
||||
end
|
||||
end
|
||||
|
Loading…
x
Reference in New Issue
Block a user