Skip to content

Commit

Permalink
FIX: endless loop #33 + code cleanup, adjust tests
Browse files Browse the repository at this point in the history
  • Loading branch information
guidoschmidt committed Apr 26, 2024
1 parent 1bc8c4c commit 290407c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 50 deletions.
72 changes: 35 additions & 37 deletions circadian.el
Original file line number Diff line number Diff line change
Expand Up @@ -70,41 +70,20 @@
;; Only load the argument theme, when `custom-enabled-themes'
;; does not contain it.
(mapc #'disable-theme custom-enabled-themes)

(if (not (equal nil circadian-next-timer))
(progn
(cancel-timer circadian-next-timer)
(setq circadian-next-timer nil)))

(condition-case nil
(progn
(run-hook-with-args 'circadian-before-load-theme-hook theme)

(if (not (equal (list theme) custom-enabled-themes))
(progn
(setq circadian-next-timer nil)
(load-theme theme t)
(let ((time (circadian-now-time)))
(message "[circadian.el] → Enabled %s theme @ %s"
theme
(format-time-string "%H:%M:%S" time)))))

(let* ((themes (circadian-themes-parse))
(now (circadian-now-time))
(past-themes (circadian-filter-inactivate-themes themes now))
(entry (car (last (or past-themes themes))))
(next-entry (or (cadr (member entry themes))
(if (circadian-a-earlier-b-p (circadian-now-time) (cl-first entry))
(car themes)
(cl-first past-themes))))
(next-time (circadian--encode-time
(cl-first (cl-first next-entry))
(cl-second (cl-first next-entry)))))
(if (equal nil circadian-next-timer)
(progn
(setq circadian-next-timer (run-at-time next-time nil #'circadian-activate-current))
(message (concat "[circadian.el] → Next run @ " (format-time-string "%H:%M:%S" next-time))))))

(run-hook-with-args 'circadian-after-load-theme-hook theme))
(message "[circadian.el] → Enabled %s theme @ %s"
theme
(format-time-string "%H:%M:%S %Z"))
(circadian-schedule)))

(run-hook-with-args 'circadian-after-load-theme-hook theme))
(error "[circadian.el/ERROR] → Problem loading theme %s" theme)))

(defun circadian--encode-time (hour min)
Expand Down Expand Up @@ -160,16 +139,33 @@ set and and sort the final list by time."
(entry (car (last (or past-themes themes))))
(theme-or-theme-list (cdr entry))
(theme (if (listp theme-or-theme-list)
(nth (random (length (cl-second theme-or-theme-list))) (cl-second theme-or-theme-list))
(progn
(nth (random (length theme-or-theme-list)) theme-or-theme-list))
theme-or-theme-list)))
(circadian-enable-theme theme)
))

(circadian-enable-theme theme)))

(defun circadian-activate-and-schedule ()
"Check which themes are overdue to be activated and load the last."
(interactive)
(circadian-activate-current))
(defun circadian-schedule()
"Schedule the next timer for circadian."
(let* ((themes (circadian-themes-parse))
(now (circadian-now-time))
(past-themes (circadian-filter-inactivate-themes themes now))
(entry (car (last (or past-themes themes))))
(next-entry (or (cadr (member entry themes))
(if (circadian-a-earlier-b-p (circadian-now-time) (cl-first entry))
(car themes)
(cl-first past-themes))))
(next-theme (cdr next-entry))
(next-time (circadian--encode-time
(cl-first (cl-first next-entry))
(cl-second (cl-first next-entry)))))
(if (equal nil circadian-next-timer)
(progn
(setq circadian-next-timer (run-at-time next-time nil #'circadian-activate-current))
(message "[circadian.el] → Next theme %s @ %s"
(if (listp next-theme)
(concat "one of " (format "%s" next-theme))
next-theme)
(format-time-string "%H:%M:%S %Z" next-time))))))

;; --- Sunset-sunrise
(defun circadian--frac-to-time (f)
Expand Down Expand Up @@ -249,7 +245,9 @@ or set calendar-longitude:
;;;###autoload
(defun circadian-setup ()
"Setup circadian based on `circadian-themes'."
(circadian-activate-and-schedule))
(interactive)
(circadian-activate-current)
(circadian-schedule))

(provide 'circadian)
;;; circadian.el ends here
25 changes: 12 additions & 13 deletions test.el
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
time-now)))))
(with-mock
(stub circadian-now-time => '(5 0 0))
(circadian-activate-and-schedule)
(circadian-setup)

(should (equal (list 'adwaita) custom-enabled-themes)))

Expand All @@ -34,7 +34,7 @@
time-now)))))
(with-mock
(stub circadian-now-time => '(5 2 0))
(circadian-activate-and-schedule)
(circadian-setup)
(should (equal (list 'wombat) custom-enabled-themes)))

;; After 14:47, before 23:59
Expand All @@ -44,7 +44,7 @@
time-now)))))
(with-mock
(stub circadian-now-time => '(14 47 1))
(circadian-activate-and-schedule)
(circadian-setup)
(should (equal (list 'tango) custom-enabled-themes)))

;; After 23:59
Expand All @@ -54,7 +54,7 @@
time-now)))))
(with-mock
(stub circadian-now-time => '(23 59 15))
(circadian-activate-and-schedule)
(circadian-setup)
(should (equal (list 'adwaita) custom-enabled-themes)))

;; Surpassing midnight
Expand All @@ -64,23 +64,22 @@
time-now)))))
(with-mock
(stub circadian-now-time => '(0 2 10))
(circadian-activate-and-schedule)
(circadian-setup)
(should (equal (list 'adwaita) custom-enabled-themes))))



(ert-deftest test-circadian-activate-and-schedule ()
"Test `circadian-activate-and-schedule' used in `circadian-setup'."
;; (print "→ TEST: circadian-activate-and-schedule")
(ert-deftest test-circadian-setup ()
"Test `circadian-setup'."
(setq circadian-themes '(("7:00" . wombat)
("16:00" . tango)))
(with-mock
(stub circadian-now-time => '(7 21 0))
(circadian-activate-and-schedule)
(circadian-setup)
(should (equal (list 'wombat) custom-enabled-themes)))
(with-mock
(stub circadian-now-time => '(17 0 0))
(circadian-activate-and-schedule)
(circadian-setup)
(should (equal (list 'tango) custom-enabled-themes))))


Expand All @@ -94,11 +93,11 @@
(setq circadian-themes '((:sunrise . adwaita)
(:sunset . wombat)))
(stub circadian-now-time => '(14 21 0))
(circadian-activate-and-schedule)
(circadian-setup)
(should (equal 'adwaita (cl-first custom-enabled-themes)))

(stub circadian-now-time => '(22 30 0))
(circadian-activate-and-schedule)
(circadian-setup)
(should (equal 'wombat (cl-first custom-enabled-themes)))))


Expand Down Expand Up @@ -153,7 +152,7 @@ https://github.com/guidoschmidt/circadian.el/issues/27"

(defvar test-order '(member
test-circadian-filter-and-activate-themes
test-circadian-activate-and-schedule
test-circadian-setup
test-circadian-sunrise-sunset
test-circadian-time-comparisons
test-circadian-setup-benchmark
Expand Down

0 comments on commit 290407c

Please sign in to comment.