gemacs/importers/battery.scm

57 lines
1.6 KiB
Scheme
Raw Normal View History

(define-module (importers battery)
#:use-module (ice-9 popen)
#:use-module (ice-9 rdelim)
#:use-module (ice-9 textual-ports)
;; #:use-module (ice-9 format)
#:use-module (srfi srfi-19) ; string/date
#:use-module (org format)
#:use-module (srfi srfi-1)
#:export (*battery-path*
get-battery
battery->org))
;; Depends on ubattery
;; use "ubattery --enumerate" to list batteries
(define *battery-path* "")
(define (get-battery battery-path)
(let* ((port
(open-input-pipe
(string-join `("upower" "--dump" ,battery-path) " ")))
(str (get-string-all port)))
(close-pipe port)
str))
(define (battery-props battery-path)
(filter!
(lambda (x) (not (unspecified? x)))
(map
(lambda (x) (when (= 2 (length x))
`(,(string-trim-both (car x)) .
,(string-trim-both (cadr x)))))
(map
(lambda (x) (string-split x #\:))
(string-split (get-battery battery-path) #\nl)))))
(define (get-battery-prop battery-path prop)
(cdr (assoc prop (battery-props battery-path))))
(define (battery->org-props btr)
(org-properties
`(("STATUS" ,(get-battery-prop btr "state"))
("CAPACITY" ,(get-battery-prop btr "capacity"))
("PERCENTAGE" ,(get-battery-prop btr "percentage"))
("LID-CLOSED" ,(get-battery-prop btr "lid-is-closed"))
("CONSUMPTION" ,(get-battery-prop btr "energy-rate"))
)))
(define (battery->org battery)
(string-concatenate
`("* "
,(date->org (current-date) #t #t) " "
"Battery "
,(get-battery-prop *battery-path* "native-path")
"\n"
,(battery->org-props battery)
"\n")))