-
Notifications
You must be signed in to change notification settings - Fork 0
/
homebrew_upgrades.1d.py
executable file
·73 lines (52 loc) · 2.05 KB
/
homebrew_upgrades.1d.py
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
#!/usr/bin/env python3
# <swiftbar.hideRunInTerminal>true</swiftbar.hideRunInTerminal>
# <swiftbar.hideDisablePlugin>true</swiftbar.hideDisablePlugin>
# <swiftbar.hideSwiftBar>true</swiftbar.hideSwiftBar>
# <swiftbar.environment>[HOMEBREW_NO_ANALYTICS=1, HOMEBREW_NO_AUTO_UPDATE=1]</swiftbar.environment>
import json
import subprocess
from dataclasses import dataclass
import plugin
PLUGIN_ICON = "🍺"
BREW_PATH = "/opt/homebrew/bin/brew" # default location for Apple silicon
@dataclass
class Package:
def __init__(self, name: str, current_version: str, **_: object):
self.name = name
self.current_version = current_version
def main() -> None:
dump = subprocess.check_output([BREW_PATH, "list", "--installed-on-request"], text=True)
manually_installed = {line for line in dump.splitlines()}
outdated = subprocess.check_output([BREW_PATH, "outdated", "--json"], text=True)
data = json.loads(outdated)
formulas = [Package(**obj) for obj in data["formulae"] if obj["name"] in manually_installed]
casks = [Package(**obj) for obj in data["casks"]]
total = len(formulas) + len(casks)
if total == 0:
return
plugin.print_menu_item(PLUGIN_ICON)
plugin.print_menu_separator()
plugin.print_menu_action(
f"Upgrade {total} package(s)",
[BREW_PATH, "upgrade"],
refresh=True,
sfimage="arrow.up.square",
)
print_group("Formulas", formulas)
print_group("Casks", casks)
def print_group(title: str, packages: list[Package]) -> None:
if len(packages) == 0:
return
plugin.print_menu_separator()
plugin.print_menu_item(title)
longest_name_length = max(len(pkg.name) for pkg in packages)
for pkg in packages:
plugin.print_menu_action(
f"{pkg.name:<{longest_name_length}} {pkg.current_version}",
[BREW_PATH, "upgrade", pkg.name],
refresh=True,
sfimage="shippingbox",
font="SFMono-Regular", # use a monospaced font for a proper alignment
)
if __name__ == "__main__":
main()