feat(waybar): implement custom systemd status
Some checks failed
Check Formatting of Files / Check-Formatting (push) Failing after 37s

This commit is contained in:
Price Hiller 2025-01-09 18:51:28 -06:00
parent 3936043f80
commit 4b0fba074b
Signed by: Price
GPG Key ID: C3FADDE7A8534BEB
3 changed files with 85 additions and 8 deletions

View File

@ -20,7 +20,7 @@
], ],
"modules-right": [ "modules-right": [
"privacy", "privacy",
"systemd-failed-units", "custom/systemd-failed-units",
"tray", "tray",
"backlight", "backlight",
"network", "network",
@ -225,10 +225,10 @@
"icon-spacing": 2, "icon-spacing": 2,
"icon-size": 14, "icon-size": 14,
}, },
"systemd-failed-units": { "custom/systemd-failed-units": {
"format": "✗ Degraded Units: {nr_failed}", "exec": "~/.config/waybar/scripts/systemd-status.py",
"system": true, "return-type": "json",
"user": true "interval": 3
}, },
"tray": { "tray": {
"icon-size": 14 "icon-size": 14

View File

@ -0,0 +1,77 @@
#!/usr/bin/env -S nix shell nixpkgs#python3 nixpkgs#nix-prefetch-git --command python3
from enum import Enum
import subprocess
import json
from typing import NotRequired, TypedDict
class SystemUnitStatus(str, Enum):
Ok = "ok"
Degraded = "degraded"
WaybarJsonRet = TypedDict(
"WaybarJsonRet",
{
"text": str,
"class": SystemUnitStatus,
"alt": NotRequired[str],
"tooltip": NotRequired[str],
"percentage": NotRequired[str],
},
)
class FailedUnit(TypedDict):
unit: str
load: str
active: str
sub: str
description: str
def get_failed_units(extra_args: list[str] | None = None) -> list[FailedUnit]:
extra_args = extra_args or []
args = ["systemctl", "--state=failed", "--output=json"]
args.extend(extra_args)
units_proc = subprocess.run(args, capture_output=True)
units_proc.check_returncode()
failed_units: list[FailedUnit] = json.loads(units_proc.stdout)
return failed_units
system_failed_units = get_failed_units()
user_failed_units = get_failed_units(["--user"])
total_units_failed = len(system_failed_units) + len(user_failed_units)
status: SystemUnitStatus = SystemUnitStatus.Ok
text = ""
failed_units_tooltip: list[str] = []
if total_units_failed > 0:
text = f" Degraded Units: {total_units_failed}"
status = SystemUnitStatus.Degraded
failed_units_tooltip.extend(
map(
lambda unit: f"<b><span color='#FFA066'>System</span> <span color='#FF5D62'>></span> {unit["unit"]}</b>",
system_failed_units,
)
)
failed_units_tooltip.extend(
map(
lambda unit: f"<b><span color='#FFA066'>User</span> <span color='#FF5D62'>></span> {unit["unit"]}</b>",
user_failed_units,
)
)
out: WaybarJsonRet = {
"text": text,
"class": status,
"tooltip": "\r\n".join(failed_units_tooltip),
}
print(json.dumps(out))

View File

@ -72,7 +72,7 @@ tooltip label,
#tray, #tray,
#backlight, #backlight,
#privacy, #privacy,
#systemd-failed-units, #custom-systemd-failed-units,
#tray menu, #tray menu,
#custom-lock { #custom-lock {
background-color: #16161d; background-color: #16161d;
@ -285,11 +285,11 @@ tooltip {
color: #A3D4D5; color: #A3D4D5;
} }
#systemd-failed-units.ok { #custom-systemd-failed-units.ok {
color: #98BB6C; color: #98BB6C;
} }
#systemd-failed-units.degraded { #custom-systemd-failed-units.degraded {
color: #FF5D62; color: #FF5D62;
font-weight: bolder; font-weight: bolder;
} }