[feat] make durations in details view customizable

This commit is contained in:
contrapunctus 2021-08-08 01:29:30 +05:30
parent 6648b146f9
commit dd46ffe8ac
2 changed files with 10 additions and 58 deletions

View File

@ -82,8 +82,10 @@ file.")
(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 "%m:%s" ,(make-string 3 ?\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 " "-"))
(details "%h:%.2m:%z%.2s"))
"List specifying duration formats.
Each element must be in the form
@ -105,7 +107,7 @@ the same."
(-let [(format-string blank-string)
(alist-get (or field 'chronometrist)
chronometrist-duration-formats)]
(if (zerop seconds)
(if (and (zerop seconds) blank-string)
blank-string
(format-seconds format-string seconds))))
@ -949,23 +951,6 @@ EVENT should be a plist (see `chronometrist-file')."
(time-subtract (parse-iso8601-time-string stop)
(parse-iso8601-time-string start))))
(defun chronometrist-format-duration-long (seconds)
"Return SECONDS as a human-friendly duration string.
e.g. \"2 hours, 10 minutes\". SECONDS must be an integer. If
SECONDS is less than 60, return a blank string."
(let* ((hours (/ seconds 60 60))
(minutes (% (/ seconds 60) 60))
(hour-string (if (= 1 hours) "hour" "hours"))
(minute-string (if (= 1 minutes) "minute" "minutes")))
(cond ((and (zerop hours) (zerop minutes)) "")
((zerop hours)
(format "%s %s" minutes minute-string))
((zerop minutes)
(format "%s %s" hours hour-string))
(t (format "%s %s, %s %s"
hours hour-string
minutes minute-string)))))
(defcustom chronometrist-update-interval 5
"How often the `chronometrist' buffer should be updated, in seconds.
@ -2084,7 +2069,7 @@ Return value is a list as specified by `tabulated-list-entries'."
(ts-now))))
(interval (floor (ts-diff stop start)))
(index-string (format "%s" index))
(duration (chronometrist-format-duration-long interval))
(duration (chronometrist-format-duration interval 'details))
(timespan (format "from %s to %s"
(ts-format chronometrist-details-time-format-string
start)

View File

@ -431,7 +431,8 @@ file.")
`((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 " "-")))
(report-total "%h:%.2m:%z%.2s" ,(format "% 5s " "-"))
(details "%h:%.2m:%z%.2s"))
"List specifying duration formats.
Each element must be in the form
@ -456,7 +457,7 @@ the same."
(-let [(format-string blank-string)
(alist-get (or field 'chronometrist)
chronometrist-duration-formats)]
(if (zerop seconds)
(if (and (zerop seconds) blank-string)
blank-string
(format-seconds format-string seconds))))
#+END_SRC
@ -1901,40 +1902,6 @@ EVENT should be a plist (see `chronometrist-file')."
(time-subtract (parse-iso8601-time-string stop)
(parse-iso8601-time-string start))))
#+END_SRC
*** format-duration-long :function:
#+BEGIN_SRC emacs-lisp
(defun chronometrist-format-duration-long (seconds)
"Return SECONDS as a human-friendly duration string.
e.g. \"2 hours, 10 minutes\". SECONDS must be an integer. If
SECONDS is less than 60, return a blank string."
(let* ((hours (/ seconds 60 60))
(minutes (% (/ seconds 60) 60))
(hour-string (if (= 1 hours) "hour" "hours"))
(minute-string (if (= 1 minutes) "minute" "minutes")))
(cond ((and (zerop hours) (zerop minutes)) "")
((zerop hours)
(format "%s %s" minutes minute-string))
((zerop minutes)
(format "%s %s" hours hour-string))
(t (format "%s %s, %s %s"
hours hour-string
minutes minute-string)))))
#+END_SRC
**** tests
#+BEGIN_SRC emacs-lisp :tangle ../tests/chronometrist-tests.el :load test
(ert-deftest chronometrist-format-duration-long ()
(should (equal (chronometrist-format-duration-long 5) ""))
(should (equal (chronometrist-format-duration-long 65) "1 minute"))
(should (equal (chronometrist-format-duration-long 125) "2 minutes"))
(should (equal (chronometrist-format-duration-long 3605) "1 hour"))
(should (equal (chronometrist-format-duration-long 3660) "1 hour, 1 minute"))
(should (equal (chronometrist-format-duration-long 3725) "1 hour, 2 minutes"))
(should (equal (chronometrist-format-duration-long 7200) "2 hours"))
(should (equal (chronometrist-format-duration-long 7260) "2 hours, 1 minute"))
(should (equal (chronometrist-format-duration-long 7320) "2 hours, 2 minutes")))
#+END_SRC
** Timer
Instead of the Emacs convention of pressing ~g~ to update, we keep buffers updated with a timer.
@ -3320,7 +3287,7 @@ Return value is a list as specified by `tabulated-list-entries'."
(ts-now))))
(interval (floor (ts-diff stop start)))
(index-string (format "%s" index))
(duration (chronometrist-format-duration-long interval))
(duration (chronometrist-format-duration interval 'details))
(timespan (format "from %s to %s"
(ts-format chronometrist-details-time-format-string
start)