From ba54bd7e4184c2d1a1e6d4f75399aef3ccb38533 Mon Sep 17 00:00:00 2001 From: Daniel Barlow Date: Fri, 25 Mar 2022 11:21:21 +0000 Subject: [PATCH] extract "system info" functions into new metric.fnl module --- blinkenlicht/README.md | 9 +++++---- blinkenlicht/bl.fnl | 23 +++++------------------ blinkenlicht/metric.fnl | 21 +++++++++++++++++++++ 3 files changed, 31 insertions(+), 22 deletions(-) create mode 100644 blinkenlicht/metric.fnl diff --git a/blinkenlicht/README.md b/blinkenlicht/README.md index 9dfe887..f2ea646 100644 --- a/blinkenlicht/README.md +++ b/blinkenlicht/README.md @@ -17,9 +17,9 @@ Not quite dogfood-ready yet, but fast approaching. * blinkenlicht.fnl is the module that parses `bar` and `indicator` forms and does all the UI -* also in bl.fnl but needs extracting to a home of its own, all the - glue that looks up load average, wifi signal strength, time of day - etc for indicators to use +* metric.fnl is a collection of functions that read metrics (load + average, battery status etc) from the system, for indicators to + display. Use the `default.nix` for guidance as to libraries and other setup required - or just use it, of course. @@ -44,7 +44,8 @@ Gtk will find it anyway. Magic. * [ ] allow height customisation * [ ] add some mechanism for indicators that wait for events instead of polling * [ ] set poll interval based on indicators' requested intervals -* [ ] set indicator background colour (use css for this?) +* [X] set indicator background colour (use css for this?) +* [ ] allow indicator to change styles based on status ```fennel diff --git a/blinkenlicht/bl.fnl b/blinkenlicht/bl.fnl index db42e6f..103ef4e 100644 --- a/blinkenlicht/bl.fnl +++ b/blinkenlicht/bl.fnl @@ -1,23 +1,10 @@ (local {: bar : indicator : stylesheet : run} (require :blinkenlicht)) +(local {: view} (require :fennel)) + +(local metric (require :metric)) (stylesheet "licht.css") -(fn loadavg [] - (with-open [f (io.open "/proc/loadavg" :r)] - (tonumber (: (f:read "*a") :match "[0-9.]+" )))) - -(fn disk-free-percent [] - 83) - -(fn battery-status [path] - (let [name (.. (or path "/sys/class/power_supply/BAT0") "/uevent")] - (with-open [f (io.open name :r)] - (let [fields {}] - (each [line #(f:read "*l")] - (let [(name value) (line:match "([^=]+)=(.+)")] - (tset fields (: (name:gsub "_" "-") :lower) value))) - fields)))) - (fn battery-icon-codepoint [status percent] (if ; (= status "Charging") 0xf376 ; glyph not present in font-awesome free (> percent 90) 0xf240 ;full @@ -41,7 +28,7 @@ [ (indicator { :interval 200 - :icon #(if (> (loadavg) 2) "face-sad" "face-smile") + :icon #(if (> (metric.loadavg) 2) "face-sad" "face-smile") }) ;; (let [f (io.open "/tmp/statuspipe" "r")] ;; (indicator { @@ -53,7 +40,7 @@ :classes ["yellow"] :text #(let [{:power-supply-energy-full full :power-supply-energy-now now - :power-supply-status status} (battery-status) + :power-supply-status status} (metric.battery) percent (math.floor (* 100 (/ (tonumber now) (tonumber full)))) icon-code (battery-icon-codepoint status percent)] (string.format "%s %d%%" (utf8.char icon-code) percent)) diff --git a/blinkenlicht/metric.fnl b/blinkenlicht/metric.fnl new file mode 100644 index 0000000..491b5b7 --- /dev/null +++ b/blinkenlicht/metric.fnl @@ -0,0 +1,21 @@ +(local {: view} (require :fennel)) + + +(fn loadavg [] + (with-open [f (io.open "/proc/loadavg" :r)] + (let [line (f:read "*a") + (one five fifteen) (line:match "([%d.]+) +([%d.]+) +([%d.]+)")] + (values (tonumber one) (tonumber five) (tonumber fifteen))))) + +(print (loadavg)) + +(fn battery [path] + (let [name (.. (or path "/sys/class/power_supply/BAT0") "/uevent")] + (with-open [f (io.open name :r)] + (let [fields {}] + (each [line #(f:read "*l")] + (let [(name value) (line:match "([^=]+)=(.+)")] + (tset fields (: (name:gsub "_" "-") :lower) value))) + fields)))) + +{ : loadavg : battery }