diff options
-rw-r--r-- | lisp/mastodon-tl.el | 52 | ||||
-rw-r--r-- | lisp/mastodon.el | 2 |
2 files changed, 35 insertions, 19 deletions
diff --git a/lisp/mastodon-tl.el b/lisp/mastodon-tl.el index 1d672a5..1b53460 100644 --- a/lisp/mastodon-tl.el +++ b/lisp/mastodon-tl.el @@ -32,7 +32,6 @@ ;;; Code: (require 'shr) -(require 'ts) (require 'thingatpt) ; for word-at-point (require 'time-date) (require 'cl-lib) @@ -710,6 +709,7 @@ The descriptive string is a human readable version relative to the current time while the next change timestamp give the first time that this description will change in the future. TIMESTAMP is assumed to be in the past." + ;; FIXME: Use `mastodon-tl--human-duration'! (let* ((now (or current-time (current-time))) (time-difference (time-subtract now timestamp)) (seconds-difference (float-time time-difference)) @@ -1145,25 +1145,41 @@ LONGEST-OPTION is the option whose length determines the formatting." (propertize str 'face 'font-lock-comment-face)) "\n")))) +(defconst mastodon-tl--time-units + '("sec" 60.0 ;Use a float to convert `n' to float. + "min" 60 + "hour" 24 + "day" 7 + "week" 4.345 + "month" 12 + "year")) + (defun mastodon-tl--format-poll-expiry (timestamp) "Convert poll expiry TIMESTAMP into a descriptive string." - (let ((parsed (ts-human-duration - (ts-diff (ts-parse timestamp) (ts-now))))) - (cond ((> (plist-get parsed :days) 0) - (format "%s days, %s hours left" - (plist-get parsed :days) - (plist-get parsed :hours))) - ((> (plist-get parsed :hours) 0) - (format "%s hours, %s minutes left" - (plist-get parsed :hours) - (plist-get parsed :minutes))) - ((> (plist-get parsed :minutes) 0) - (format "%s minutes left" (plist-get parsed :minutes))) - (t ; we failed to guess: - (format "%s days, %s hours, %s minutes left" - (plist-get parsed :days) - (plist-get parsed :hours) - (plist-get parsed :minutes)))))) + ;; FIXME: Could we document the format of TIMESTAMP here? + (let* ((ts (encode-time (parse-time-string timestamp))) + (seconds (time-to-seconds (time-subtract ts nil)))) + (concat (mastodon-tl--human-duration (max 0 seconds)) " left"))) + +(defun mastodon-tl--human-duration (seconds) + "Return a string describing SECONDS in a more human-friendly way." + (cl-assert (>= seconds 0)) + (let* ((units mastodon-tl--time-units) + (n1 seconds) (unit1 (pop units)) n2 unit2 + next) + (while (and units (> (truncate (setq next (/ n1 (car units)))) 0)) + (setq unit2 unit1) + (setq n2 (- n1 (* (car units) (truncate n1 (car units))))) + (setq n1 next) + (pop units) + (setq unit1 (pop units))) + (setq n1 (truncate n1)) + (if n2 (setq n2 (truncate n2))) + (if (memq n2 '(nil 0)) + (format "%d %s%s" n1 unit1 (if (> n1 1) "s" "")) + (format "%d %s%s, %d %s%s" + n1 unit1 (if (> n1 1) "s" "") + n2 unit2 (if (> n2 1) "s" ""))))) (defun mastodon-tl--read-poll-option () "Read a poll option to vote on a poll." diff --git a/lisp/mastodon.el b/lisp/mastodon.el index a3b372d..1c5e0d5 100644 --- a/lisp/mastodon.el +++ b/lisp/mastodon.el @@ -7,7 +7,7 @@ ;; Marty Hiatt <martianhiatus@riseup.net> ;; Maintainer: Marty Hiatt <martianhiatus@riseup.net> ;; Version: 1.0.0 -;; Package-Requires: ((emacs "27.1") (request "0.3.0") (persist "0.4") (ts "0.3")) +;; Package-Requires: ((emacs "27.1") (request "0.3.0") (persist "0.4")) ;; Homepage: https://codeberg.org/martianh/mastodon.el ;; This file is not part of GNU Emacs. |