extract "system info" functions into new metric.fnl module

This commit is contained in:
Daniel Barlow 2022-03-25 11:21:21 +00:00
parent e198e8aae5
commit ba54bd7e41
3 changed files with 31 additions and 22 deletions

View File

@ -17,9 +17,9 @@ Not quite dogfood-ready yet, but fast approaching.
* blinkenlicht.fnl is the module that parses `bar` and `indicator` * blinkenlicht.fnl is the module that parses `bar` and `indicator`
forms and does all the UI forms and does all the UI
* also in bl.fnl but needs extracting to a home of its own, all the * metric.fnl is a collection of functions that read metrics (load
glue that looks up load average, wifi signal strength, time of day average, battery status etc) from the system, for indicators to
etc for indicators to use display.
Use the `default.nix` for guidance as to libraries and other setup Use the `default.nix` for guidance as to libraries and other setup
required - or just use it, of course. required - or just use it, of course.
@ -44,7 +44,8 @@ Gtk will find it anyway. Magic.
* [ ] allow height customisation * [ ] allow height customisation
* [ ] add some mechanism for indicators that wait for events instead of polling * [ ] add some mechanism for indicators that wait for events instead of polling
* [ ] set poll interval based on indicators' requested intervals * [ ] 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 ```fennel

View File

@ -1,23 +1,10 @@
(local {: bar : indicator : stylesheet : run} (require :blinkenlicht)) (local {: bar : indicator : stylesheet : run} (require :blinkenlicht))
(local {: view} (require :fennel))
(local metric (require :metric))
(stylesheet "licht.css") (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] (fn battery-icon-codepoint [status percent]
(if ; (= status "Charging") 0xf376 ; glyph not present in font-awesome free (if ; (= status "Charging") 0xf376 ; glyph not present in font-awesome free
(> percent 90) 0xf240 ;full (> percent 90) 0xf240 ;full
@ -41,7 +28,7 @@
[ [
(indicator { (indicator {
:interval 200 :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")] ;; (let [f (io.open "/tmp/statuspipe" "r")]
;; (indicator { ;; (indicator {
@ -53,7 +40,7 @@
:classes ["yellow"] :classes ["yellow"]
:text #(let [{:power-supply-energy-full full :text #(let [{:power-supply-energy-full full
:power-supply-energy-now now :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)))) percent (math.floor (* 100 (/ (tonumber now) (tonumber full))))
icon-code (battery-icon-codepoint status percent)] icon-code (battery-icon-codepoint status percent)]
(string.format "%s %d%%" (utf8.char icon-code) percent)) (string.format "%s %d%%" (utf8.char icon-code) percent))

21
blinkenlicht/metric.fnl Normal file
View File

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