feat: CSV history

This commit is contained in:
Pavel Korytov 2021-11-06 11:41:17 +03:00
parent 248ef58270
commit d4aba46750

36
pomm.el
View file

@ -109,6 +109,20 @@ The format is the same as in `format-seconds'"
:group 'pomm :group 'pomm
:type 'string) :type 'string)
(defcustom pomm-csv-history-file nil
"The csv history file location.
The parent directory has to exists!
A new entry is written whenever the timer changes status or kind
of period. The format is as follows:
- timestamp
- status
- kind
- iteration"
:group 'pomm
:type 'string)
(defcustom pomm-on-tick-hook nil (defcustom pomm-on-tick-hook nil
"A hook to run on every tick when the timer is running." "A hook to run on every tick when the timer is running."
:group 'pomm :group 'pomm
@ -166,13 +180,16 @@ Updated by `pomm-update-mode-line-string'.")
(history . ,nil) (history . ,nil)
(last-changed-time ,(time-convert nil 'integer))) (last-changed-time ,(time-convert nil 'integer)))
pomm-current-mode-line-string "") pomm-current-mode-line-string "")
(setf (alist-get 'status pomm--state) 'stopped)) (setf (alist-get 'status pomm--state) 'stopped)
(run-hooks 'pomm-on-status-changed-hook))
(defun pomm--init-state () (defun pomm--init-state ()
"Initialize the Pomodoro timer state. "Initialize the Pomodoro timer state.
This function is meant to be ran only once, at the first start of the timer." This function is meant to be ran only once, at the first start of the timer."
(add-hook 'pomm-on-status-changed-hook #'pomm--save-state) (add-hook 'pomm-on-status-changed-hook #'pomm--save-state)
(add-hook 'pomm-on-status-changed-hook #'pomm--maybe-save-csv)
(add-hook 'pomm-on-period-changed-hook #'pomm--maybe-save-csv)
(if (or (not (file-exists-p pomm-state-file-location)) (if (or (not (file-exists-p pomm-state-file-location))
(not pomm-state-file-location)) (not pomm-state-file-location))
(pomm--do-reset) (pomm--do-reset)
@ -204,6 +221,23 @@ This function is meant to be ran only once, at the first start of the timer."
(> (alist-get 'start-time item) cleanup-timestamp)) (> (alist-get 'start-time item) cleanup-timestamp))
(alist-get 'history pomm--state)))))) (alist-get 'history pomm--state))))))
(defun pomm--maybe-save-csv ()
"Write down the current state of the time to csv history.
Set `pomm-csv-history-file' to customize the file location. If the
variable doesn't exist, function does nothing."
(when pomm-csv-history-file
(unless (file-exists-p pomm-csv-history-file)
(with-temp-file pomm-csv-history-file
(insert "timestamp,status,period,iteration\n")))
(write-region
(format "%d,%s,%s,%d\n"
(time-convert nil 'integer)
(symbol-name (alist-get 'status pomm--state))
(symbol-name (alist-get 'kind (alist-get 'current pomm--state)))
(or (alist-get 'iteration (alist-get 'current pomm--state)) 0))
nil pomm-csv-history-file 'append 1)))
(defun pomm-reset () (defun pomm-reset ()
"Reset the Pomodoro timer." "Reset the Pomodoro timer."
(interactive) (interactive)