[feat] resurrect -format-duration as -format-duration-default

add support for procedure values to -duration-formats and -format-duration
This commit is contained in:
contrapunctus 2021-08-11 16:03:41 +05:30
parent dd46ffe8ac
commit 6f183f83d7
2 changed files with 68 additions and 18 deletions

View File

@ -81,10 +81,14 @@ file.")
(chronometrist-sexp-current-task))
(defcustom chronometrist-duration-formats
`((chronometrist "%h:%.2m:%z%.2s" ,(concat (make-string 7 ?\s) "-"))
(chronometrist-total "%h:%.2m:%z%.2s" ,(concat (make-string 6 ?\s) "-"))
(report "%h:%.2m:%z%.2s" ,(format "% 5s " "-"))
(report-total "%h:%.2m:%z%.2s" ,(format "% 5s " "-"))
`((chronometrist chronometrist-format-duration-default
,(concat (make-string 7 ?\s) "-"))
(chronometrist-total chronometrist-format-duration-default
,(concat (make-string 6 ?\s) "-"))
(report chronometrist-format-duration-default
,(concat (make-string 5 ?\s) "-"))
(report-total chronometrist-format-duration-default
,(format "% 5s " "-"))
(details "%h:%.2m:%z%.2s"))
"List specifying duration formats.
Each element must be in the form
@ -93,9 +97,12 @@ Each element must be in the form
FIELD should be a symbol unique to this list.
FORMAT-STRING should be a string acceptable to `format-seconds'.
FORMAT-STRING should be a string acceptable to `format-seconds',
or a procedure of two arguments (SECONDS and BLANK-STRING)
returning such a string.
If the given duration is zero, BLANK-STRING is used instead.")
BLANK-STRING should be a string to be used if the given duration
is zero.")
(defun chronometrist-format-duration (seconds &optional field)
"Format SECONDS as a duration string.
@ -107,9 +114,26 @@ the same."
(-let [(format-string blank-string)
(alist-get (or field 'chronometrist)
chronometrist-duration-formats)]
(if (and (zerop seconds) blank-string)
blank-string
(format-seconds format-string seconds))))
(cond ((and (zerop seconds) blank-string)
blank-string)
((stringp format-string)
(format-seconds format-string seconds))
((functionp format-string)
(funcall format-string seconds)))))
(defun chronometrist-format-duration-default (seconds)
(-let* (((h m s) (chronometrist-seconds-to-hms seconds))
(blank (make-string 3 ?\s)))
(let ((h (if (zerop h) blank (format "%2d:" h)))
(m (cond ((and (zerop h) (zerop m))
blank)
((zerop h)
(format "%2d:" m))
(t (format "%02d:" m))))
(s (if (and (zerop h) (zerop m))
(format "%2d" s)
(format "%02d" s))))
(concat h m s))))
(defun chronometrist-common-file-empty-p (file)
"Return t if FILE is empty."

View File

@ -428,10 +428,14 @@ file.")
*** duration-formats :function:
#+BEGIN_SRC emacs-lisp
(defcustom chronometrist-duration-formats
`((chronometrist "%h:%.2m:%z%.2s" ,(concat (make-string 7 ?\s) "-"))
(chronometrist-total "%h:%.2m:%z%.2s" ,(concat (make-string 6 ?\s) "-"))
(report "%h:%.2m:%z%.2s" ,(format "% 5s " "-"))
(report-total "%h:%.2m:%z%.2s" ,(format "% 5s " "-"))
`((chronometrist chronometrist-format-duration-default
,(concat (make-string 7 ?\s) "-"))
(chronometrist-total chronometrist-format-duration-default
,(concat (make-string 6 ?\s) "-"))
(report chronometrist-format-duration-default
,(concat (make-string 5 ?\s) "-"))
(report-total chronometrist-format-duration-default
,(format "% 5s " "-"))
(details "%h:%.2m:%z%.2s"))
"List specifying duration formats.
Each element must be in the form
@ -440,9 +444,12 @@ Each element must be in the form
FIELD should be a symbol unique to this list.
FORMAT-STRING should be a string acceptable to `format-seconds'.
FORMAT-STRING should be a string acceptable to `format-seconds',
or a procedure of two arguments (SECONDS and BLANK-STRING)
returning such a string.
If the given duration is zero, BLANK-STRING is used instead.")
BLANK-STRING should be a string to be used if the given duration
is zero.")
#+END_SRC
*** format-duration :function:
@ -457,9 +464,28 @@ the same."
(-let [(format-string blank-string)
(alist-get (or field 'chronometrist)
chronometrist-duration-formats)]
(if (and (zerop seconds) blank-string)
blank-string
(format-seconds format-string seconds))))
(cond ((and (zerop seconds) blank-string)
blank-string)
((stringp format-string)
(format-seconds format-string seconds))
((functionp format-string)
(funcall format-string seconds)))))
#+END_SRC
*** format-duration-default :function:
#+BEGIN_SRC emacs-lisp
(defun chronometrist-format-duration-default (seconds)
(-let* (((h m s) (chronometrist-seconds-to-hms seconds))
(blank (make-string 3 ?\s)))
(let ((h (if (zerop h) blank (format "%2d:" h)))
(m (cond ((and (zerop h) (zerop m))
blank)
((zerop h)
(format "%2d:" m))
(t (format "%02d:" m))))
(s (if (and (zerop h) (zerop m))
(format "%2d" s)
(format "%02d" s))))
(concat h m s))))
#+END_SRC
*** file-empty-p :reader:
#+BEGIN_SRC emacs-lisp