From f063883f6ccaa8b95664bb063ecb0e1f09bff9cc Mon Sep 17 00:00:00 2001 From: Sameer Rahmani Date: Sun, 12 Dec 2021 22:18:02 +0000 Subject: [PATCH] Add battery and memory units for the status bar --- core/cubes/modeline/statusbar-default.el | 34 ++++++++++++++++++++---- core/fg42/statusbar.el | 12 ++++++--- 2 files changed, 37 insertions(+), 9 deletions(-) diff --git a/core/cubes/modeline/statusbar-default.el b/core/cubes/modeline/statusbar-default.el index 11c7494..6fd3584 100644 --- a/core/cubes/modeline/statusbar-default.el +++ b/core/cubes/modeline/statusbar-default.el @@ -25,10 +25,33 @@ (require 'fg42/statusbar) -(defbar-unit battery 30 "B-" - (format "B%s" (with-temp-buffer - (insert-file-contents "/sys/class/power_supply/BAT0/capacity") - (buffer-string)))) +(defmacro fg42/percentage-face (value err warn) + "Return the correct face with repect to VALUE and the given +thresholds ERR adn WARN" + `(cond ((> ,value ,err) 'error) ((> ,value ,warn) 'warning) (t 'success))) + +(defbar-unit fg42/statusbar-battery 30 "B--" + (format "B%s" + (string-trim + (with-temp-buffer + (insert-file-contents "/sys/class/power_supply/BAT0/capacity") + (buffer-string))))) + + +(defbar-unit fg42/statusbar-memory 3 "M--S--" + (let* ((struct (string-trim + (shell-command-to-string "free -k|tail -n 2|awk '{printf(\"%s,%s,\", $3, $2)}'"))) + (vals (mapcar #'cl-parse-integer (butlast (split-string struct ",")))) + ;; TODO: Check for division by zero + (m (* 100 (/ (float(car vals)) (cadr vals)))) + ;; TODO: Check for division by zero + (s (* 100 (/ (float(caddr vals)) (cadddr vals)))) + (x )) + + (format + "M%sS%s" + (propertize (format "%02d" m) 'face (fg42/percentage-face m 90 75)) + (propertize (format "%02d" s) 'face (fg42/percentage-face s 90 75))))) ;; The default modeline for FG42 that is compatible with ;; the FG42's status bar only @@ -40,7 +63,8 @@ (list :brief (list - 'battery + 'fg42/statusbar-battery + 'fg42/statusbar-memory "|" '(buffer-name) "|" '(format-mode-line "%l:%c") "|" '(format-time-string "%m-%d %H:%M")) :long " ")) ;; No GUI diff --git a/core/fg42/statusbar.el b/core/fg42/statusbar.el index f131169..1a0c44e 100644 --- a/core/fg42/statusbar.el +++ b/core/fg42/statusbar.el @@ -173,10 +173,14 @@ that runs the given BODY at the given INTERVAL." `(progn (defvar ,name ,default) (defvar ,timer-name) - (add-hook 'fg42/statusbar-enable-hook - (lambda () - (setq ,timer-name - (run-with-timer 0 ,interval (lambda () ,@body))))) + (let ((fn (lambda () ,@body))) + (add-hook 'fg42/statusbar-enable-hook + (lambda () + (setq ,timer-name + (run-with-timer 0 ,interval + (lambda () + (setq ,name (funcall fn)))))))) + (add-hook 'fg42/statusbar-disable-hook (lambda () (when (timerp ,timer-name)