chronometrist-sexp-query-till -> chronometrist-sexp-between

Implement correct behaviour (removing
chronometrist-common-plist-date-match-p), add tests, add to reference.
This commit is contained in:
contrapunctus 2020-05-21 13:21:11 +05:30
parent 422e60c621
commit 4edb213326
4 changed files with 72 additions and 40 deletions

View File

@ -52,16 +52,6 @@ Used to prevent more than one watch being added for the same
file.")
;;;; Functions
(defun chronometrist-common-plist-date-match-p (plist date)
"Return t if the :start or :stop of PLIST occurs on DATE.
DATE should be a string in the ISO-8601 format (YYYY-MM-DD)."
(let* ((start (chronometrist-timestamp->iso-date
(plist-get plist :start)))
(stop (chronometrist-timestamp->iso-date
(plist-get plist :stop))))
(or (equal start date)
(equal stop date))))
(defun chronometrist-current-task ()
"Return the name of the currently clocked-in task, or nil if not clocked in."
(chronometrist-sexp-current-task))

View File

@ -1,10 +1,12 @@
;;; chronometrist-sexp.el --- s-expression backend for Chronometrist -*- lexical-binding: t; -*-
;;; Commentary:
;;
;; We use (with-current-buffer ... (save-excursion ...)) very often.
;; Could be refactored.
(require 'chronometrist-custom)
(require 'chronometrist-plist-pp)
(require 'ts)
;;; Code:
@ -18,21 +20,38 @@
(find-file-other-window chronometrist-file)
(goto-char (point-max)))
;; FIXME - broken, will only return event if date = whatever the date
;; for the latest events is
(cl-defun chronometrist-sexp-query-till (&optional (date (chronometrist-date)))
"Return events from today until DATE (inclusive).
Events are a list of plists.
If DATE is not supplied, today is used."
(with-current-buffer (find-file-noselect "~/.emacs.d/chronometrist.sexp")
(goto-char (point-max))
(let (expr)
(cl-loop do (backward-list 1)
(setq expr (read (current-buffer)))
(backward-list 1)
while (and expr
(chronometrist-common-plist-date-match-p expr date))
collect expr))))
(cl-defun chronometrist-sexp-between (&optional (ts-beg (chronometrist-date)) (ts-end (ts-adjust 'day +1 (chronometrist-date))))
"Return events between TS-BEG and TS-END.
Events are a list of plists, in reverse chronological order.
If not supplied, TS-BEG is the beginning of today and TS-END is
the beginning of tomorrow, i.e. the events for today are returned.
An event is considered to be between TS-BEG and TS-END even if
just the :start or the :stop time occurs between them. Thus,
events returned may span midnights - use
`chronometrist-midnight-spanning-p' to check."
(with-current-buffer (find-file-noselect chronometrist-file)
(save-excursion
(goto-char (point-max))
(cl-loop
with expr with start with stop
do (backward-list 1)
(setq expr (read (current-buffer)))
(backward-list 1)
;; loop till we reach the beginning of the range
while
(and expr
(setq start (chronometrist-iso-timestamp->ts
(plist-get expr :start))
stop (plist-get expr :stop)
stop (if stop
(setq stop (chronometrist-iso-timestamp->ts stop))
(ts-now)))
(or (ts> start ts-beg) (ts> stop ts-beg)))
when (or (ts-in ts-beg ts-end start)
(ts-in ts-beg ts-end stop))
collect expr))))
(defun chronometrist-sexp-last ()
"Return last s-expression from `chronometrist-file'."

View File

@ -163,14 +163,16 @@
6. Function - chronometrist-plist-pp-to-string (object)
7. Function - chronometrist-plist-pp (object &optional stream)
*** chronometrist-queries.el
1. Function - chronometrist-last ()
1. Function - chronometrist-sexp-query-till (&optional (ts (chronometrist-date)))
* &optional ts -> events
2. Function - chronometrist-last ()
* -> plist
2. Function - chronometrist-task-time-one-day (task &optional (ts (ts-now)))
3. Function - chronometrist-task-time-one-day (task &optional (ts (ts-now)))
* String &optional ts -> seconds
3. Function - chronometrist-active-time-one-day (&optional ts)
4. Function - chronometrist-active-time-one-day (&optional ts)
* &optional ts -> seconds
4. Function - chronometrist-statistics-count-active-days (task &optional (table chronometrist-events))
5. Function - chronometrist-task-events-in-day (task ts)
5. Function - chronometrist-statistics-count-active-days (task &optional (table chronometrist-events))
6. Function - chronometrist-task-events-in-day (task ts)
*** chronometrist-report-custom.el
1. Custom variable - chronometrist-report-buffer-name
2. Custom variable - chronometrist-report-week-start-day
@ -280,13 +282,14 @@
13. Function - chronometrist-goals-on-file-change ()
*** chronometrist-sexp
1. Function - chronometrist-sexp-open-log ()
2. Function - chronometrist-sexp-query-till (&optional (date (chronometrist-date)))
3. Function - chronometrist-sexp-last ()
2. Function - chronometrist-sexp-between (&optional (ts-beg (chronometrist-date)) (ts-end (ts-adjust 'day +1 (chronometrist-date))))
3. Function - chronometrist-sexp-query-till (&optional (date (chronometrist-date)))
4. Function - chronometrist-sexp-last ()
* -> plist
4. Function - chronometrist-sexp-current-task ()
5. Function - chronometrist-sexp-events-populate ()
6. Function - chronometrist-sexp-create-file ()
7. Function - chronometrist-sexp-new (plist &optional (buffer (find-file-noselect chronometrist-file)))
8. Function - chronometrist-sexp-delete-list (&optional arg)
9. Function - chronometrist-sexp-replace-last (plist)
10. Command - chronometrist-sexp-reindent-buffer ()
5. Function - chronometrist-sexp-current-task ()
6. Function - chronometrist-sexp-events-populate ()
7. Function - chronometrist-sexp-create-file ()
8. Function - chronometrist-sexp-new (plist &optional (buffer (find-file-noselect chronometrist-file)))
9. Function - chronometrist-sexp-delete-list (&optional arg)
10. Function - chronometrist-sexp-replace-last (plist)
11. Command - chronometrist-sexp-reindent-buffer ()

View File

@ -0,0 +1,20 @@
;; -*- lexical-binding: t; -*-
(require 'buttercup)
(require 'chronometrist-sexp)
(describe "chronometrist-sexp-between"
:var ((ts (chronometrist-iso-date->ts "2020-05-10")))
(before-all
(setq chronometrist-file-old chronometrist-file
chronometrist-file "tests/test.sexp"))
(after-all
(setq chronometrist-file chronometrist-file-old))
(it "returns events between a certain time"
(expect (length
(chronometrist-sexp-between (chronometrist-iso-date->ts "2020-05-10")
(chronometrist-iso-date->ts "2020-05-11")))
:to-equal 3)))
;; Local Variables:
;; nameless-current-name: "chronometrist"
;; End: