fix: handle midnight-spanning intervals when appending plist to file

This commit is contained in:
contrapunctus 2021-06-18 03:52:11 +05:30
parent f790d84a08
commit 0e10742982
3 changed files with 37 additions and 10 deletions

View File

@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed
3. Display graph ranges in `chronometrist-spark` column
4. `chronometrist-tags-add` and `chronometrist-key-values-unified-prompt` now also work interactively.
### Fixed
5. Partial update of `chronometrist-events` takes midnight-spanning intervals into account.
## [0.8.1] - 2021-06-01
### Changed

View File

@ -752,6 +752,30 @@ If REPLACE is non-nil, replace the last event with PLIST."
(append it (list plist))
(puthash date it chronometrist-events))))
(defun chronometrist-events-add-helper (key plist table)
"Append PLIST to values for KEY in TABLE.
TABLE must be a hash table where the value for KEY is a list."
(--> (append (gethash key table) (list plist))
(puthash key it table)))
(cl-defun chronometrist-events-add (plist &optional (table chronometrist-events))
"Add PLIST to the end of TABLE, splitting PLIST if necessary."
(-let* ((start-ts (chronometrist-iso-timestamp-to-ts
(plist-get plist :start)))
(start-date (ts-format "%F" start-ts))
(start-date-events (gethash start-date table))
(split (chronometrist-events-maybe-split plist))
((split-1 split-2) split)
(((&plist :start old-start :stop old-stop)
(&plist :start new-start :stop new-stop)) split)
(new-start-date (when new-start
(ts-format "%F"
(chronometrist-iso-timestamp-to-ts new-start)))))
(if (null split)
(chronometrist-events-add-helper start-date plist table)
(chronometrist-events-add-helper start-date split-1 table)
(chronometrist-events-add-helper new-start-date split-2 table))))
(defun chronometrist-events-last-date ()
"Return an ISO-8601 date string for the latest date present in `chronometrist-events'."
(--> (hash-table-keys chronometrist-events)
@ -1253,7 +1277,7 @@ the `chronometrist' buffer."
((&plist :name new-task) (chronometrist-sexp-last)))
(pcase change
(:append ;; a new plist was added at the end of the file
(chronometrist-events-update (chronometrist-sexp-last))
(chronometrist-events-add (chronometrist-sexp-last))
(chronometrist-add-to-task-list new-task))
(:modify ;; the last plist in the file was changed
(chronometrist-events-update (chronometrist-sexp-last) t)

View File

@ -1643,16 +1643,17 @@ If REPLACE is non-nil, replace the last event with PLIST."
(puthash date it chronometrist-events))))
#+END_SRC
#+BEGIN_SRC emacs-lisp :tangle no :load no
#+BEGIN_SRC emacs-lisp
(defun chronometrist-events-add-helper (key plist table)
"Append PLIST to values for KEY in TABLE."
"Append PLIST to values for KEY in TABLE.
TABLE must be a hash table where the value for KEY is a list."
(--> (append (gethash key table) (list plist))
(puthash key it table)))
#+END_SRC
#+BEGIN_SRC emacs-lisp :tangle no :load no
#+BEGIN_SRC emacs-lisp
(cl-defun chronometrist-events-add (plist &optional (table chronometrist-events))
"Add PLIST to the end of TABLE, splitting it if necessary."
"Add PLIST to the end of TABLE, splitting PLIST if necessary."
(-let* ((start-ts (chronometrist-iso-timestamp-to-ts
(plist-get plist :start)))
(start-date (ts-format "%F" start-ts))
@ -1662,12 +1663,12 @@ If REPLACE is non-nil, replace the last event with PLIST."
(((&plist :start old-start :stop old-stop)
(&plist :start new-start :stop new-stop)) split)
(new-start-date (when new-start
(chronometrist-iso-timestamp-to-ts new-start))))
(ts-format "%F"
(chronometrist-iso-timestamp-to-ts new-start)))))
(if (null split)
(chronometrist-events-add-helper start-date plist table)
(chronometrist-events-add-helper start-date split-1 table)
(chronometrist-events-add-helper new-start-date split-2 table))))
#+END_SRC
*** last-date :reader:
#+BEGIN_SRC emacs-lisp
@ -2356,7 +2357,7 @@ the `chronometrist' buffer."
((&plist :name new-task) (chronometrist-sexp-last)))
(pcase change
(:append ;; a new plist was added at the end of the file
(chronometrist-events-update (chronometrist-sexp-last))
(chronometrist-events-add (chronometrist-sexp-last))
(chronometrist-add-to-task-list new-task))
(:modify ;; the last plist in the file was changed
(chronometrist-events-update (chronometrist-sexp-last) t)
@ -2389,7 +2390,7 @@ the `chronometrist' buffer."
(chronometrist-out))
t))
#+END_SRC
**** chronometrist-in :command:
**** chronometrist-in :command:
#+BEGIN_SRC emacs-lisp
(defun chronometrist-in (task &optional _prefix)
"Clock in to TASK; record current time in `chronometrist-file'.
@ -2399,7 +2400,7 @@ TASK is the name of the task, a string. PREFIX is ignored."
(chronometrist-sexp-new plist)
(chronometrist-refresh)))
#+END_SRC
**** chronometrist-out :command:
**** chronometrist-out :command:
#+BEGIN_SRC emacs-lisp
(defun chronometrist-out (&optional _prefix)
"Record current moment as stop time to last s-exp in `chronometrist-file'.