Adds battery
This commit is contained in:
+18
-1
@@ -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 + '}'
|
||||
)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user