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/dsl"
|
||||||
require "hbc/metadata"
|
require "hbc/metadata"
|
||||||
|
require "searchable"
|
||||||
|
|
||||||
module Hbc
|
module Hbc
|
||||||
class Cask
|
class Cask
|
||||||
extend Enumerable
|
extend Enumerable
|
||||||
extend Forwardable
|
extend Forwardable
|
||||||
|
extend Searchable
|
||||||
include Metadata
|
include Metadata
|
||||||
|
|
||||||
attr_reader :token, :sourcefile_path, :config
|
attr_reader :token, :sourcefile_path, :config
|
||||||
|
@ -14,31 +14,14 @@ module Hbc
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.extract_regexp(string)
|
|
||||||
if string =~ %r{^/(.*)/$}
|
|
||||||
Regexp.last_match[1]
|
|
||||||
else
|
|
||||||
false
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.search(*arguments)
|
def self.search(*arguments)
|
||||||
partial_matches = []
|
query = arguments.join(" ")
|
||||||
search_term = arguments.join(" ")
|
string_or_regex = query_regexp(query)
|
||||||
search_regexp = extract_regexp arguments.first
|
local_results = search_casks(string_or_regex)
|
||||||
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
|
|
||||||
|
|
||||||
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
|
end
|
||||||
|
|
||||||
def self.render_results(partial_matches, remote_matches, search_term)
|
def self.render_results(partial_matches, remote_matches, search_term)
|
||||||
@ -53,22 +36,13 @@ module Hbc
|
|||||||
end
|
end
|
||||||
|
|
||||||
unless partial_matches.empty?
|
unless partial_matches.empty?
|
||||||
if extract_regexp search_term
|
ohai "Matches"
|
||||||
ohai "Regexp Matches"
|
puts Formatter.columns(partial_matches)
|
||||||
else
|
|
||||||
ohai "Matches"
|
|
||||||
end
|
|
||||||
puts Formatter.columns(partial_matches.map(&method(:highlight_installed)))
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return if remote_matches.empty?
|
return if remote_matches.empty?
|
||||||
ohai "Remote Matches"
|
ohai "Remote Matches"
|
||||||
puts Formatter.columns(remote_matches.map(&method(:highlight_installed)))
|
puts Formatter.columns(remote_matches)
|
||||||
end
|
|
||||||
|
|
||||||
def self.highlight_installed(token)
|
|
||||||
return token unless Cask.new(token).installed?
|
|
||||||
pretty_installed token
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.help
|
def self.help
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
require "searchable"
|
require "searchable"
|
||||||
|
require "hbc/cask"
|
||||||
|
|
||||||
module Homebrew
|
module Homebrew
|
||||||
module Search
|
module Search
|
||||||
@ -79,5 +80,17 @@ module Homebrew
|
|||||||
end
|
end
|
||||||
end.compact
|
end.compact
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
@ -87,7 +87,7 @@ describe Hbc::CLI::Search, :cask do
|
|||||||
expect {
|
expect {
|
||||||
Hbc::CLI::Search.run("/^local-c[a-z]ffeine$/")
|
Hbc::CLI::Search.run("/^local-c[a-z]ffeine$/")
|
||||||
}.to output(<<~EOS).to_stdout.as_tty
|
}.to output(<<~EOS).to_stdout.as_tty
|
||||||
==> Regexp Matches
|
==> Matches
|
||||||
local-caffeine
|
local-caffeine
|
||||||
EOS
|
EOS
|
||||||
end
|
end
|
||||||
@ -110,13 +110,14 @@ describe Hbc::CLI::Search, :cask do
|
|||||||
EOS
|
EOS
|
||||||
end
|
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
|
it "highlights installed packages" do
|
||||||
Hbc::CLI::Install.run("local-caffeine")
|
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
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user