Class: Hbc::CLI::Search

Inherits:
AbstractCommand show all
Defined in:
Library/Homebrew/cask/lib/hbc/cli/search.rb

Instance Attribute Summary

Attributes inherited from AbstractCommand

#args

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractCommand

abstract?, command_name, #initialize, needs_init?, run, visible

Methods included from Options

included, #process_arguments

Constructor Details

This class inherits a constructor from Hbc::CLI::AbstractCommand

Class Method Details

.extract_regexp(string) ⇒ Object



13
14
15
16
17
18
19
# File 'Library/Homebrew/cask/lib/hbc/cli/search.rb', line 13

def self.extract_regexp(string)
  if string =~ %r{^/(.*)/$}
    Regexp.last_match[1]
  else
    false
  end
end

.helpObject



96
97
98
# File 'Library/Homebrew/cask/lib/hbc/cli/search.rb', line 96

def self.help
  "searches all known Casks"
end

.highlight_installed(token) ⇒ Object



91
92
93
94
# File 'Library/Homebrew/cask/lib/hbc/cli/search.rb', line 91

def self.highlight_installed(token)
  return token unless Cask.new(token).installed?
  pretty_installed token
end

.render_results(exact_match, partial_matches, remote_matches, search_term) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'Library/Homebrew/cask/lib/hbc/cli/search.rb', line 62

def self.render_results(exact_match, partial_matches, remote_matches, search_term)
  unless $stdout.tty?
    puts [*exact_match, *partial_matches, *remote_matches]
    return
  end

  if !exact_match && partial_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"
    end
    puts Formatter.columns(partial_matches.map(&method(:highlight_installed)))
  end

  return if remote_matches.empty?
  ohai "Remote Matches"
  puts Formatter.columns(remote_matches.map(&method(:highlight_installed)))
end

.search(*arguments) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'Library/Homebrew/cask/lib/hbc/cli/search.rb', line 40

def self.search(*arguments)
  exact_match = nil
  partial_matches = []
  search_term = arguments.join(" ")
  search_regexp = extract_regexp arguments.first
  all_tokens = CLI.nice_listing(Hbc.all_tokens)
  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, "")
    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 = search_remote(search_term)

  [exact_match, partial_matches, remote_matches, search_term]
end

.search_remote(query) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'Library/Homebrew/cask/lib/hbc/cli/search.rb', line 21

def self.search_remote(query)
  matches = begin
    GitHub.search_code(
      user: "caskroom",
      path: "Casks",
      filename: query,
      extension: "rb",
    )
  rescue GitHub::Error => error
    opoo "Error searching on GitHub: #{error}\n"
    []
  end
  matches.map do |match|
    tap = Tap.fetch(match["repository"]["full_name"])
    next if tap.installed?
    "#{tap.name}/#{File.basename(match["path"], ".rb")}"
  end.compact
end

Instance Method Details

#runObject



4
5
6
7
8
9
10
11
# File 'Library/Homebrew/cask/lib/hbc/cli/search.rb', line 4

def run
  if args.empty?
    puts Formatter.columns(CLI.nice_listing(Hbc.all_tokens))
  else
    results = self.class.search(*args)
    self.class.render_results(*results)
  end
end