diff --git a/elisp/chronometrist.el b/elisp/chronometrist.el index 0d3c97c..b2356e0 100644 --- a/elisp/chronometrist.el +++ b/elisp/chronometrist.el @@ -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." diff --git a/elisp/chronometrist.org b/elisp/chronometrist.org index 2fdedc6..9ebbb66 100644 --- a/elisp/chronometrist.org +++ b/elisp/chronometrist.org @@ -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