From 26fabb22edfea51e8a686c179ab91d00a2ff0bc3 Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Tue, 5 May 2026 12:30:42 -0500 Subject: refactor!: collapse three time-left format defcustoms into one alist I merged `chime-time-left-format-at-event', `chime-time-left-format-short', and `chime-time-left-format-long' into a single alist `chime-time-left-formats' keyed by `at-event' / `short' / `long'. Three knobs for one feature (countdown display) was unnecessary surface area; one alist is the same flexibility with a third the namespace. `chime--time-left' switched from a pcase-on-variable to a pcase-on-regime that picks an alist key, then `alist-get's the format string. Behavior is identical for default settings. Test setup in the four affected files now builds the alist with `(list (cons 'KEY VAL) ...)' instead of `'(...)'. The literal-quote form returns the SAME cons-cell structure on every evaluation, so a previous test mutating it via `setf' on `alist-get' poisoned later tests. `list' + `cons' produces fresh structure per call, which is what the tests actually need. Migration: `(setq chime-time-left-format-short "in %mm")' becomes `(setf (alist-get 'short chime-time-left-formats) "in %mm")', or a full `(setq chime-time-left-formats '((at-event . ...) (short . ...) (long . ...)))' replacement. --- chime.el | 63 ++++++++++++++++++++++++++++++--------------------------------- 1 file changed, 30 insertions(+), 33 deletions(-) (limited to 'chime.el') diff --git a/chime.el b/chime.el index e843537..9de2e72 100644 --- a/chime.el +++ b/chime.el @@ -224,43 +224,40 @@ Note: Avoid using seconds (%S) as chime polls once per minute." (warn "chime-display-time-format-string: Using seconds (%%S) is not recommended as chime polls once per minute")) (set-default symbol value))) -(defcustom chime-time-left-format-at-event "right now" - "Format string for when event time has arrived (0 or negative seconds). -This is a literal string with no format codes." - :package-version '(chime . "0.6.0") - :group 'chime - :type 'string) - -(defcustom chime-time-left-format-short "in %M" - "Format string for times under 1 hour. -Uses `format-seconds' codes: +(defcustom chime-time-left-formats + '((at-event . "right now") + (short . "in %M") + (long . "in %H %M")) + "Format strings for time-until-event display, keyed by regime. +An alist with three keys: + + at-event - Literal string when event time has arrived (0 or negative + seconds). No format codes. + short - `format-seconds' template for times under 1 hour. + long - `format-seconds' template for times 1 hour or longer. + +Available `format-seconds' codes for short/long: %m - minutes as number only (e.g., \"37\") %M - minutes with unit name (e.g., \"37 minutes\") + %h - hours as number only (e.g., \"1\") + %H - hours with unit name (e.g., \"1 hour\") -Examples: +Examples for short: \"in %M\" -> \"in 37 minutes\" \"in %mm\" -> \"in 37m\" - \"%m min\" -> \"37 min\"" - :package-version '(chime . "0.6.0") - :group 'chime - :type 'string) + \"%m min\" -> \"37 min\" -(defcustom chime-time-left-format-long "in %H %M" - "Format string for times 1 hour or longer. -Uses `format-seconds' codes: - %h - hours as number only (e.g., \"1\") - %H - hours with unit name (e.g., \"1 hour\") - %m - minutes as number only (e.g., \"37\") - %M - minutes with unit name (e.g., \"37 minutes\") - -Examples: +Examples for long: \"in %H %M\" -> \"in 1 hour 37 minutes\" \"in %hh %mm\" -> \"in 1h 37m\" \"(%h hr %m min)\" -> \"(1 hr 37 min)\" \"%hh%mm\" -> \"1h37m\"" - :package-version '(chime . "0.6.0") + :package-version '(chime . "0.8.0") :group 'chime - :type 'string) + :type '(alist :key-type (choice (const at-event) + (const short) + (const long)) + :value-type string)) (defcustom chime-predicate-whitelist nil "Receive notifications for events matching these predicates only. @@ -699,16 +696,16 @@ Returns non-nil only if the timestamp includes HH:MM time information." (defun chime--time-left (seconds) "Human-friendly representation for SECONDS. -Format is controlled by `chime-time-left-format-at-event', -`chime-time-left-format-short', and `chime-time-left-format-long'." +Format is controlled by `chime-time-left-formats' (keys: at-event, +short, long)." ;; Don't fold this into a `(-> seconds (pcase ...) ...)' threading form — ;; edebug's defun parser rejects threaded pcase, which breaks coverage ;; instrumentation (undercover) on this file. - (let ((format-string - (pcase seconds - ((pred (>= 0)) chime-time-left-format-at-event) - ((pred (>= 3600)) chime-time-left-format-short) - (_ chime-time-left-format-long)))) + (let* ((regime (pcase seconds + ((pred (>= 0)) 'at-event) + ((pred (>= 3600)) 'short) + (_ 'long))) + (format-string (alist-get regime chime-time-left-formats))) (format-seconds format-string seconds))) (defun chime--get-hh-mm-from-org-time-string (time-string) -- cgit v1.2.3