-
Notifications
You must be signed in to change notification settings - Fork 2
/
brewt
executable file
·165 lines (134 loc) · 4.58 KB
/
brewt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/env ruby
##########################################
###
## File: brewt
## Desc: Like "gemt"
##
#
require 'pathname' # STDLIB
me = Pathname.new(__FILE__)
require 'amazing_print' # Pretty print Ruby objects with proper indentation and colors
require 'json' # STDLIB
require 'terminal-size' # A tiny gem to accomplish a simple task: Determining the terminal size.
require 'tty-spinner' # A terminal spinner for tasks that have non-deterministic time frame.
require 'word_wrapper' # Pure ruby word wrapping
USAGE = <<~EOS
list brew installed packages -- like "rake -T"
usage: #{me.basename} [-h | --help | -k | --keywords] [search_terms]
Where:
search_terms is an optional list of keywords or package names
seperated by spaces
if there are no search_terms then all
installed packages will be shown
Example:
brewt -k crystal shows list of the brew installed packages
that have "crystal" as part of its name or
description.
brewt crystal shows only information about a brew managed
package named 'crystal'
EOS
KEYWORD_SEARCH = false
unless ARGV.empty?
if %w[ -h --help ].include? ARGV.first
puts USAGE
exit
end
if %w[ -k --keyword ].include? ARGV.first
KEYWORD_SEARCH = true
ARGV.shift
end
end
search_terms = ARGV.map(&:downcase)
if KEYWORD_SEARCH || ARGV.empty?
options = '--installed'
else
options = search_terms.sort.join(' ')
end
spinner = TTY::Spinner.new("Loading information about installed brew packages ... :spinner", hide_cursor: true, clear: true, format: :pulse_2)
spinner.auto_spin
installed_packages = JSON.parse `brew info --json=v1 #{options}`
spinner.stop
puts
# look at the package full_name and desc to see if at
# least one of the search terms is present.
# package is a Hash
# search_terms is an Array of strings
def select_package(package, search_terms)
return true if search_terms.empty?
text = package['full_name'] + ' ' + package['desc']
search_terms.each do |term|
return true if text.include? term
end
return false
end
if KEYWORD_SEARCH
installed_packages.select! do |package|
select_package(package, search_terms)
end
end
longest_name = installed_packages.map{|package| package['full_name'].size}.max
desc_start_col = longest_name + 2
width = Terminal.size[:width] - desc_start_col
installed_packages.each do |package|
package_name = package['name']
package_full_name = package['full_name']
package_desc = package['desc']
package_link = package['homepage']
puts
print package_full_name
print " "*(desc_start_col - package_full_name.size)
text = WordWrapper::MinimumRaggedness.new(width, package_desc.strip).wrap
text += package_link.nil? ? '' : package_link
text_array = text.split("\n")
last_x = text_array.size-1
text_array.each_index do |x|
prefix = x == last_x ? '|__ ' : '| '
puts (x<1 ? text_array[x] : (' '*desc_start_col) + prefix+text_array[x] )
end
end # installed_packages.each do |package|
puts
__END__
ap installed_packages.first #=>
{
"name" => "ack",
"full_name" => "ack",
"oldname" => nil,
"aliases" => [],
"versioned_formulae" => [],
"desc" => "Search tool like grep, but optimized for programmers",
"homepage" => "https://beyondgrep.com/",
"versions" => {
"stable" => "2.24",
"devel" => "2.999-03",
"head" => "HEAD",
"bottle" => false
},
"revision" => 0,
"version_scheme" => 0,
"bottle" => {},
"keg_only" => false,
"options" => [],
"build_dependencies" => [],
"dependencies" => [],
"recommended_dependencies" => [],
"optional_dependencies" => [],
"requirements" => [],
"conflicts_with" => [],
"caveats" => nil,
"installed" => [
[0] {
"version" => "2.24",
"used_options" => [],
"built_as_bottle" => false,
"poured_from_bottle" => false,
"runtime_dependencies" => [],
"installed_as_dependency" => false,
"installed_on_request" => true
}
],
"linked_keg" => "2.24",
"pinned" => false,
"outdated" => false
} #=> nil
=end
#