Adds battery

This commit is contained in:
Thomas Forgione 2025-04-15 22:50:20 +02:00
parent 3456c5ec2f
commit 3f8d4a32f9
3 changed files with 42 additions and 7 deletions

View File

@ -8,6 +8,8 @@ import sys
os.chdir(os.path.expanduser('~/.config/dotfiles/hypr')) os.chdir(os.path.expanduser('~/.config/dotfiles/hypr'))
bars = "_▂▃▄▅▆▇█" bars = "_▂▃▄▅▆▇█"
batteries = "󰂎󰁺󰁻󰁼󰁽󰁾󰁿󰂀󰂁󰂁󰁹"
batteries_charging = "󰢟󰢜󰂆󰂇󰂈󰢝󰂉󰢞󰂊󰂋󰂅"
class Monitor: class Monitor:
""" """
@ -154,6 +156,7 @@ def monitor_stats():
while True: while True:
cpu_percent = psutil.cpu_percent(interval=2) cpu_percent = psutil.cpu_percent(interval=2)
mem_percent = psutil.virtual_memory().percent mem_percent = psutil.virtual_memory().percent
battery = psutil.sensors_battery()
for i in range(len(cpu_values) - 1): for i in range(len(cpu_values) - 1):
cpu_values[i] = cpu_values[i+1] cpu_values[i] = cpu_values[i+1]
@ -161,11 +164,25 @@ def monitor_stats():
cpu_values[-1] = cpu_percent cpu_values[-1] = cpu_percent
mem_values[-1] = mem_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: with open('.stat.txt', 'w') as f:
f.write( f.write(
' ' + ''.join([to_bar(x) for x in cpu_values]) + ' ' + "% 5.1f"%cpu_percent + '%\n' + ' ' + ''.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 + '}'
) )

View File

@ -19,9 +19,9 @@
"hyprland/workspaces", "hyprland/workspaces",
"custom/cpu", "custom/cpu",
"custom/mem", "custom/mem",
"battery", "custom/battery",
"tray", "tray",
"clock", "clock"
], ],
"hyprland/window": { "hyprland/window": {
"format": "{}", "format": "{}",
@ -136,11 +136,16 @@
"spacing": 5 "spacing": 5
}, },
"custom/cpu": { "custom/cpu": {
"exec": "head -n 1 ~/.config/dotfiles/hypr/.stat.txt", "exec": "sed '1q;d' ~/.config/dotfiles/hypr/.stat.txt",
"restart-interval": 2 "restart-interval": 2
}, },
"custom/mem": { "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 "restart-interval": 2
} }
} }

View File

@ -100,8 +100,9 @@ button:hover {
#pulseaudio, #pulseaudio,
#idle_inhibitor, #idle_inhibitor,
#custom-cpu, #custom-cpu,
#custom-mem { #custom-mem,
padding: 0 5px; #custom-battery {
padding: 0 10px;
} }
#custom-cpu { #custom-cpu {
@ -112,6 +113,18 @@ button:hover {
color: @blue; color: @blue;
} }
#custom-battery.charging {
color: @green;
}
#custom-battery.low {
color: @yellow;
}
#custom-battery.critical {
color: @red;
}
#network { #network {
color: @yellow; color: @yellow;
} }