commit 8b7b4f9ae65429eefc04a257cd2dd5cba1881117 parent 8e0247bea06a6ba776743ec31781b1bcd9773aad Author: vin <git@vineetk.net> Date: Thu, 9 Apr 2026 11:33:49 -0400 files/emacs: add laptop battery status to modeline Diffstat:
| M | epistemia/files/emacs/.config/emacs/init.el | | | 28 | ++++++++++++++++++++++++++++ |
1 file changed, 28 insertions(+), 0 deletions(-)
diff --git a/epistemia/files/emacs/.config/emacs/init.el b/epistemia/files/emacs/.config/emacs/init.el @@ -658,3 +658,31 @@ (use-package erc-image :after erc) + +;;;;;;;;;;;;;;;;;; +;; LAPTOP STUFF ;; +;;;;;;;;;;;;;;;;;; +(defun vin/battery-left () + "Get the remaining battery capacity (empty string when full)." + (if (file-exists-p "/sys/class/power_supply/BAT0/") + (let* ((read-number-from-file (lambda (file) + (with-temp-buffer + (insert-file-contents file) + (string-to-number (buffer-string))))) + (capacity (funcall read-number-from-file "/sys/class/power_supply/BAT0/capacity")) + (power_now (funcall read-number-from-file "/sys/class/power_supply/BAT0/power_now")) + (energy_now (funcall read-number-from-file "/sys/class/power_supply/BAT0/energy_now")) + (remaining-life (if (zerop power_now) 0 (/ (float energy_now) power_now)))) + (if (zerop remaining-life) "" + (format "%d%% (%0d:%0d %.2fW)" + capacity + (truncate remaining-life) + (truncate (* (- remaining-life (truncate remaining-life)) 60)) + (/ power_now 1000000.0)))) + "")) + +(defvar vin/battery-status "") +(if (file-exists-p "/sys/class/power_supply/BAT0/") + (run-with-timer 0 60 (lambda () (setq vin/battery-status (vin/battery-left)))) + nil) +(setq global-mode-string '("| " display-time-string "| " vin/battery-status " |"))