From 290407c0eba87ab50a3ff90c21cf103787835f53 Mon Sep 17 00:00:00 2001 From: Guido Schmidt Date: Sat, 27 Apr 2024 00:16:12 +0200 Subject: [PATCH] FIX: endless loop #33 + code cleanup, adjust tests --- circadian.el | 72 +++++++++++++++++++++++++--------------------------- test.el | 25 +++++++++--------- 2 files changed, 47 insertions(+), 50 deletions(-) diff --git a/circadian.el b/circadian.el index deec19c..bfe3f5f 100644 --- a/circadian.el +++ b/circadian.el @@ -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) @@ -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) @@ -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 diff --git a/test.el b/test.el index ebb5489..5737079 100644 --- a/test.el +++ b/test.el @@ -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))) @@ -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 @@ -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 @@ -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 @@ -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)))) @@ -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))))) @@ -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