#!/usr/bin/env python3
import time
+import subprocess
import sys
def read_file(filename):
return mem_used / mem_total * 100
+def read_battery_percent():
+ args = ["upower", "--show-info", "/org/freedesktop/UPower/devices/DisplayDevice"]
+ with subprocess.Popen(args, stdout=subprocess.PIPE) as process:
+ upower_data, _ = process.communicate()
+ if process.poll() != 0:
+ return
+
+ for line in upower_data.decode("ascii").split("\n"):
+ line = line.strip()
+ if line.startswith("percentage:"):
+ percent_str = line.split()[1]
+ return int(percent_str.replace("%", ""))
+
def write(s):
sys.stdout.write(s)
-def print_percent(name, units, value, red_threshold):
- if value > red_threshold:
- color = "red"
+def print_percent(name, units, value, color_map):
+ if value is None:
+ return
+
+ value = round(value)
+
+ for low, high, color in color_map:
+ if value >= low and value <= high:
+ break
else:
color = "black"
print_percent(
"cpu", "%",
- round(read_cpu_percent()),
- red_threshold=50,
+ read_cpu_percent(),
+ color_map=[(50, 100, "red"), (0, 5, "green")],
)
print_percent(
"mem", "%",
- round(read_mem_percent()),
- red_threshold=70,
+ read_mem_percent(),
+ color_map=[(70, 100, "red")],
+ )
+
+ print_percent(
+ "batt", "%",
+ read_battery_percent(),
+ color_map=[(0, 25, "red")],
)
write("</txt>")