branch: externals/dtache commit 9c16dd3751eb43e34f875f63c26bd0dae37d2307 Author: Niklas Eklund <niklas.ekl...@zenseact.com> Commit: Niklas Eklund <niklas.ekl...@posteo.net>
Update active to state Introduce state which can either have value active or inactive. --- README.org | 6 ++-- documentation/demo_v02.org | 4 +-- dtache-compile.el | 2 +- dtache-consult.el | 6 ++-- dtache-eshell.el | 4 +-- dtache-shell.el | 4 +-- dtache.el | 77 ++++++++++++++++++++++++---------------------- test/dtache-test.el | 12 ++++---- 8 files changed, 59 insertions(+), 56 deletions(-) diff --git a/README.org b/README.org index 4bffcf90b5..f4d36ece8e 100644 --- a/README.org +++ b/README.org @@ -212,7 +212,7 @@ The user have the possibility to integrate =dtache= with the package [[https://g By default =dtache= uses the echo area to notify the user when a session has finished. An alternative is to utilize the [[https://github.com/jwiegley/alert][alert]] package to get a system notification instead. #+begin_src elisp :lexical t :results none - (defun my/dtache-inactive-session-notification (session) + (defun my/dtache-state-transition-notification (session) "Send an `alert' notification when SESSION becomes inactive." (let ((status (dtache--session-status session)) (title @@ -229,7 +229,7 @@ By default =dtache= uses the echo area to notify the user when a session has fin ('success 'dtache-success) ('failure 'dtache-failure))))) - (setq dtache-notification-function #'my/dtache-inactive-session-notification) + (setq dtache-notification-function #'my/dtache-state-transition-notification) #+end_src * Customization @@ -239,7 +239,7 @@ The user can customize the appearance of annotations in =dtache-open-session= by #+begin_src elisp :results none (defvar dtache-annotation-format - `((:width 3 :function dtache--active-str :face dtache-active-face) + `((:width 3 :function dtache--state-str :face dtache-state-face) (:width 3 :function dtache--status-str :face dtache-failure-face) (:width 10 :function dtache--session-host :face dtache-host-face) (:width 40 :function dtache--working-dir-str :face dtache-working-dir-face) diff --git a/documentation/demo_v02.org b/documentation/demo_v02.org index 87615c13aa..638ec2e254 100644 --- a/documentation/demo_v02.org +++ b/documentation/demo_v02.org @@ -67,7 +67,7 @@ This has also resulted in the deprecation of =dtache-list-sessions=, users are i #+begin_src elisp (defvar dtache-annotation-format - `((:width 3 :function dtache--active-str :face dtache-active-face) + `((:width 3 :function dtache--state-str :face dtache-state-face) (:width 3 :function dtache--status-str :face dtache-failure-face) (:width 10 :function dtache--session-host :face dtache-host-face) (:width 40 :function dtache--working-dir-str :face dtache-working-dir-face) @@ -104,7 +104,7 @@ If you are an =embark= user the way to integrate =dtache= with =embark= is to ad #+begin_src elisp :lexical t :results none (setq dtache-annotation-format - `((:width 3 :function dtache--active-str :face dtache-active-face) + `((:width 3 :function dtache--state-str :face dtache-state-face) (:width 3 :function dtache--status-str :face dtache-failure-face) (:width 10 :function dtache--session-host :face dtache-host-face) (:width 30 :function dtache--working-dir-str :face dtache-working-dir-face) diff --git a/dtache-compile.el b/dtache-compile.el index 77fa927d41..1d674ae5f1 100644 --- a/dtache-compile.el +++ b/dtache-compile.el @@ -80,7 +80,7 @@ Optionally EDIT-COMMAND." (defun dtache-compile-open (session) "Open SESSION with `dtache-compile'." (when (dtache-valid-session session) - (if (dtache--session-active session) + (if (eq 'active (dtache--session-state session)) (dtache-compile-attach session) (dtache-post-compile-session session)))) diff --git a/dtache-consult.el b/dtache-consult.el index 8fc33488ae..3c6191d726 100644 --- a/dtache-consult.el +++ b/dtache-consult.el @@ -69,7 +69,7 @@ See `consult-multi' for a description of the source values." (mapcar #'car (seq-filter (lambda (x) - (dtache--session-active (cdr x))) + (eq 'active (dtache--session-state (cdr x)))) (dtache-session-candidates (dtache-get-sessions)))))) "Active `dtache' sessions as a source for `consult'.") @@ -82,9 +82,9 @@ See `consult-multi' for a description of the source values." :items ,(lambda () (mapcar #'car - (seq-remove + (seq-filter (lambda (x) - (dtache--session-active (cdr x))) + (eq 'inactive (dtache--session-state (cdr x)))) (dtache-session-candidates (dtache-get-sessions)))))) "Inactive `dtache' sessions as a source for `consult'.") diff --git a/dtache-eshell.el b/dtache-eshell.el index f3f1d36387..9e8e0bf377 100644 --- a/dtache-eshell.el +++ b/dtache-eshell.el @@ -56,7 +56,7 @@ (thread-last (dtache-get-sessions) (seq-filter (lambda (it) (string= (dtache--session-host it) current-host))) - (seq-filter #'dtache--session-active-p)))) + (seq-filter #'dtache--determine-session-state)))) (dtache-completing-read sessions))) (defun dtache-eshell-get-dtach-process () @@ -85,7 +85,7 @@ If prefix-argument directly DETACH from the session." (interactive (list (dtache-eshell-select-session))) (when (dtache-valid-session session) - (if (and (dtache--session-active-p session) + (if (and (eq 'active (dtache--determine-session-state session)) (not (dtache--session-redirect-only session))) (cl-letf* ((dtache-session-mode 'attach) (input diff --git a/dtache-shell.el b/dtache-shell.el index 301fc4d2c0..b2409ca4cc 100644 --- a/dtache-shell.el +++ b/dtache-shell.el @@ -58,7 +58,7 @@ (thread-last (dtache-get-sessions) (seq-filter (lambda (it) (string= (dtache--session-host it) current-host))) - (seq-filter #'dtache--session-active-p)))) + (seq-filter (lambda (it) (eq 'active (dtache--determine-session-state it))))))) (dtache-completing-read sessions))) ;;;; Commands @@ -82,7 +82,7 @@ cluttering the comint-history with dtach commands." (interactive (list (dtache-shell-select-session))) (when (dtache-valid-session session) - (if (and (dtache--session-active-p session) + (if (and (eq 'active (dtache--determine-session-state session)) (not (dtache--session-redirect-only session))) (cl-letf ((dtache--current-session session) (comint-input-sender #'dtache-shell--attach-input-sender) diff --git a/dtache.el b/dtache.el index 7f02d32d1a..b3194ca1d5 100644 --- a/dtache.el +++ b/dtache.el @@ -86,7 +86,7 @@ :group 'dtache) (defcustom dtache-annotation-format - '((:width 3 :function dtache--active-str :face dtache-active-face) + '((:width 3 :function dtache--state-str :face dtache-state-face) (:width 3 :function dtache--status-str :face dtache-failure-face) (:width 10 :function dtache--session-host :face dtache-host-face) (:width 40 :function dtache--working-dir-str :face dtache-working-dir-face) @@ -121,8 +121,9 @@ :type '(repeat (regexp :format "%v")) :group 'dtache) -(defcustom dtache-notification-function #'dtache-inactive-session-notification - "Variable to specify notification function when a session becomes inactive." +(defcustom dtache-notification-function #'dtache-state-transition-notification + "Variable to set which function to use to issue a notification when +a session transitions from active to inactive." :type 'function :group 'dtache) @@ -173,9 +174,9 @@ Valid values are: create, new and attach") '((t :inherit error)) "Face used to highlight failure in `dtache'.") -(defface dtache-active-face +(defface dtache-state-face '((t :inherit success)) - "Face used to highlight active in `dtache'.") + "Face used to highlight state in `dtache'.") (defface dtache-duration-face '((t :inherit font-lock-builtin-face)) @@ -239,7 +240,7 @@ Valid values are: create, new and attach") (status nil) (duration nil) (output-size nil) - (active nil)) + (state nil)) ;;;; Commands @@ -268,7 +269,7 @@ Optionally SUPPRESS-OUTPUT." (interactive (list (dtache-completing-read (dtache-get-sessions)))) (when (dtache-valid-session session) - (if (dtache--session-active-p session) + (if (eq 'active (dtache--determine-session-state session)) (dtache--attach-session session) (dtache--view-session session)))) @@ -343,7 +344,7 @@ Optionally SUPPRESS-OUTPUT." (interactive (list (dtache-completing-read (dtache-get-sessions)))) (when (dtache-valid-session session) - (if (dtache--session-active-p session) + (if (eq 'active (dtache--determine-session-state session)) (message "Kill session first before removing it.") (dtache--db-remove-entry session)))) @@ -385,7 +386,7 @@ Optionally SUPPRESS-OUTPUT." (interactive (list (dtache-completing-read (dtache-get-sessions)))) (when (dtache-valid-session session) - (if (dtache--session-active-p session) + (if (eq 'active (dtache--determine-session-state session)) (let* ((file-path (dtache--session-file session 'log)) (tramp-verbose 1)) @@ -421,7 +422,7 @@ Optionally SUPPRESS-OUTPUT." "Detach from current session. This command is only activated if `dtache--buffer-session' is set and -`dtache--session-active-p' returns t." +`dtache--determine-session-state' returns t." (interactive) (if (dtache-session-p dtache--buffer-session) (if-let ((command-or-compile @@ -435,7 +436,7 @@ This command is only activated if `dtache--buffer-session' is set and (message "[detached]")) (setq dtache--buffer-session nil) (kill-buffer-and-window)) - (if (dtache--session-active-p dtache--buffer-session) + (if (eq 'active (dtache--determine-session-state dtache--buffer-session)) ;; `dtache-eshell' (if-let ((process (and (eq major-mode 'eshell-mode) (dtache-eshell-get-dtach-process)))) @@ -492,7 +493,7 @@ nil before closing." :log-directory (file-name-as-directory dtache-log-directory) :host (dtache--host) :metadata (dtache-metadata) - :active t))) + :state 'active))) (dtache--db-insert-entry session) (dtache--start-session-monitor session) session)) @@ -566,13 +567,13 @@ Optionally SUPPRESS-OUTPUT." ;; Update local active sessions (when (and (string= "localhost" (dtache--session-host session)) - (dtache--session-active session)) + (eq 'active (dtache--session-state session))) (dtache--update-session session)))) (dtache--db-get-sessions)) ;; Start monitors (thread-last (dtache--db-get-sessions) - (seq-filter #'dtache--session-active) + (seq-filter (lambda (it) (eq 'active (dtache--session-state it)))) (seq-do #'dtache--start-session-monitor)) ;; Add `dtache-shell-mode' @@ -601,8 +602,8 @@ If session is not valid trigger an automatic cleanup on SESSION's host." 'success 'failure)))) -(defun dtache-inactive-session-notification (session) - "Send a notification when SESSION becomes inactive." +(defun dtache-state-transition-notification (session) + "Send a notification when SESSION transitions from active to inactive." (let ((status (pcase (dtache--session-status session) ('success "Dtache finished") ('failure "Dtache failed")) )) @@ -755,16 +756,18 @@ Optionally CONCAT the command return command into a string." "..." (substring command (- (length command) (/ dtache-max-command-length 2)) (length command)))))) -(defun dtache--session-active-p (session) +(defun dtache--determine-session-state (session) "Return t if SESSION is active." - (file-exists-p - (dtache--session-file session 'socket))) + (if (file-exists-p + (dtache--session-file session 'socket)) + 'active + 'inactive)) -(defun dtache--session-deactivated-p (session) - "Return t if SESSION has been deactivated." +(defun dtache--state-transition-p (session) + "Return t if SESSION has transitioned from active to inactive." (and - (dtache--session-active session) - (not (file-exists-p (dtache--session-file session 'socket))))) + (eq 'active (dtache--session-state session)) + (eq 'inactive (dtache--determine-session-state session)))) (defun dtache--session-missing-p (session) "Return t if SESSION is missing." @@ -793,8 +796,8 @@ Optionally CONCAT the command return command into a string." (let* ((timer) (callback (lambda () - (when (dtache--session-deactivated-p session) - (dtache--session-final-update session) + (when (dtache--state-transition-p session) + (dtache--session-state-transition-update session) (cancel-timer timer))))) (setq timer (funcall (plist-get dtache-timer-configuration :function) @@ -810,7 +813,7 @@ Optionally CONCAT the command return command into a string." (lambda (event) (pcase-let ((`(,_ ,action ,_) event)) (when (eq action 'deleted) - (dtache--session-final-update session)))))) + (dtache--session-state-transition-update session)))))) (defun dtache--session-deduplicate (sessions) "Make car of SESSIONS unique by adding an identifier to it." @@ -845,16 +848,16 @@ Sessions running on current host or localhost are updated." (seq-do (lambda (it) (if (and (or (string= current-host (dtache--session-host it)) (string= "localhost" (dtache--session-host it))) - (or (dtache--session-active it) - (dtache--session-deactivated-p it))) + (or (eq 'active (dtache--session-state it)) + (dtache--state-transition-p it))) (dtache--update-session it))) (dtache--db-get-sessions)))) (defun dtache--update-session (session) "Update SESSION." - (if (or (dtache--session-deactivated-p session) + (if (or (dtache--state-transition-p session) (dtache--session-missing-p session)) - (dtache--session-final-update session) + (dtache--session-state-transition-update session) (setf (dtache--session-output-size session) (file-attribute-size (file-attributes (dtache--session-file session 'log)))) @@ -984,8 +987,8 @@ Optionally make the path LOCAL to host." ('attach "-a") (_ "-n"))) -(defun dtache--session-final-update (session) - "Make a final update to SESSION." +(defun dtache--session-state-transition-update (session) + "Update SESSION due to state transition." (if (dtache--session-missing-p session) ;; Remove missing session (dtache--db-remove-entry session) @@ -996,7 +999,7 @@ Optionally make the path LOCAL to host." (file-attributes (dtache--session-file session 'log)))) - (setf (dtache--session-active session) nil) + (setf (dtache--session-state session) 'inactive) (setf (dtache--session-duration session) (- (time-to-seconds) (dtache--session-creation-time session))) @@ -1097,7 +1100,7 @@ the current time is used." (defun dtache--duration-str (session) "Return SESSION's duration time." (let* ((time - (round (if (dtache--session-active session) + (round (if (eq 'active (dtache--session-state session)) (- (time-to-seconds) (dtache--session-creation-time session)) (dtache--session-duration session)))) (hours (/ time 3600)) @@ -1125,9 +1128,9 @@ the current time is used." ('success " ") ('unknown " "))) -(defun dtache--active-str (session) - "Return string if SESSION is active." - (if (dtache--session-active session) +(defun dtache--state-str (session) + "Return string based on SESSION state." + (if (eq 'active (dtache--session-state session)) "*" " ")) diff --git a/test/dtache-test.el b/test/dtache-test.el index 7fc35e4784..004c9f918a 100644 --- a/test/dtache-test.el +++ b/test/dtache-test.el @@ -146,9 +146,9 @@ (ert-deftest dtache-test-session-active-p () (dtache-test--with-temp-database (let ((session (dtache-test--create-session :command "foo" :host "localhost"))) - (should (dtache--session-active-p session)) + (should (eq 'active (dtache--determine-session-state session))) (dtache-test--change-session-state session 'deactivate) - (should (not (dtache--session-active-p session)))))) + (should (eq 'inactive (dtache--determine-session-state session)))))) (ert-deftest dtache-test-session-dead-p () (dtache-test--with-temp-database @@ -196,7 +196,7 @@ (id (dtache--session-id session)) (copy)) (setq copy (copy-dtache-session session)) - (setf (dtache--session-active copy) nil) + (setf (dtache--session-state copy) nil) (should (not (equal copy (dtache--db-get-session id)))) (dtache--db-update-entry copy t) (should (equal copy (car (dtache--db-get-sessions))))))) @@ -264,9 +264,9 @@ (should (string= " " (dtache--status-str (dtache--session-create :status 'success)))) (should (string= " " (dtache--status-str (dtache--session-create :status 'unknown))))) -(ert-deftest dtache-test-active-str () - (should (string= "*" (dtache--active-str (dtache--session-create :active t)))) - (should (string= " " (dtache--active-str (dtache--session-create :active nil))))) +(ert-deftest dtache-test-state-str () + (should (string= "*" (dtache--state-str (dtache--session-create :state 'active)))) + (should (string= " " (dtache--state-str (dtache--session-create :state 'inactive))))) (ert-deftest dtache-test-working-dir-str () (should