diff options
author | JSDurand <mmemmew@gmail.com> | 2021-04-20 09:27:17 +0800 |
---|---|---|
committer | JSDurand <mmemmew@gmail.com> | 2021-04-20 09:27:17 +0800 |
commit | a8fed34329cfb2f3095c300d1e910008d7a7b2b1 (patch) | |
tree | f6940b0ff43b4547e36826ffb37f25e38b2b3c61 | |
parent | d5c1b16c18bd78fe33def35cdbfcda30ca07a0a1 (diff) |
Display battery and time on the modeline.
Now I can use Emacs in the full-screen mode.
* battery-conf.el (battery-pmset):
(durand-battery-pmset): Override the default so that
it also offers temperature information.
(battery-mode-line-format):
(battery-mode-line-limit):
(battery-update-interval):
(battery-load-low):
(battery-load-critical):
Some sane defaults.
* init.el (durand-battery):
(durand-time): Advice to load the configurations and
display at the same time.
* time-conf.el (display-time-format):
(display-time-interval):
(display-time-default-load-average):
(display-time-mail-directory):
(display-time-mail-function):
(display-time-use-mail-icon):
(display-time-mail-string):
(display-time-mail-face):
Sane defaults.
-rw-r--r-- | battery-conf.el | 99 | ||||
-rw-r--r-- | init.el | 8 | ||||
-rw-r--r-- | time-conf.el | 43 |
3 files changed, 150 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 @@ -128,6 +128,14 @@ no effect." (define-key global-map (vector 3 ?l) #'prepare-org) ;;;###autoload +(load-after-function durand-battery "battery-conf.el" "Display battery information." nil + (display-battery-mode 1)) + +;;;###autoload +(load-after-function durand-time "time-conf.el" "Display time information." nil + (display-time-mode 1)) + +;;;###autoload (load-after-function durand-mu4e "mail.el" "Open mu4e." t (mu4e)) (define-key global-map (vector 3 109) #'durand-mu4e) diff --git a/time-conf.el b/time-conf.el new file mode 100644 index 0000000..4456833 --- /dev/null +++ b/time-conf.el @@ -0,0 +1,43 @@ +;;; time-conf.el --- Display time -*- lexical-binding: t; -*- + +;; Copyright (C) 2021 李俊緯 + +;; Author: 李俊緯 <mmemmew@gmail.com> +;; Keywords: convenience, help, calendar, 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: + +;; Display time on the mode line. + +;;; Code: + +(setq display-time-format "%H:%M %Y-%m-%d") + ;;;; Covered by `display-time-format' +;; (setq display-time-24hr-format t) +;; (setq display-time-day-and-date t) +(setq display-time-interval 60) +(setq display-time-default-load-average nil) +;; NOTE: I don't want to see a mail indicator on the mode line. +(setq display-time-mail-directory nil) +(setq display-time-mail-function nil) +(setq display-time-use-mail-icon nil) +(setq display-time-mail-string nil) +(setq display-time-mail-face nil) + +(add-hook 'after-init-hook #'display-time-mode) + +(provide 'time-conf) +;;; time-conf.el ends here |