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'))
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 + '}'
)

View File

@ -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
}
}

View File

@ -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;
}