Skip to content

Commit

Permalink
Add expiry checks when starting a new pomodoro
Browse files Browse the repository at this point in the history
Thanks to @chrisbarrett.
  • Loading branch information
leoc committed Nov 16, 2013
1 parent 647c0b5 commit 7434dd3
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
20 changes: 20 additions & 0 deletions org-pomodoro-tests.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
;;; org-pomodoro-tests.el --- Org-pomodoro tests

;;; Commentary:
;; All tests for org-pomodoro.el.

;;; Code:

(ert-deftest org-pomodoro-expires-when-last-clockin-too-old ()
"Test the return value is true when org-pomodoro-last-clock-in is too old."
(let ((org-pomodoro-last-clock-in '(0 120 0 0))
(org-pomodoro-expiry-time 120))
(should (equal t (org-pomodoro-expires-p)))))

(ert-deftest org-pomodoro-expires-when-last-clockin-is-new ()
"Test the return value is false when org-pomodoro-last-clock-in is new."
(let ((org-pomodoro-last-clock-in (current-time))
(org-pomodoro-expiry-time 120))
(should (equal nil (org-pomodoro-expires-p)))))

;;; org-pomodoro-tests.el ends here
25 changes: 25 additions & 0 deletions org-pomodoro.el
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@
:group 'org-pomodoro
:type 'file)

(defcustom org-pomodoro-expiry-time 120
"The time in minutes for which a pomodoro group is valid.
If you do not clock in for this period of time you will be prompted
whether to reset the pomodoro count next time you call `org-pomodoro'."
:group 'org-pomodoro
:type 'integer)

;; Break Values

(defcustom org-pomodoro-short-break-length 5
Expand Down Expand Up @@ -178,6 +185,9 @@ or :break when starting a break.")
(defvar org-pomodoro-count 0
"The number of pomodoros since the last long break.")

(defvar org-pomodoro-last-clock-in nil
"The last time the pomodoro was set.")

;;; Internal

;; Helper Functions
Expand All @@ -186,6 +196,13 @@ or :break when starting a break.")
"Retrieve whether org-pomodoro is active or not."
(not (eq org-pomodoro-state :none)))

(defun org-pomodoro-expires-p ()
"Return true when the last clock-in was more than `org-pomodoro-expiry-time`."
(let* ((current-time-secs (nth 1 (current-time)))
(last-clock-in-secs (nth 1 org-pomodoro-last-clock-in))
(delta-minutes (/ (- current-time-secs last-clock-in-secs) 60)))
(< org-pomodoro-expiry-time delta-minutes)))

(defun org-pomodoro-play-sound (type)
"Play an audio file specified by TYPE (:pomodoro, :short-break, :long-break)."
(let ((sound (cl-case type
Expand Down Expand Up @@ -323,6 +340,14 @@ When no timer is running for `org-pomodoro` a new pomodoro is started and
the current task is clocked in. Otherwise EMACS will ask whether we´d like to
kill the current timer, this may be a break or a running pomodoro."
(interactive)

(when (and org-pomodoro-last-clock-in
org-pomodoro-expiry-time
(org-pomodoro-expires-p)
(y-or-n-p "Reset pomodoro count? "))
(setq org-pomodoro-count 0))
(setq org-pomodoro-last-clock-in (current-time))

(if (equal org-pomodoro-state :none)
(progn
(cond
Expand Down

0 comments on commit 7434dd3

Please sign in to comment.