diff options
Diffstat (limited to 'battery-conf.el')
-rw-r--r-- | battery-conf.el | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/battery-conf.el b/battery-conf.el new file mode 100644 index 0000000..2781eda --- /dev/null +++ b/battery-conf.el @@ -0,0 +1,99 @@ +;;; battery-conf.el --- COnfigurations of the display of battery -*- lexical-binding: t; -*- + +;; Copyright (C) 2021 李俊緯 + +;; Author: 李俊緯 <mmemmew@gmail.com> +;; Keywords: convenience, processes, hardware, unix + +;; This program is free software; you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation, either version 3 of the License, or +;; (at your option) any later version. + +;; This program is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with this program. If not, see <https://www.gnu.org/licenses/>. + +;;; Commentary: + +;; Some configurations of the display of the information of battery. + +;;; Code: + +(defun durand-battery-pmset () + "Get battery status information using `pmset'. + +The following %-sequences are provided: +%L Power source (verbose) +%B Battery status (verbose) +%b Battery status, empty means high, `-' means low, + `!' means critical, and `+' means charging +%d Battery temperature -- added by Durand + +%p Battery load percentage +%h Remaining time in hours +%m Remaining time in minutes +%t Remaining time in the form `h:min'" + (let (power-source load-percentage battery-status battery-status-symbol + battery-temp remaining-time hours minutes) + (with-temp-buffer + (ignore-errors (call-process "pmset" nil t nil "-g" "ps")) + (goto-char (point-min)) + (when (re-search-forward "\\(?:Currentl?y\\|Now\\) drawing from '\\(AC\\|Battery\\) Power'" nil t) + (setq power-source (match-string 1)) + (when (re-search-forward "^ -InternalBattery-0\\([ \t]+(id=[0-9]+)\\)*[ \t]+" nil t) + (when (looking-at "\\([0-9]\\{1,3\\}\\)%") + (setq load-percentage (match-string 1)) + (goto-char (match-end 0)) + (cond ((looking-at "; charging") + (setq battery-status "charging" + battery-status-symbol "+")) + ((< (string-to-number load-percentage) battery-load-critical) + (setq battery-status "critical" + battery-status-symbol "!")) + ((< (string-to-number load-percentage) battery-load-low) + (setq battery-status "low" + battery-status-symbol "-")) + (t + (setq battery-status "high" + battery-status-symbol ""))) + (when (re-search-forward "\\(\\([0-9]+\\):\\([0-9]+\\)\\) remaining" nil t) + (setq remaining-time (match-string 1)) + (let ((h (string-to-number (match-string 2))) + (m (string-to-number (match-string 3)))) + (setq hours (number-to-string (+ h (if (< m 30) 0 1))) + minutes (number-to-string (+ (* h 60) m)))))))) + ;; Now battery temperature + (erase-buffer) + (call-process "ioreg" nil t nil "-n" "AppleSmartBattery" "-r") + (goto-char (point-min)) + (re-search-forward "Temperature..=." nil t) + (re-search-forward "[[:digit:]]+" nil t) + (setq battery-temp + (format "%.1f" + (/ (string-to-number (match-string-no-properties 0)) + 100.0)))) + (list (cons ?L (or power-source "N/A")) + (cons ?d (or battery-temp "N/A")) + (cons ?p (or load-percentage "N/A")) + (cons ?B (or battery-status "N/A")) + (cons ?b (or battery-status-symbol "")) + (cons ?h (or hours "N/A")) + (cons ?m (or minutes "N/A")) + (cons ?t (or remaining-time "N/A"))))) + +(advice-add 'battery-pmset :override 'durand-battery-pmset) + +(setq battery-mode-line-format " %d %L %b%p%%%%") +(setq battery-mode-line-limit 90) +(setq battery-update-interval 180) +(setq battery-load-low 20) +(setq battery-load-critical 10) +(add-hook 'after-init-hook #'display-battery-mode) + +(provide 'battery-conf) +;;; battery-conf.el ends here |