emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] externals/dtache c09328ca1e 081/158: Add actions to a session


From: ELPA Syncer
Subject: [elpa] externals/dtache c09328ca1e 081/158: Add actions to a session
Date: Wed, 19 Jan 2022 18:58:00 -0500 (EST)

branch: externals/dtache
commit c09328ca1edd305988842f077eb3998f79d8d3d8
Author: Niklas Eklund <niklas.eklund@posteo.net>
Commit: Niklas Eklund <niklas.eklund@posteo.net>

    Add actions to a session
    
    The actions is a property of a dtache-session which is a property
    list. It is designed to customize the action properties of a session.
---
 README.org        | 26 --------------------
 dtache-compile.el |  5 ++--
 dtache-eshell.el  |  2 ++
 dtache-shell.el   | 12 +++------
 dtache.el         | 73 +++++++++++++++++++++++--------------------------------
 5 files changed, 39 insertions(+), 79 deletions(-)

diff --git a/README.org b/README.org
index dbea0b0173..59def66344 100644
--- a/README.org
+++ b/README.org
@@ -325,32 +325,6 @@ A part from the extensions provided with this package the 
users of =dtache= can
   (advice-add #'dired-rsync--do-run :around #'my/dtache-dired-rsync-advice)
 #+end_src
 
-** Customize an individual session
-
-=Dtache= tries to leave possibilities for the users to customize the usage. 
One key feature is that the customization can happen on session level. Meaning 
that the user can choose to customize a session right before it is started. The 
customization will then be embedded into the session object and persist for as 
long as the session exists.
-
-#+begin_src elisp :lexical t :dir ~/code/python :results none
-  (defun my/dtache-custom-open-session (command)
-    "Run COMMAND in a session and control how it should be opened."
-    (let ((dtache-open-session-function
-           (lambda (session)
-             (let ((dtache-compile-hooks
-                    `(,@dtache-compile-hooks
-                      ,(lambda ()
-                         (make-variable-buffer-local 
'compilation-parse-errors-function)
-                         (setq compilation-parse-errors-filename-function
-                               (lambda (_)
-                                 (format "~/.bashrc")))))))
-               (dtache-open-dwim session)))))
-      (dtache-start-session command)))
-
-  (demo/dtache-custom-session "pylint demo.py")
-#+end_src
-
-The example in the code block is creating a custom open function that will 
modify the =compilation-parse-errors-function= and set it locally in the buffer 
that will be opened with =dtache-compile-session=. In the example regardless of 
what errors pylint manages to find the links in the compile buffer will all 
open =~/.bashrc=. This customization only affects the sessions that are started 
with the =my/dtache-custom-open-session=.
-
-The user also have the same possibility of customization when it comes to the 
variables =dtache-session-callback-function= and 
=dtache-session-status-function=.
-
 * Versions
 
 Information about the changes to the package can be found in the 
=CHANGELOG.org=.
diff --git a/dtache-compile.el b/dtache-compile.el
index ce5a480a35..301e54b3b5 100644
--- a/dtache-compile.el
+++ b/dtache-compile.el
@@ -32,8 +32,7 @@
 
 (defvar dtache-compile-command nil
   "This variable has value t if `compile' is supposed to run with `dtache'.")
-(defvar dtache-compile-history nil
-  "History of commands run with `dtache-compile'.")
+(defvar dtache-compile-session-action '(:attach dtache-compile-attach :view 
dtache-compile-session))
 
 ;;;; Commands
 
@@ -43,6 +42,7 @@
 Optionally enable COMINT if prefix-argument is provided."
   (interactive)
   (let* ((dtache-compile-command t)
+         (dtache-session-action dtache-compile-session-action)
          (dtache-session-type 'compile)
          (dtache--dtach-mode 'create))
     (call-interactively #'compile)))
@@ -53,6 +53,7 @@ Optionally enable COMINT if prefix-argument is provided."
 Optionally EDIT-COMMAND."
   (interactive)
   (let* ((dtache-compile-command t)
+         (dtache-session-action dtache-compile-session-action)
          (dtache-session-type 'compile)
          (dtache--dtach-mode 'create))
     (recompile edit-command)))
diff --git a/dtache-eshell.el b/dtache-eshell.el
index 00467cacba..5674a78810 100644
--- a/dtache-eshell.el
+++ b/dtache-eshell.el
@@ -33,6 +33,7 @@
 ;;;; Variables
 
 (defvar dtache-eshell-command nil)
+(defvar dtache-shell-session-action '(:attach dtache-shell-command-attach 
:view dtache-view-dwim))
 
 ;;;; Functions
 
@@ -56,6 +57,7 @@
   "Create a session if `dtache-eshell-command' value is t."
   (when dtache-eshell-command
     (let* ((dtache--dtach-mode 'create)
+           (dtache-session-action dtache-shell-session-action)
            (command (mapconcat #'identity
                                `(,eshell-last-command-name
                                  ,@eshell-last-arguments)
diff --git a/dtache-shell.el b/dtache-shell.el
index 9d73eb680f..c54575a9af 100644
--- a/dtache-shell.el
+++ b/dtache-shell.el
@@ -35,6 +35,8 @@
   "A list of regexps to block non-supported input.")
 (defvar dtache-shell-new-block-list '("^sudo.*")
   "A list of regexps to block from creating a session without attaching.")
+(defvar dtache-shell-session-action '(:attach dtache-shell-command-attach 
:view dtache-view-dwim)
+  "Actions for a session created with `dtache-shell'.")
 
 ;;;;; Private
 
@@ -77,19 +79,11 @@ This function also makes sure that the HISTFILE is disabled 
for local shells."
   "Create a session and attach to it unless DETACH."
   (interactive "P")
   (let* ((dtache-session-type 'shell)
+         (dtache-session-action dtache-shell-session-action)
          (dtache--dtach-mode (if detach 'new 'create))
          (comint-input-sender #'dtache-shell--create-input-sender))
     (comint-send-input)))
 
-;;;###autoload
-(defun dtache-shell-new-session ()
-  "Create a new session."
-  (interactive)
-  (let ((dtache-session-type 'shell)
-        (dtache--dtach-mode 'new)
-        (comint-input-sender #'dtache-shell--create-input-sender))
-    (comint-send-input)))
-
 ;;;###autoload
 (defun dtache-shell-attach (session)
   "Attach to SESSION.
diff --git a/dtache.el b/dtache.el
index ecf2077a74..e3dd277203 100644
--- a/dtache.el
+++ b/dtache.el
@@ -73,25 +73,19 @@
   "Interval in seconds for the update rate when tailing a session.")
 (defvar dtache-session-type nil
   "Variable to specify the origin of the session.")
-(defvar dtache-open-session-function nil
-  "Custom function to use to open a session.")
 (defvar dtache-notification-function #'dtache-inactive-session-notification
   "Variable to specify notification function when a session becomes inactive.")
-(defvar dtache-session-callback-function nil
-  "Custom function to callback when a session becomes inactive.")
-(defvar dtache-session-status-function nil
-  "Custom function to deduce the status of a session.")
 (defvar dtache-compile-hooks nil
   "Hooks to run when compiling a session.")
 (defvar dtache-metadata-annotators-alist nil
   "An alist of annotators for metadata.")
 (defvar dtache-timer-configuration '(:seconds 10 :repeat 60 :function 
run-with-timer)
   "A property list defining how often to run a timer.")
-(defvar dtache-type-open-dispatch '((shell-command . 
dtache-shell-command-attach)
-                                    (shell . dtache-shell-command-attach)
-                                    (eshell . dtache-shell-command-attach)
-                                    (compile . dtache-compile-open))
-  "How to open an active session based on type.")
+
+(defvar dtache-session-action nil
+  "A property list of actions for a session.")
+(defvar dtache-shell-command-action '(:attach dtache-shell-command-attach 
:view dtache-view-dwim)
+  "Actions for a session created with `dtache-shell-command'.")
 
 (defvar dtache-annotation-format
   `((:width 3 :function dtache--active-str :face dtache-active-face)
@@ -190,15 +184,13 @@
   (id nil :read-only t)
   (command nil :read-only t)
   (type nil :read-only t)
-  (open-function nil :read-only t)
-  (callback-function nil :read-only t)
-  (status-function nil :read-only t)
   (working-directory nil :read-only t)
   (creation-time nil :read-only t)
   (session-directory nil :read-only t)
   (metadata nil :read-only t)
   (host nil :read-only t)
   (redirect-only nil :read-only t)
+  (action nil :read-only t)
   (status nil)
   (duration nil)
   (output-size nil)
@@ -219,7 +211,8 @@ If called with prefix-argument the output is suppressed."
                                              default-directory))
                           "Dtache shell command: ")
                         nil 'dtache-shell-command-history)))
-  (let ((dtache-session-type 'shell-command))
+  (let ((dtache-session-type 'shell-command)
+        (dtache-session-action dtache-shell-command-action))
     (dtache-start-session command current-prefix-arg)))
 
 ;;;###autoload
@@ -228,10 +221,15 @@ If called with prefix-argument the output is suppressed."
   (interactive
    (list (dtache-completing-read (dtache-get-sessions))))
   (when (dtache-valid-session session)
-    (if-let ((open-function
-              (dtache--session-open-function session)))
-        (funcall open-function session)
-      (dtache-open-dwim session))))
+    (if (dtache--session-active-p session)
+        (if (dtache--session-redirect-only session)
+            (dtache-tail-output session)
+          (let ((attach (or (plist-get (dtache--session-action session) 
:attach)
+                            #'dtache-tail-output)))
+            (funcall attach session)))
+      (let ((view (or (plist-get (dtache--session-action session) :view)
+                      #'dtache-view-dwim)))
+        (funcall view session)))))
 
 ;;;###autoload
 (defun dtache-compile-session (session)
@@ -266,12 +264,7 @@ If called with prefix-argument the output is suppressed."
   (when (dtache-valid-session session)
     (let* ((default-directory
              (dtache--session-working-directory session))
-           (dtache-open-session-function
-            (dtache--session-open-function session))
-           (dtache-session-callback-function
-            (dtache--session-callback-function session))
-           (dtache-session-status-function
-            (dtache--session-status-function session)))
+           (dtache-session-action (dtache--session-action session)))
       (dtache-start-session (dtache--session-command session)))))
 
 ;;;###autoload
@@ -429,9 +422,7 @@ nil before closing."
          (dtache--session-create :id (intern (dtache--create-id command))
                                  :command command
                                  :type dtache-session-type
-                                 :open-function dtache-open-session-function
-                                 :callback-function 
dtache-session-callback-function
-                                 :status-function 
dtache-session-status-function
+                                 :action dtache-session-action
                                  :working-directory 
(dtache-get-working-directory)
                                  :redirect-only (dtache-redirect-only-p 
command)
                                  :creation-time (time-to-seconds 
(current-time))
@@ -616,16 +607,9 @@ If session is not valid trigger an automatic cleanup on 
SESSION's host."
                   ('failure "Dtache failed")) ))
     (message "%s: %s" status (dtache--session-command session))))
 
-(defun dtache-open-dwim (session)
-  "Open SESSION in a do what I mean fashion."
-  (cond ((dtache--session-active session)
-         (if (dtache--session-redirect-only session)
-             (dtache-tail-output session)
-           (if-let ((open-fun (alist-get
-                               (dtache--session-type session) 
dtache-type-open-dispatch)))
-               (funcall open-fun session)
-             (dtache-tail-output session))))
-        ((eq 'success (dtache--session-status session))
+(defun dtache-view-dwim (session)
+  "View SESSION in a do what I mean fashion."
+  (cond ((eq 'success (dtache--session-status session))
          (dtache-open-output session))
         ((eq 'failure (dtache--session-status session))
          (dtache-compile-session session))
@@ -645,6 +629,11 @@ If session is not valid trigger an automatic cleanup on 
SESSION's host."
            (dtache--dtach-mode 'attach))
       (dtache-start-session nil))))
 
+(defun dtache-delete-sessions ()
+  "Delete all `dtache' sessions."
+  (seq-do #'dtache--db-remove-entry
+          (dtache-get-sessions)))
+
 ;;;;; Other
 
 (defun dtache-start-session-monitor (session)
@@ -942,9 +931,9 @@ Optionally CONCAT the command return command into a string."
           (- (time-to-seconds) (dtache--session-creation-time session)))
 
     ;; Update status
-    (if-let ((status (dtache--session-status-function session)))
-        (setf (dtache--session-status session) (funcall status session))
-      (setf (dtache--session-status session) (dtache-session-exit-code-status 
session)))
+    (let ((status (or (plist-get (dtache--session-action session) :status)
+                      #'dtache-session-exit-code-status)))
+      (setf (dtache--session-status session) (funcall status session)))
 
     ;; Send notification
     (funcall dtache-notification-function session)
@@ -953,7 +942,7 @@ Optionally CONCAT the command return command into a string."
     (dtache--db-update-entry session t)
 
     ;; Execute callback
-    (when-let ((callback (dtache--session-callback-function session)))
+    (when-let ((callback (plist-get (dtache--session-action session) 
:callback)))
       (funcall callback session))))
 
 (defun dtache--kill-processes (pid)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]