[Optimize] Improve the performance of fancy-battery segment.

This commit is contained in:
Vincent Zhang 2019-03-22 01:50:29 +08:00
parent 3b704ee080
commit 748824e692
2 changed files with 73 additions and 65 deletions

View file

@ -1952,11 +1952,13 @@ we don't want to remove that so we just return the original."
;; fancy battery ;; fancy battery
;; ;;
(doom-modeline-def-segment fancy-battery (defvar-local doom-modeline--battery-status nil)
(when (and (doom-modeline--active) (defun doom-modeline-update-battery-status (&optional status)
(bound-and-true-p fancy-battery-mode)) "Update battery STATUS."
(let* ((charging? (string-equal "AC" (cdr (assoc ?L fancy-battery-last-status)))) (setq doom-modeline--battery-status
(percentage (cdr (assq ?p fancy-battery-last-status))) (let* ((status (or status fancy-battery-last-status))
(charging? (string-equal "AC" (cdr (assoc ?L status))))
(percentage (cdr (assq ?p status)))
(percentage-number (string-to-number percentage)) (percentage-number (string-to-number percentage))
(face (cond (face (cond
(charging? 'fancy-battery-charging) (charging? 'fancy-battery-charging)
@ -1989,13 +1991,13 @@ we don't want to remove that so we just return the original."
(if doom-modeline-icon (if doom-modeline-icon
(doom-modeline-icon-faicon "battery-empty" :v-adjust -0.0575) (doom-modeline-icon-faicon "battery-empty" :v-adjust -0.0575)
(propertize "!" 'face face))))) (propertize "!" 'face face)))))
(status (and percentage (concat percentage "%%"))) (percent-str (and percentage (concat percentage "%%")))
(help-echo (if battery-echo-area-format (help-echo (if battery-echo-area-format
(battery-format battery-echo-area-format fancy-battery-last-status) (battery-format battery-echo-area-format status)
"Battery status not available"))) "Battery status not available")))
(concat (concat
" " " "
(if status (if percent-str
(concat (concat
(propertize icon (propertize icon
'face (if doom-modeline-icon 'face (if doom-modeline-icon
@ -2008,7 +2010,7 @@ we don't want to remove that so we just return the original."
'help-echo help-echo) 'help-echo help-echo)
(if doom-modeline-icon (if doom-modeline-icon
(propertize doom-modeline-vspc 'help-echo help-echo)) (propertize doom-modeline-vspc 'help-echo help-echo))
(propertize status (propertize percent-str
'face face 'face face
'help-echo help-echo)) 'help-echo help-echo))
;; Battery status is not available ;; Battery status is not available
@ -2019,6 +2021,12 @@ we don't want to remove that so we just return the original."
:face 'error) :face 'error)
(propertize "N/A" 'face 'error))) (propertize "N/A" 'face 'error)))
" ")))) " "))))
(add-hook 'fancy-battery-status-update-functions #'doom-modeline-update-battery-status)
(doom-modeline-def-segment fancy-battery
(when (and (doom-modeline--active)
(bound-and-true-p fancy-battery-mode))
(or doom-modeline--battery-status (doom-modeline-update-battery-status))))
(defun doom-modeline-override-fancy-battery-modeline () (defun doom-modeline-override-fancy-battery-modeline ()
"Override `fancy-battery' mode-line." "Override `fancy-battery' mode-line."

View file

@ -4,7 +4,7 @@
;; Author: Vincent Zhang <seagle0128@gmail.com> ;; Author: Vincent Zhang <seagle0128@gmail.com>
;; Homepage: https://github.com/seagle0128/doom-modeline ;; Homepage: https://github.com/seagle0128/doom-modeline
;; Version: 1.9.4 ;; Version: 1.9.5
;; Package-Requires: ((emacs "25.1") (all-the-icons "1.0.0") (shrink-path "0.2.0") (eldoc-eval "0.1") (dash "2.11.0")) ;; Package-Requires: ((emacs "25.1") (all-the-icons "1.0.0") (shrink-path "0.2.0") (eldoc-eval "0.1") (dash "2.11.0"))
;; Keywords: faces mode-line ;; Keywords: faces mode-line