[Feature] Use built-in battery instead of unmaintained fancy-battery.

See 9b88ae77a0.
This commit is contained in:
Vincent Zhang 2019-10-25 01:09:30 +08:00
parent 22b14c9b8e
commit 93135d4220
3 changed files with 102 additions and 99 deletions

View file

@ -41,9 +41,10 @@ The `doom-modeline` was designed for minimalism, and offers:
- A window number segment for `ace-window`, `winum` and `window-numbering`
- An indicator for modal editing state, including `evil`, `overwrite`, `god`, `ryo` and
`xah-fly-keys`, etc.
- An indicator for remote host
- An indicator for debug state
- An indicator for battery status
- An indicator for current input method
- An indicator for debug state
- An indicator for remote host
- An indicator for LSP state with `lsp-mode` or `eglot`
- An indicator for GitHub notifications
- An indicator for unread emails with `mu4e-alert`
@ -52,7 +53,6 @@ The `doom-modeline` was designed for minimalism, and offers:
- An indicator for party parrot
- An indicator for PDF page number with `pdf-tools`
- An indicator for markdown/org preview with `grip`
- An indicator for battery status with `fancy-battery`
- Truncated file name, file icon, buffer state and project name in buffer information segment, which
is compatible with `project`, `find-file-in-project` and `projectile`.
- New mode-line for `Info-mode` buffers

View file

@ -50,6 +50,8 @@
(defvar anzu-cons-mode-line-p)
(defvar aw-keys)
(defvar battery-echo-area-format)
(defvar battery-load-critical)
(defvar battery-status-function)
(defvar edebug-execution-mode)
(defvar evil-ex-active-highlights-alist)
(defvar evil-ex-argument)
@ -59,7 +61,6 @@
(defvar evil-visual-beginning)
(defvar evil-visual-end)
(defvar evil-visual-selection)
(defvar fancy-battery-last-status)
(defvar flycheck-current-errors)
(defvar flycheck-mode-menu-map)
(defvar flymake--backend-state)
@ -2052,89 +2053,84 @@ we don't want to remove that so we just return the original."
;;
;; fancy battery
;; battery status
;;
(defvar-local doom-modeline--battery-status nil)
(defun doom-modeline-update-battery-status (&optional status)
"Update battery STATUS."
(defun doom-modeline-update-battery-status ()
"Update battery status."
(setq doom-modeline--battery-status
(when-let*
((status (or status (bound-and-true-p fancy-battery-last-status)))
(charging? (string-equal "AC" (cdr (assoc ?L status))))
(percentage (cdr (assq ?p status)))
(percentage-number (string-to-number percentage))
(face (cond
(charging? 'fancy-battery-charging)
((< percentage-number 10) 'fancy-battery-critical)
((< percentage-number 25) 'fancy-battery-discharging)
((< percentage-number 95) 'mode-line)
(t 'fancy-battery-charging)))
(icon (cond
(charging?
(doom-modeline-icon 'alltheicon
"battery-charging"
"🔋"
"+"
face
:height 1.4
:v-adjust -0.1))
((> percentage-number 95)
(doom-modeline-icon 'faicon
"battery-full"
"🔋"
"-"
face
:v-adjust -0.0575))
((> percentage-number 70)
(doom-modeline-icon 'faicon
"battery-three-quarters"
"🔋"
"-"
face
:v-adjust -0.0575))
((> percentage-number 40)
(doom-modeline-icon 'faicon
"battery-half"
"🔋"
"-"
face
:v-adjust -0.0575))
((> percentage-number 15)
(doom-modeline-icon 'faicon
"battery-quarter"
"🔋"
"-"
face
:v-adjust -0.0575))
(t
(doom-modeline-icon 'faicon
"battery-empty"
"🔋"
"!"
face
:v-adjust -0.0575))))
(percent-str (and percentage (concat percentage "%%")))
(help-echo (if battery-echo-area-format
(battery-format battery-echo-area-format status)
"Battery status not available")))
(concat
(doom-modeline-spc)
(if percent-str
(propertize (concat icon
(doom-modeline-vspc)
(propertize percent-str 'face face))
'help-echo help-echo)
;; Battery status is not available
(propertize (doom-modeline-icon 'faicon
"battery-empty"
""
"N/A"
'error
:v-adjust -0.0575)
'help-echo "Battery status not available"))
(doom-modeline-spc)))))
(add-hook 'fancy-battery-status-update-functions #'doom-modeline-update-battery-status)
(let* ((data (and (bound-and-true-p battery-status-function)
(funcall battery-status-function)))
(charging? (string-equal "AC" (cdr (assoc ?L data))))
(percentage (car (read-from-string (cdr (assq ?p data)))))
(face (when (numberp percentage)
(cond (charging? 'success)
((< percentage battery-load-critical) 'error)
((< percentage 25) 'warning)
((< percentage 95) 'mode-line)
(t 'success))))
(icon (if (numberp percentage)
(cond (charging?
(doom-modeline-icon 'alltheicon
"battery-charging"
"🔋"
"+"
face
:height 1.4
:v-adjust -0.1))
((> percentage 95)
(doom-modeline-icon 'faicon
"battery-full"
"🔋"
"-"
face
:v-adjust -0.0575))
((> percentage 70)
(doom-modeline-icon 'faicon
"battery-three-quarters"
"🔋"
"-"
face
:v-adjust -0.0575))
((> percentage 40)
(doom-modeline-icon 'faicon
"battery-half"
"🔋"
"-"
face
:v-adjust -0.0575))
((> percentage battery-load-critical)
(doom-modeline-icon 'faicon
"battery-quarter"
"🔋"
"-"
face
:v-adjust -0.0575))
(t (doom-modeline-icon 'faicon
"battery-empty"
"🔋"
"!"
face
:v-adjust -0.0575)))
(doom-modeline-icon 'faicon
"battery-empty"
""
"N/A"
'error
:v-adjust -0.0575)))
(text (if (numberp percentage)
(format "%d%%%%" percentage)
""))
(help-echo (if (and battery-echo-area-format (numberp percentage))
(battery-format battery-echo-area-format data)
"Battery status not available")))
(concat (doom-modeline-spc)
(propertize (concat icon
(doom-modeline-vspc)
(propertize text 'face face))
'help-echo help-echo)
(doom-modeline-spc)))))
(when (>= emacs-major-version 26)
(add-variable-watcher
@ -2155,19 +2151,24 @@ we don't want to remove that so we just return the original."
(with-current-buffer buf
(doom-modeline-update-battery-status)))))))
(doom-modeline-def-segment fancy-battery
(doom-modeline-def-segment battery
"Display battery status."
(when (and (doom-modeline--active)
(bound-and-true-p fancy-battery-mode))
(bound-and-true-p display-battery-mode))
(or doom-modeline--battery-status (doom-modeline-update-battery-status))))
(defun doom-modeline-override-fancy-battery-modeline ()
"Override `fancy-battery' mode-line."
(defun doom-modeline-override-battery-modeline ()
"Override default battery mode-line."
(if (bound-and-true-p doom-modeline-mode)
(setq global-mode-string
(delq 'fancy-battery-mode-line global-mode-string))))
(add-hook 'fancy-battery-mode-hook #'doom-modeline-override-fancy-battery-modeline)
(add-hook 'doom-modeline-mode-hook #'doom-modeline-override-fancy-battery-modeline)
(progn
(advice-add #'battery-update :override #'doom-modeline-update-battery-status)
(setq global-mode-string
(delq 'battery-mode-line-string global-mode-string)))
(progn
(advice-remove #'battery-update #'doom-modeline-update-battery-status)
(when display-battery-mode (display-battery-mode 1)))))
(add-hook 'display-battery-mode-hook #'doom-modeline-override-battery-modeline)
(add-hook 'doom-modeline-mode-hook #'doom-modeline-override-battery-modeline)
;;

View file

@ -47,9 +47,10 @@
;; - A window number segment for winum and window-numbering
;; - An indicator for modal editing state, including evil, overwrite, god, ryo
;; and xah-fly-keys, etc.
;; - An indicator for remote host
;; - An indicator for battery status
;; - An indicator for current input method
;; - An indicator for debug state
;; - An indicator for remote host
;; - An indicator for LSP state with lsp-mode or eglot
;; - An indicator for github notifications
;; - An indicator for unread emails with mu4e-alert
@ -58,7 +59,6 @@
;; - An indicator for party parrot
;; - An indicator for PDF page number with pdf-tools
;; - An indicator for markdown/org preivews with grip
;; - An indicator for battery status with fancy-battery
;; - Truncated file name, file icon, buffer state and project name in buffer
;; information segment, which is compatible with project, find-file-in-project
;; and projectile
@ -90,7 +90,7 @@
(doom-modeline-def-modeline 'main
'(bar workspace-name window-number modals matches buffer-info remote-host buffer-position parrot selection-info)
'(objed-state misc-info persp-name fancy-battery grip irc mu4e github debug lsp minor-modes input-method indent-info buffer-encoding major-mode process vcs checker))
'(objed-state misc-info persp-name battery grip irc mu4e github debug lsp minor-modes input-method indent-info buffer-encoding major-mode process vcs checker))
(doom-modeline-def-modeline 'minimal
'(bar matches buffer-info-simple)
@ -98,11 +98,11 @@
(doom-modeline-def-modeline 'special
'(bar window-number modals matches buffer-info buffer-position parrot selection-info)
'(objed-state misc-info fancy-battery irc-buffers debug lsp minor-modes input-method indent-info buffer-encoding major-mode process checker))
'(objed-state misc-info battery irc-buffers debug lsp minor-modes input-method indent-info buffer-encoding major-mode process checker))
(doom-modeline-def-modeline 'project
'(bar window-number buffer-default-directory)
'(misc-info fancy-battery mu4e github debug major-mode process))
'(misc-info battery mu4e github debug major-mode process))
(doom-modeline-def-modeline 'package
'(bar window-number package)
@ -126,7 +126,7 @@
(doom-modeline-def-modeline 'timemachine
'(bar window-number matches git-timemachine buffer-position parrot selection-info)
'(misc-info fancy-battery mu4e github debug minor-modes indent-info buffer-encoding major-mode))
'(misc-info battery mu4e github debug minor-modes indent-info buffer-encoding major-mode))
;;
;; Interfaces
@ -200,7 +200,9 @@ If DEFAULT is non-nil, set the default mode-line for all buffers."
(defvar doom-modeline-mode-map (make-sparse-keymap))
(declare-function helm-display-mode-line 'helm) ; suppress warnings
;; suppress warnings
(declare-function battery-update 'battery)
(declare-function helm-display-mode-line 'helm)
;;;###autoload
(define-minor-mode doom-modeline-mode
@ -227,7 +229,7 @@ If DEFAULT is non-nil, set the default mode-line for all buffers."
(add-hook 'pdf-view-mode-hook #'doom-modeline-set-pdf-modeline)
(add-hook 'git-timemachine-mode-hook #'doom-modeline-set-timemachine-modeline)
(add-hook 'paradox-menu-mode-hook #'doom-modeline-set-package-modeline)
;; Add advice
;; Add advices
(advice-add #'helm-display-mode-line :override #'doom-modeline-set-helm-modeline))
(progn
;; Restore mode-line