Add new partitions detection method through duf

eshell-info-banner will now default to duf if it is installed and
avaliable on the system, otherwise it will default to
platform-specific implementation.
This commit is contained in:
Lucien Cartier-Tilet 2021-06-18 18:26:24 +02:00
parent 6a043f6c19
commit 1635241762
Signed by: phundrak
GPG Key ID: BD7789E705CB8DCA
2 changed files with 98 additions and 35 deletions

View File

@ -87,7 +87,7 @@ A couple of variables can be edited by the user in order to configure
- ~eshell-info-banner-critical-percentage~ :: Percentage from which the - ~eshell-info-banner-critical-percentage~ :: Percentage from which the
level should be displayed as critical. level should be displayed as critical.
Default value ~90~ Default value: ~90~
- ~eshell-info-banner-tramp-aware~ :: When using Eshell through TRAMP, - ~eshell-info-banner-tramp-aware~ :: When using Eshell through TRAMP,
you can decide whether ~eshell-info-banner~ will display information you can decide whether ~eshell-info-banner~ will display information
about the remote system you are connected to or only display about the remote system you are connected to or only display
@ -95,7 +95,22 @@ A couple of variables can be edited by the user in order to configure
~eshell-info-banner-tramp-aware~ to ~t~ to display information on the ~eshell-info-banner-tramp-aware~ to ~t~ to display information on the
remote system or to ~nil~ to keep local information only. remote system or to ~nil~ to keep local information only.
Default value ~t~ Default value: ~t~
- ~eshell-info-banner-use-duf~ :: Whether or not to use [[https://github.com/muesli/duf][duf]]. ~duf~ is a
better replacement for ~df~ and should be more platform-agnostic than
the latter. I also suspect this implementation will be faster than
the one with ~df~, since there is very few string manipulation with
~duf~ compared to the implementations with ~df~. However, due to
~shell-command-to-string~, Windows users might want to turn it off by
default.
Default value: ~t~ if ~duf~ is found on the system, ~nil~ otherwise
- ~eshell-info-banner-duf-executable~ :: Path to your ~duf~ executable. If
~duf~ is not found by default by Emacs, you can override
~eshell-info-banner-use-duf~ with ~t~ and specify the path to ~duf~ with
this custom variable.
Default value: ~duf~
Dont like the colors used by the package? They should follow by Dont like the colors used by the package? They should follow by
default your theme, but you can customize the following faces: default your theme, but you can customize the following faces:

View File

@ -2,7 +2,7 @@
;; Author: Lucien Cartier-Tilet <lucien@phundrak.com> ;; Author: Lucien Cartier-Tilet <lucien@phundrak.com>
;; Maintainer: Lucien Cartier-Tilet <lucien@phundrak.com> ;; Maintainer: Lucien Cartier-Tilet <lucien@phundrak.com>
;; Version: 0.4.4 ;; Version: 0.5.0
;; Package-Requires: ((emacs "25") (dash "2") (f "0.20") (s "1")) ;; Package-Requires: ((emacs "25") (dash "2") (f "0.20") (s "1"))
;; Homepage: https://labs.phundrak.com/phundrak/eshell-info-banner.el ;; Homepage: https://labs.phundrak.com/phundrak/eshell-info-banner.el
@ -130,6 +130,22 @@
:type 'list :type 'list
:version "0.3.0") :version "0.3.0")
(defcustom eshell-info-banner-duf-executable "duf"
"Path to the `duf' executable."
:group 'eshell-info-banner
:type 'string
:safe #'stringp
:version "0.5.0")
(defcustom eshell-info-banner-use-duf (if (executable-find eshell-info-banner-duf-executable)
t
nil)
"If non-nil, use `duf' instead of `df'."
:group 'eshell-info-banner
:type 'boolean
:safe #'booleanp
:version "0.5.0")
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Faces ; ; Faces ;
@ -235,6 +251,36 @@ neither of these, an error will be thrown by the function."
(eshell-info-banner--abbr-path (cdr path)))) (eshell-info-banner--abbr-path (cdr path))))
(t (error "Invalid argument %s, neither stringp or listp" path)))) (t (error "Invalid argument %s, neither stringp or listp" path))))
(defun eshell-info-banner--get-mounted-partitions/duf ()
"Detect mounted partitions on systems supporting `duf'.
Return detected partitions as a list of structs. See
`eshell-info-banner-partition-prefixes' to see how partitions are
chosen. Relies on the `duf' command.
FIXME: filter first partitions, then get other information."
(let* ((partitions (json-read-from-string (shell-command-to-string (concat eshell-info-banner-duf-executable
" -json"))))
(partitions (cl-remove-if-not (lambda (partition)
(let ((device (format "%s" (cdr (assoc 'device partition)))))
(seq-some (lambda (prefix)
(string-prefix-p prefix device t))
eshell-info-banner-partition-prefixes)))
(seq-into-sequence partitions))))
(mapcar (lambda (partition)
(let* ((mount-point (format "%s" (cdr (assoc 'mount_point partition))))
(total (cdr (assoc 'total partition)))
(used (cdr (assoc 'used partition)))
(percent (/ (* 100 used) total)))
(make-eshell-info-banner--mounted-partitions
:path (if (> (length mount-point) eshell-info-banner-shorten-path-from)
(eshell-info-banner--abbr-path mount-point t)
mount-point)
:size (file-size-human-readable total)
:used (file-size-human-readable used)
:percent percent)))
partitions)))
(defun eshell-info-banner--get-mounted-partitions/gnu () (defun eshell-info-banner--get-mounted-partitions/gnu ()
"Detect mounted partitions on a Linux system. "Detect mounted partitions on a Linux system.
@ -303,6 +349,8 @@ chosen. Relies on the `df' command."
"Detect mounted partitions on the system. "Detect mounted partitions on the system.
Return detected partitions as a list of structs." Return detected partitions as a list of structs."
(if eshell-info-banner-use-duf
(eshell-info-banner--get-mounted-partitions/duf)
(pcase system-type (pcase system-type
((or 'gnu 'gnu/linux 'gnu/kfreebsd) ((or 'gnu 'gnu/linux 'gnu/kfreebsd)
(eshell-info-banner--get-mounted-partitions/gnu)) (eshell-info-banner--get-mounted-partitions/gnu))
@ -313,7 +361,7 @@ Return detected partitions as a list of structs."
(other (other
(progn (progn
(message "Partition detection for %s not yet supported." other) (message "Partition detection for %s not yet supported." other)
nil)))) nil)))))
(defun eshell-info-banner--partition-to-string (partition text-padding bar-length) (defun eshell-info-banner--partition-to-string (partition text-padding bar-length)
"Display a progress bar showing how full a `PARTITION' is. "Display a progress bar showing how full a `PARTITION' is.
@ -328,7 +376,7 @@ For TEXT-PADDING and BAR-LENGTH, see the documentation of
:weight 'bold)) :weight 'bold))
": " ": "
(eshell-info-banner--progress-bar bar-length percentage) (eshell-info-banner--progress-bar bar-length percentage)
(format " %6s / %-5s (%3s%%)" (format " %6s / %-6s (%3s%%)"
(eshell-info-banner--mounted-partitions-used partition) (eshell-info-banner--mounted-partitions-used partition)
(eshell-info-banner--mounted-partitions-size partition) (eshell-info-banner--mounted-partitions-size partition)
(eshell-info-banner--with-face (eshell-info-banner--with-face
@ -436,7 +484,7 @@ displayed."
(concat (s-pad-right text-padding "." type) (concat (s-pad-right text-padding "." type)
": " ": "
(eshell-info-banner--progress-bar bar-length percentage) (eshell-info-banner--progress-bar bar-length percentage)
(format " %6s / %-5s (%3s%%)" (format " %6s / %-6s (%3s%%)"
(file-size-human-readable used) (file-size-human-readable used)
(file-size-human-readable total) (file-size-human-readable total)
(eshell-info-banner--with-face (eshell-info-banner--with-face
@ -532,7 +580,7 @@ the warning face with a battery level of 25% or less."
(eshell-info-banner--progress-bar bar-length (eshell-info-banner--progress-bar bar-length
percentage percentage
t) t)
(s-repeat 16 " ") (s-repeat 17 " ")
(format "(%3s%%)\n" (format "(%3s%%)\n"
(eshell-info-banner--with-face (eshell-info-banner--with-face
(number-to-string percentage) (number-to-string percentage)
@ -667,7 +715,7 @@ build number)."
eshell-info-banner-width)) eshell-info-banner-width))
(middle-padding (- tot-width right-text left-padding 4)) (middle-padding (- tot-width right-text left-padding 4))
(bar-length (- tot-width left-padding 4 22))) (bar-length (- tot-width left-padding 4 23)))
(concat (format "%s\n" (s-repeat tot-width eshell-info-banner-progress-bar-char)) (concat (format "%s\n" (s-repeat tot-width eshell-info-banner-progress-bar-char))
(format "%s: %s Kernel.: %s\n" (format "%s: %s Kernel.: %s\n"
(s-pad-right left-padding (s-pad-right left-padding