[Waybar] Add custom update manager

This commit is contained in:
2022-10-03 15:21:57 +02:00
parent 7b9a3c41dd
commit 70d96e45f1
3 changed files with 117 additions and 1 deletions

View File

@@ -1,7 +1,7 @@
[{
"spacing": 4, // Gaps between modules (4px)
"modules-left": ["sway/workspaces", "sway/mode", "custom/media"],
"modules-right": ["pulseaudio", "memory", "cpu", "battery", "network", "clock", "tray"],
"modules-right": ["pulseaudio", "memory", "cpu", "battery", "network", "custom/pacman", "clock", "tray"],
"memory": {
"format": "\uf538 {used:0.1f}GiB",
},
@@ -55,6 +55,17 @@
"default": ["", "", ""]
},
"on-click": "pavucontrol"
},
"custom/pacman": {
"format": "{icon} {}",
"return-type": "json",
"escape": true,
"format-icons": {
"uptodate": "\uf00c",
"available": "\uf019"
},
"interval": 600,
"exec": "$HOME/.config/waybar/pacman.py '(?:^(?:linux-|sway|wl)|pipewire)' 2> /dev/null"
}
}]

85
.config/waybar/pacman.py Executable file
View File

@@ -0,0 +1,85 @@
#!/usr/bin/env python3
import json
import os
import re
import subprocess
import sys
PACMAN_UPDATES_DB = f'/tmp/checkup-db-{os.getlogin()}/'
def write_output(packages, important_regex):
n_packages = packages.count('\n')
if n_packages == 0:
output_text = 'Up to date'
output_class = 'good'
output_alt = 'uptodate'
else:
if n_packages == 1:
output_text = '1 update available'
else:
output_text = f'{n_packages} updates available'
output_class = 'info'
output_alt = 'available'
if important_regex and re.search(important_regex, packages,
flags=re.MULTILINE):
output_class = 'important'
output = {'text': output_text,
'class': output_class,
'alt': output_alt}
sys.stdout.write(json.dumps(output) + '\n')
sys.stdout.flush()
def generate_and_update_database():
if not os.path.isdir(PACMAN_UPDATES_DB):
os.mkdir(PACMAN_UPDATES_DB)
subprocess.run(['fakeroot',
'--',
'pacman',
'-Sy',
'--dbpath',
PACMAN_UPDATES_DB,
'--logfile',
'/dev/null'],
env={'LC_ALL': 'C'},
stdout=subprocess.DEVNULL,
stderr=subprocess.STDOUT)
def check_updates(important_regex):
p = subprocess.run(['fakeroot',
'--',
'pacman',
'-Qu',
'--dbpath',
PACMAN_UPDATES_DB],
env={'LC_ALL': 'C'},
capture_output=True)
packages = p.stdout.decode('utf-8')
p = subprocess.run(['yay',
'-Qua',
'--devel'],
capture_output=True)
packages += p.stdout.decode('utf-8')
write_output(packages, important_regex)
def main(important_regex=None):
generate_and_update_database()
check_updates(important_regex)
if __name__ == '__main__':
if len(sys.argv) > 1:
main(sys.argv[1])
else:
main()

View File

@@ -136,6 +136,26 @@ label:focus {
color: #002b36;
}
#custom-pacman {
padding-left: 5px;
padding-right: 5px;
}
#custom-pacman.good {
background-color: #859900;
color: #002b36;
}
#custom-pacman.info {
background-color: #268bd2;
color: #002b36;
}
#custom-pacman.important {
background-color: #d30102;
color: #002b36;
}
#custom-media {
background-color: #66cc99;
color: #2a5c45;