From cfe1ab88a3e4193eee735b6afb85de199bb7ce47 Mon Sep 17 00:00:00 2001 From: contrapunctus Date: Tue, 8 Jun 2021 02:33:42 +0530 Subject: [PATCH] fix(spark): show only max duration for tasks with lone active day --- elisp/chronometrist-spark.el | 33 ++++++++++++++++++++------------ elisp/chronometrist-spark.org | 36 +++++++++++++++++++++++------------ 2 files changed, 45 insertions(+), 24 deletions(-) diff --git a/elisp/chronometrist-spark.el b/elisp/chronometrist-spark.el index 7c359a3..921baef 100644 --- a/elisp/chronometrist-spark.el +++ b/elisp/chronometrist-spark.el @@ -36,6 +36,20 @@ "If non-nil, display range of each sparkline." :type 'boolean) +(defun chronometrist-spark-range (durations active-p) + "Return range for DURATIONS as a string. +" + (let* ((duration-minutes (--map (/ it 60) durations)) + (durations-nonzero (seq-remove #'zerop + duration-minutes))) + (if (= 1 (length durations-nonzero)) + ;; This task only had activity on one day in the given range + ;; of days - these durations, then, cannot really have a + ;; minimum and maximum range + (format "(%sm)" (apply #'max duration-minutes)) + (format "(%sm~%sm)" (apply #'min durations-nonzero) + (apply #'max duration-minutes))))) + (defun chronometrist-spark-row-transformer (row) "Add a sparkline cell to ROW. Used to add a sparkline column to `chronometrist-rows'. @@ -45,24 +59,19 @@ ROW must be a valid element of the list specified by (-let* (((task vector) row) (sparkline (cl-loop with today = (ts-now) - with duration - with active + with duration with active-p for day from (- (- chronometrist-spark-length 1)) to 0 collect (setq duration (chronometrist-task-time-one-day task (ts-adjust 'day day today))) into durations if (not (zerop duration)) - do (setq active t) + do (setq active-p t) finally return - (let ((duration-minutes (--map (/ it 60) durations))) - (if (and active chronometrist-spark-show-range) - (let ((durations-nonzero (seq-remove #'zerop duration-minutes))) - (format "%s (%sm~%sm)" - (spark durations) - (apply #'min durations-nonzero) - (apply #'max duration-minutes))) - (format "%s" (spark durations))))))) + (if (and active-p chronometrist-spark-show-range) + (format "%s %s" (spark durations) + (chronometrist-spark-range durations active-p)) + (format "%s" (spark durations)))))) (list task (vconcat vector `[,sparkline])))) (defun chronometrist-spark-schema-transformer (schema) @@ -71,7 +80,7 @@ Used to add a sparkline column to `chronometrist-schema-transformers'. SCHEMA should be a vector as specified by `tabulated-list-format'." (vconcat schema `[("Graph" ,(if chronometrist-spark-show-range - (+ chronometrist-spark-length 20) + (+ chronometrist-spark-length 12) chronometrist-spark-length) t)])) diff --git a/elisp/chronometrist-spark.org b/elisp/chronometrist-spark.org index 588b9aa..f9dcd83 100644 --- a/elisp/chronometrist-spark.org +++ b/elisp/chronometrist-spark.org @@ -59,6 +59,23 @@ :type 'boolean) #+END_SRC +** range :function: +#+BEGIN_SRC emacs-lisp +(defun chronometrist-spark-range (durations active-p) + "Return range for DURATIONS as a string. +" + (let* ((duration-minutes (--map (/ it 60) durations)) + (durations-nonzero (seq-remove #'zerop + duration-minutes))) + (if (= 1 (length durations-nonzero)) + ;; This task only had activity on one day in the given range + ;; of days - these durations, then, cannot really have a + ;; minimum and maximum range + (format "(%sm)" (apply #'max duration-minutes)) + (format "(%sm~%sm)" (apply #'min durations-nonzero) + (apply #'max duration-minutes))))) +#+END_SRC + ** TODO row-transformer :function: if larger than 7 add space after (% length 7)th element @@ -78,24 +95,19 @@ ROW must be a valid element of the list specified by (-let* (((task vector) row) (sparkline (cl-loop with today = (ts-now) - with duration - with active + with duration with active-p for day from (- (- chronometrist-spark-length 1)) to 0 collect (setq duration (chronometrist-task-time-one-day task (ts-adjust 'day day today))) into durations if (not (zerop duration)) - do (setq active t) + do (setq active-p t) finally return - (let ((duration-minutes (--map (/ it 60) durations))) - (if (and active chronometrist-spark-show-range) - (let ((durations-nonzero (seq-remove #'zerop duration-minutes))) - (format "%s (%sm~%sm)" - (spark durations) - (apply #'min durations-nonzero) - (apply #'max duration-minutes))) - (format "%s" (spark durations))))))) + (if (and active-p chronometrist-spark-show-range) + (format "%s %s" (spark durations) + (chronometrist-spark-range durations active-p)) + (format "%s" (spark durations)))))) (list task (vconcat vector `[,sparkline])))) #+END_SRC @@ -110,7 +122,7 @@ Used to add a sparkline column to `chronometrist-schema-transformers'. SCHEMA should be a vector as specified by `tabulated-list-format'." (vconcat schema `[("Graph" ,(if chronometrist-spark-show-range - (+ chronometrist-spark-length 20) + (+ chronometrist-spark-length 12) chronometrist-spark-length) t)])) #+END_SRC