1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
;;; 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)
;; One letter suffices
(setq power-source (substring (match-string 1) 0 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 79)
(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
|