From 69a7dfd1fcaf25f440ae918ebd978676e93f3e6a Mon Sep 17 00:00:00 2001 From: contrapunctus Date: Mon, 2 Aug 2021 12:14:31 +0530 Subject: [PATCH] [feat] (WIP) use format-seconds; make duration formats customizable --- elisp/chronometrist.el | 5 ++++- elisp/chronometrist.org | 42 +++++++++++++++++++++++++++-------------- 2 files changed, 32 insertions(+), 15 deletions(-) diff --git a/elisp/chronometrist.el b/elisp/chronometrist.el index 6e6784d..d2019c0 100644 --- a/elisp/chronometrist.el +++ b/elisp/chronometrist.el @@ -1072,7 +1072,10 @@ Return the value returned by Fₙ." arg)) (defcustom chronometrist-schema - '[("#" 3 t) ("Task" 25 t) ("Time" 10 t) ("Active" 10 t)] + '[("#" 3 t) + ("Task" 25 t) + ("Time" 10 t :right-align t) + ("Active" 10 t :right-align t)] "Vector specifying schema of `chronometrist' buffer. See `tabulated-list-format'." :type '(vector)) diff --git a/elisp/chronometrist.org b/elisp/chronometrist.org index 242098d..e76857a 100644 --- a/elisp/chronometrist.org +++ b/elisp/chronometrist.org @@ -425,25 +425,36 @@ file.") "Return the name of the currently clocked-in task, or nil if not clocked in." (chronometrist-sexp-current-task)) #+END_SRC -*** format-time :function: +*** duration-formats :function: #+BEGIN_SRC emacs-lisp -(cl-defun chronometrist-format-duration (seconds &optional (blank (make-string 3 ?\s))) +(defcustom chronometrist-duration-formats + `((chronometrist "%h:%.2m:%z%.2s" ,(concat (make-string 7 ?\s) "-")) + (report "%m:%s" ,(make-string 3 ?\s))) + "List specifying duration formats. +Each element must be in the form + +\(FIELD FORMAT-STRING [BLANK-STRING]) + +FIELD should be a symbol unique to this list. + +FORMAT-STRING should be a string acceptable to `format-seconds'. + +If the given duration is zero, BLANK-STRING is used instead.") + +#+END_SRC +*** format-duration :function: +#+BEGIN_SRC emacs-lisp +(defun chronometrist-format-duration (seconds) "Format SECONDS as a string suitable for display in Chronometrist buffers. SECONDS must be a positive integer. BLANK is a string to display in place of blank values. If not supplied, 3 spaces are used." - (-let [(h m s) (chronometrist-seconds-to-hms seconds)] - (if (and (zerop h) (zerop m) (zerop s)) - (concat (make-string 7 ?\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))))) + (-let [(format-string blank-string) + (alist-get 'chronometrist chronometrist-duration-formats)] + (if (zerop seconds) + blank-string + (format-seconds format-string seconds)))) #+END_SRC *** file-empty-p :reader: #+BEGIN_SRC emacs-lisp @@ -2097,7 +2108,10 @@ Return the value returned by Fₙ." #+BEGIN_SRC emacs-lisp (defcustom chronometrist-schema - '[("#" 3 t) ("Task" 25 t) ("Time" 10 t) ("Active" 10 t)] + '[("#" 3 t) + ("Task" 25 t) + ("Time" 10 t :right-align t) + ("Active" 10 t :right-align t)] "Vector specifying schema of `chronometrist' buffer. See `tabulated-list-format'." :type '(vector))