Add battery level to header

This commit is contained in:
Lucien Cartier-Tilet 2021-04-29 23:59:56 +02:00
parent 7054b08123
commit 1b7ed9e9c8
Signed by: phundrak
GPG Key ID: BD7789E705CB8DCA
3 changed files with 62 additions and 10 deletions

View File

@ -9,6 +9,9 @@ words, lets see how it looks like:
#+caption: Screenshot of the default Eshell information banner #+caption: Screenshot of the default Eshell information banner
[[file:img/screenshot.png]] [[file:img/screenshot.png]]
This will be displayed every time you open a new Eshell instance, or
if you invoke ~eshell-info-banner~ from it.
This package is geared towards Linux in particular, I am pretty sure This package is geared towards Linux in particular, I am pretty sure
it will not work on Windows, and there will probably be bugs on it will not work on Windows, and there will probably be bugs on
macOS. PR are welcome if you want to fix that! macOS. PR are welcome if you want to fix that!
@ -72,6 +75,18 @@ default your theme, but you can customize the following faces:
- ~eshell-info-banner-critical-face~ :: Used for filling the progress - ~eshell-info-banner-critical-face~ :: Used for filling the progress
bar when on critical levels bar when on critical levels
* My computer doesnt have a battery, will this still work?
As you can see, one line shows you your battery level. Il will start
to warn you in a reverse way compared to the other progress bars, as
it should for battery levels a fully charged battery at 100% is not
at a critical level, but at 0% it would be.
However, you might be on a desktop or any kind of computer that does
not have a battery, so… what do? Dont worry, /Emacs will automatically
detect whether you have a battery or not/ and will only display this
line if you have one. If you dont have a battery, the only difference
is you will have one less line than laptop users.
* License * License
~eshell-info-banner.el~ is available under the GNU GPL-3.0 license. You ~eshell-info-banner.el~ is available under the GNU GPL-3.0 license. You
can find the full text in [[file:LICENSE.md][LICENSE.md]]. can find the full text in [[file:LICENSE.md][LICENSE.md]].

View File

@ -187,22 +187,30 @@ the left side of the banner."
'eshell-info-banner-warning-face) 'eshell-info-banner-warning-face)
(t 'eshell-info-banner-normal-face)))) (t 'eshell-info-banner-normal-face))))
(defun eshell-info-banner--progress-bar (length percentage) (defun eshell-info-banner--progress-bar (length percentage &optional invert)
"Display a progress bar `LENGTH' long and `PERCENTAGE' full. "Display a progress bar `LENGTH' long and `PERCENTAGE' full.
The full path will be displayed filled with the character The full path will be displayed filled with the character
specified by `eshell-info-banner-progress-bar-char' up to specified by `eshell-info-banner-progress-bar-char' up to
`PERCENTAGE' percents. The rest will be empty." `PERCENTAGE' percents. The rest will be empty.
(let* ((length-filled (if (= 0 percentage)
0 If `INVERT' is t, then consider the percentage to approach
(/ (* length percentage) 100))) critical levels close to 0 rather than 100."
(length-empty (- length length-filled))) (message "Length: %s" length)
(let* ((length-filled (if (= 0 percentage)
0
(/ (* length percentage) 100)))
(length-empty (- length length-filled))
(percentage-level (if invert
(- 100 percentage)
percentage)))
(concat (concat
(eshell-info-banner--with-face "[" :weight 'bold) (eshell-info-banner--with-face "[" :weight 'bold)
(eshell-info-banner--with-face (s-repeat length-filled eshell-info-banner-progress-bar-char) (eshell-info-banner--with-face (s-repeat length-filled eshell-info-banner-progress-bar-char)
:weight 'bold :weight 'bold
:inherit (eshell-info-banner--get-color-percentage percentage)) :inherit (eshell-info-banner--get-color-percentage percentage-level))
(eshell-info-banner--with-face (s-repeat length-empty eshell-info-banner-progress-bar-char) (eshell-info-banner--with-face (s-repeat length-empty eshell-info-banner-progress-bar-char)
:weight 'bold :inherit 'eshell-info-banner-background-face) :weight 'bold
:inherit 'eshell-info-banner-background-face)
(eshell-info-banner--with-face "]" :weight 'bold)))) (eshell-info-banner--with-face "]" :weight 'bold))))
(defun eshell-info-banner--display-memory (type used total text-padding bar-length) (defun eshell-info-banner--display-memory (type used total text-padding bar-length)
@ -255,6 +263,35 @@ See also `eshell-info-banner--display-memory'."
(number-to-string percentage) (number-to-string percentage)
:inherit (eshell-info-banner--get-color-percentage percentage)))))) :inherit (eshell-info-banner--get-color-percentage percentage))))))
(defun eshell-info-banner--display-battery (text-padding bar-length)
"If the computer has a battery, display its level.
Pad the left text with dots by `TEXT-PADDING' characters.
`BAR-LENGTH' indicates the length in characters of the progress
bar.
The usage of `eshell-info-banner-warning-percentage' and
`eshell-info-banner-critical-percentage' is reversed, and can be
thought of as the percentage of discharge of the computer.
Thus, setting the warning at 75% will be translated as showing
the warning face with a battery level of 25% or less."
(let ((battery-level (battery)))
(if (string= battery-level "Battery status not available")
""
(let ((percentage (save-match-data
(string-match "\\([0-9]+\\)\\.[0-9]+%" battery-level)
(string-to-number (substring battery-level (match-beginning 1) (match-end 1))))))
(concat (s-pad-right text-padding "." "Battery")
": "
(eshell-info-banner--progress-bar bar-length
percentage
t)
(s-repeat 16 " ")
(format "(%3s%%)\n" (eshell-info-banner--with-face
(number-to-string percentage)
:inherit (eshell-info-banner--get-color-percentage (- 100.0 percentage)))))))))
; Public functions ;;;;;;;;;;;;;;;;;;;; ; Public functions ;;;;;;;;;;;;;;;;;;;;
;;;###autoload ;;;###autoload
@ -303,8 +340,8 @@ See also `eshell-info-banner--display-memory'."
(format "%s: %s Uptime.: %s\n" (format "%s: %s Uptime.: %s\n"
(s-pad-right left-padding "." "Hostname") (s-pad-right left-padding "." "Hostname")
(s-pad-right middle-padding " " (eshell-info-banner--with-face hostname :weight 'bold)) (s-pad-right middle-padding " " (eshell-info-banner--with-face hostname :weight 'bold))
uptime uptime)
) (eshell-info-banner--display-battery left-padding bar-length)
(eshell-info-banner--display-memory "Ram" (eshell-info-banner--display-memory "Ram"
(string-to-number (nth 2 ram)) (string-to-number (nth 2 ram))
(string-to-number (nth 1 ram)) (string-to-number (nth 1 ram))

Binary file not shown.

Before

Width:  |  Height:  |  Size: 35 KiB

After

Width:  |  Height:  |  Size: 38 KiB