From 3f8d4a32f9c144f7a24a699137311426c85d4705 Mon Sep 17 00:00:00 2001 From: Thomas Forgione Date: Tue, 15 Apr 2025 22:50:20 +0200 Subject: [PATCH] Adds battery --- hypr/hyprscript | 19 ++++++++++++++++++- waybar/config | 13 +++++++++---- waybar/style.css | 17 +++++++++++++++-- 3 files changed, 42 insertions(+), 7 deletions(-) diff --git a/hypr/hyprscript b/hypr/hyprscript index 4e12cf9..189b7dd 100755 --- a/hypr/hyprscript +++ b/hypr/hyprscript @@ -8,6 +8,8 @@ import sys os.chdir(os.path.expanduser('~/.config/dotfiles/hypr')) bars = "_▂▃▄▅▆▇█" +batteries = "󰂎󰁺󰁻󰁼󰁽󰁾󰁿󰂀󰂁󰂁󰁹" +batteries_charging = "󰢟󰢜󰂆󰂇󰂈󰢝󰂉󰢞󰂊󰂋󰂅" class Monitor: """ @@ -154,6 +156,7 @@ def monitor_stats(): while True: cpu_percent = psutil.cpu_percent(interval=2) mem_percent = psutil.virtual_memory().percent + battery = psutil.sensors_battery() for i in range(len(cpu_values) - 1): cpu_values[i] = cpu_values[i+1] @@ -161,11 +164,25 @@ def monitor_stats(): cpu_values[-1] = cpu_percent mem_values[-1] = mem_percent + battery_index = round(11 * battery.percent / 100) + battery_icon = batteries_charging[battery_index] if battery.power_plugged else batteries[battery_index] + + if battery.power_plugged: + battery_class = 'charging' + elif battery.percent < 30.0: + battery_class = 'low' + elif battery.percent < 15.0: + battery_class = 'critical' + else: + battery_class = None + + battery_json = f', "class": "{battery_class}"' if battery_class else '' with open('.stat.txt', 'w') as f: f.write( ' ' + ''.join([to_bar(x) for x in cpu_values]) + ' ' + "% 5.1f"%cpu_percent + '%\n' + - ' ' + ''.join([to_bar(x) for x in mem_values]) + ' ' + "% 5.1f"%mem_percent + '%' + ' ' + ''.join([to_bar(x) for x in mem_values]) + ' ' + "% 5.1f"%mem_percent + '%\n' + + '{"text": "' + battery_icon + ' ' + "% 3.0f"%battery.percent + '%"' + battery_json + '}' ) diff --git a/waybar/config b/waybar/config index d26dbb0..cec9016 100644 --- a/waybar/config +++ b/waybar/config @@ -19,9 +19,9 @@ "hyprland/workspaces", "custom/cpu", "custom/mem", - "battery", + "custom/battery", "tray", - "clock", + "clock" ], "hyprland/window": { "format": "{}", @@ -136,11 +136,16 @@ "spacing": 5 }, "custom/cpu": { - "exec": "head -n 1 ~/.config/dotfiles/hypr/.stat.txt", + "exec": "sed '1q;d' ~/.config/dotfiles/hypr/.stat.txt", "restart-interval": 2 }, "custom/mem": { - "exec": "tail -n 1 ~/.config/dotfiles/hypr/.stat.txt", + "exec": "sed '2q;d' ~/.config/dotfiles/hypr/.stat.txt", + "restart-interval": 2 + }, + "custom/battery": { + "exec": "sed '3q;d' ~/.config/dotfiles/hypr/.stat.txt", + "return-type": "json", "restart-interval": 2 } } diff --git a/waybar/style.css b/waybar/style.css index 77bb5b5..0353832 100644 --- a/waybar/style.css +++ b/waybar/style.css @@ -100,8 +100,9 @@ button:hover { #pulseaudio, #idle_inhibitor, #custom-cpu, -#custom-mem { - padding: 0 5px; +#custom-mem, +#custom-battery { + padding: 0 10px; } #custom-cpu { @@ -112,6 +113,18 @@ button:hover { color: @blue; } +#custom-battery.charging { + color: @green; +} + +#custom-battery.low { + color: @yellow; +} + +#custom-battery.critical { + color: @red; +} + #network { color: @yellow; }