Personal finances application for Emacs
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

75 lines
2.4 KiB

  1. ;;; elbank-progressbar.el --- Progressbar widget used by elbank -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2017-2018 Nicolas Petton
  3. ;; Author: Nicolas Petton <nicolas@petton.fr>
  4. ;; This program is free software; you can redistribute it and/or modify
  5. ;; it under the terms of the GNU General Public License as published by
  6. ;; the Free Software Foundation, either version 3 of the License, or
  7. ;; (at your option) any later version.
  8. ;; This program is distributed in the hope that it will be useful,
  9. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. ;; GNU General Public License for more details.
  12. ;; You should have received a copy of the GNU General Public License
  13. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. ;;; Commentary:
  15. ;;
  16. ;;; Code:
  17. (require 'seq)
  18. (require 'elbank-common)
  19. (defgroup elbank-progressbar nil
  20. "Elbank progressbar."
  21. :group 'elbank)
  22. (defface elbank-progressbar-box-face '((t . (:box 1)))
  23. "Face used to represent the progressbar."
  24. :group 'elbank-progressbar)
  25. (defface elbank-progressbar-fill-face '((t . (:background "dark green" :weight bold)))
  26. "Face used to fill the progressbar."
  27. :group 'elbank-progressbar)
  28. (defface elbank-progressbar-overflow-face '((t . (:background "firebrick" :weight bold)))
  29. "Face used as a warning for overflowing progressbars."
  30. :group 'elbank-progressbar)
  31. (defun elbank-insert-progressbar (percent &optional width)
  32. "Insert a progressbar at point with PERCENT filled.
  33. The of the progressbar is set by WIDTH and default to 50.
  34. If PERCENT is greater than 100, fill the progressbar in red."
  35. (let* ((width (or width 50))
  36. (filled-width (min width (* width (/ percent 100.0))))
  37. (label (format "%s%%" percent))
  38. (label-padding (/ (- filled-width (seq-length label)) 2.0))
  39. (beg (point))
  40. (col (current-column)))
  41. (dotimes (_ label-padding)
  42. (insert " "))
  43. (insert label)
  44. (dotimes (_ (min label-padding
  45. (- width (- (current-column) col))))
  46. (insert " "))
  47. (when (not (zerop percent))
  48. (put-text-property beg (point)
  49. 'face (if (> percent 100)
  50. 'elbank-progressbar-overflow-face
  51. 'elbank-progressbar-fill-face)))
  52. (dotimes (_ (- width (- (current-column) col)))
  53. (insert " "))
  54. (let ((box (make-overlay beg (point))))
  55. (overlay-put box 'face 'elbank-progressbar-box-face))))
  56. (provide 'elbank-progressbar)
  57. ;;; elbank-progressbar.el ends here