]> code.delx.au - dotfiles/commitdiff
xmonad: use xfce4-genmon instead of xmobar for cpu/mem
authorJames Bunton <jamesbunton@delx.net.au>
Thu, 6 Nov 2014 06:38:42 +0000 (17:38 +1100)
committerJames Bunton <jamesbunton@delx.net.au>
Thu, 6 Nov 2014 06:40:10 +0000 (17:40 +1100)
.xmonad/xmobar.hs
bin/xfce4-genmon-script [new file with mode: 0755]

index a84cd4bf12b2feb7c46303587954f6d9334e2eb2..55e78622a6101fd45e0d138c795218e3d30679eb 100644 (file)
@@ -2,15 +2,9 @@ Config {
     font = "xft:sans-serif:size=10",
     bgColor = "#edeceb",
     fgColor = "black",
-    position = TopSize L 80 20,
-    commands = [
-        Run Cpu ["-t","Cpu:<total>%","-L","3","-H","50","--high","red","-p","3"] 10,
-        Run Memory ["-t","Mem:<usedratio>%","-p","3"] 10,
-        Run BatteryP ["BAT1"] ["-t","Batt: <acstatus><left>%","-L","25","--low","red","--","-O","+","-o","-","-f","ADP1/online"] 10,
-        Run StdinReader
-    ],
+    position = TopSize L 70 20,
+    commands = [Run StdinReader],
     sepChar = "%",
-    alignSep = "}{",
-    template = "%StdinReader% }{ %cpu% | %memory% | %battery% ",
-    lowerOnStart = False
+    template = "%StdinReader%",
+    lowerOnStart = False,
 }
diff --git a/bin/xfce4-genmon-script b/bin/xfce4-genmon-script
new file mode 100755 (executable)
index 0000000..4a3667c
--- /dev/null
@@ -0,0 +1,86 @@
+#!/usr/bin/env python3
+
+import time
+import sys
+
+def read_file(filename):
+    with open(filename) as f:
+        return f.read()
+
+def read_cpu_idle_jiffys(procstat):
+    line = procstat.split("\n")[0]
+    fields = line.split()
+    return int(fields[4])
+
+def count_cpus(procstat):
+    count = -1
+    for line in procstat.split("\n"):
+        if line.startswith("cpu"):
+            count += 1
+    return count
+
+def read_cpu_percent():
+    procstat = read_file("/proc/stat")
+    num_cpus = count_cpus(procstat)
+
+    tstart = time.time()
+    idle_jiffy1 = read_cpu_idle_jiffys(procstat)
+    time.sleep(0.1)
+    idle_jiffy2 = read_cpu_idle_jiffys(read_file("/proc/stat"))
+    tend = time.time()
+
+    duration = tend - tstart
+    idle_jiffys_per_second = (idle_jiffy2 - idle_jiffy1) / duration
+
+    idle_jiffys_per_cpu_second = idle_jiffys_per_second / num_cpus
+
+    # One jiffy is 10ms, so we can get the percentage very easily!
+    return 100 - idle_jiffys_per_cpu_second
+
+def read_mem_percent():
+    meminfo = read_file("/proc/meminfo")
+
+    d = {}
+    for line in meminfo.strip().split("\n"):
+        key, value = line.split()[:2]
+        d[key] = int(value)
+
+    mem_total = d["MemTotal:"]
+    mem_free = d["MemAvailable:"]
+    mem_used = mem_total - mem_free
+
+    return mem_used / mem_total * 100
+
+def write(s):
+    sys.stdout.write(s)
+
+def print_percent(name, units, value, red_threshold):
+    if value > red_threshold:
+        color = "red"
+    else:
+        color = "black"
+
+    write(
+        "%s <span color='%s'>%s</span>%s  " %
+        (name, color, value, units)
+    )
+
+def main():
+    write("<txt>")
+
+    print_percent(
+        "cpu", "%",
+        round(read_cpu_percent()),
+        red_threshold=50,
+    )
+
+    print_percent(
+        "mem", "%",
+        round(read_mem_percent()),
+        red_threshold=70,
+    )
+
+    write("</txt>")
+
+if __name__ == "__main__":
+    main()