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

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

[nongnu] elpa/hyperdrive 95323b4bf6 08/19: Refactoring, fixes


From: ELPA Syncer
Subject: [nongnu] elpa/hyperdrive 95323b4bf6 08/19: Refactoring, fixes
Date: Thu, 1 Aug 2024 01:00:11 -0400 (EDT)

branch: elpa/hyperdrive
commit 95323b4bf65abf484d59319352a1a4f85b91bd47
Author: Adam Porter <adam@alphapapa.net>
Commit: Adam Porter <adam@alphapapa.net>

    Refactoring, fixes
---
 hyperdrive-download-monitor.el | 72 ++++++++++++++++++------------------------
 hyperdrive.el                  | 23 ++++++++------
 2 files changed, 44 insertions(+), 51 deletions(-)

diff --git a/hyperdrive-download-monitor.el b/hyperdrive-download-monitor.el
index f00f4b461e..2fa5bc35c3 100644
--- a/hyperdrive-download-monitor.el
+++ b/hyperdrive-download-monitor.el
@@ -36,52 +36,42 @@
 
 ;;;; Functions
 
-(cl-defun h/download-monitor
-    (&key buffer-name path total-size setup-fn canceled-fn
-          (update-interval 1) (completed-fn
-                               (lambda (buffer)
-                                 (let ((buffer-window (get-buffer-window 
buffer)))
-                                   (when buffer-window
-                                     (quit-window nil buffer-window))
-                                   (kill-buffer buffer)))))
-  "Return buffer that monitors the download to PATH.
-COMPLETED-FN is called with the monitor buffer as its argument."
-  (let* ((buffer (generate-new-buffer buffer-name)))
+(cl-defun h//download-monitor
+    (&key buffer-name path total-size preamble (update-interval 1))
+  "Return buffer that monitors the download to PATH expecting it to be 
TOTAL-SIZE.
+Buffer's name is based on BUFFER-NAME.  PREAMBLE is inserted at
+the top of the buffer.  Monitor buffer is updated every
+UPDATE-INTERVAL seconds."
+  (let ((buffer (generate-new-buffer buffer-name)))
     (with-current-buffer buffer
       (setf (map-elt h/download-monitor-etc :path) path
             (map-elt h/download-monitor-etc :total-size) total-size
-            (map-elt h/download-monitor-etc :completed-fn) completed-fn
-            (map-elt h/download-monitor-etc :canceled-fn) canceled-fn)
-      (h/download-monitor-update buffer)
-      (setf (map-elt h/download-monitor-etc 'timer)
-            (run-at-time update-interval update-interval
-                         #'h/download-monitor-update buffer))
-      (when setup-fn
-        (funcall setup-fn)))
+            (map-elt h/download-monitor-etc :preamble) preamble
+            (map-elt h/download-monitor-etc :timer)
+            (run-at-time nil update-interval #'h//download-monitor-update 
buffer)))
     buffer))
 
-(defun h/download-monitor-update (buffer)
-  (let (completedp)
-    (with-current-buffer buffer
-      (pcase-let* (((map :path :total-size) h/download-monitor-etc))
-        (if (file-exists-p path)
-            (let* ((attributes (file-attributes path))
-                   (current-size (file-attribute-size attributes)))
-              (if (= current-size total-size)
-                  ;; Download complete.
-                  (setf completedp t)
-                ;; Download in progress: update buffer.
-                (erase-buffer)
-                (insert "Downloading:\n\n"
-                        "File: " path "\n"
-                        "Downloaded: " (file-size-human-readable current-size 
nil " ")
-                        " / " (file-size-human-readable total-size) "\n")))
-          ;; Download completed or canceled.
-          (setf completedp t))))
-    (when completedp
-      ;; FIXME: We just assume here that it completed and wasn't canceled.
-      (when-let ((completed-fn (map-elt h/download-monitor-etc :completed-fn)))
-        (funcall completed-fn buffer)))))
+(defun h//download-monitor-update (buffer)
+  (with-current-buffer buffer
+    (pcase-let (((map :preamble :path :total-size) h/download-monitor-etc))
+      ;; TODO: Consider using `format-spec'.
+      (when (file-exists-p path)
+        (let* ((attributes (file-attributes path))
+               (current-size (file-attribute-size attributes)))
+          (erase-buffer)
+          (insert preamble "\n\n"
+                  "File: " path "\n"
+                  "Downloaded: " (file-size-human-readable current-size nil " 
")
+                  " / " (file-size-human-readable total-size) "\n"))))))
+
+(defun h//download-monitor-close (buffer)
+  "Close download monitor BUFFER."
+  (with-current-buffer buffer
+    (cancel-timer (map-elt h/download-monitor-etc :timer)))
+  (let ((buffer-window (get-buffer-window buffer)))
+    (when buffer-window
+      (quit-window nil buffer-window)))
+  (kill-buffer buffer))
 
 (provide 'hyperdrive-download-monitor)
 
diff --git a/hyperdrive.el b/hyperdrive.el
index 852f52f448..5715b69769 100644
--- a/hyperdrive.el
+++ b/hyperdrive.el
@@ -67,6 +67,7 @@
 
 (require 'hyperdrive-lib)
 (require 'hyperdrive-org)
+(require 'hyperdrive-download-monitor)
 
 ;;;; Links
 
@@ -1448,12 +1449,18 @@ If FORCEP, don't prompt for confirmation before 
downloading."
              (cl-parse-integer
               (alist-get 'content-length (plz-response-headers response)))))
          (download (url sha256)
-           (let ((temp-file (make-temp-name
-                             (expand-file-name "hyperdrive-gateway-"
-                                               temporary-file-directory))))
+           (let* ((temp-file (make-temp-name
+                              (expand-file-name "hyperdrive-gateway-"
+                                                temporary-file-directory)))
+                  (monitor-buffer (h//download-monitor
+                                   :preamble "Downloading gateway..."
+                                   :buffer-name "*hyperdrive-install*"
+                                   :path temp-file
+                                   :total-size size)))
              (setf h/install-process
                    (plz 'get url :as `(file ,temp-file) :timeout nil
                      :then (lambda (filename)
+                             (h//download-monitor-close monitor-buffer)
                              (check filename sha256 url))
                      :else (lambda (plz-error)
                              (pcase (plz-error-curl-error plz-error)
@@ -1466,19 +1473,15 @@ If FORCEP, don't prompt for confirmation before 
downloading."
                                            url plz-error)
                                 (try)))
                              (when (file-exists-p temp-file)
-                               (delete-file temp-file)))))
-             (require 'hyperdrive-download-monitor)
+                               (delete-file temp-file))
+                             (h//download-monitor-close monitor-buffer))))
              ;; Wait for download to start before showing monitor.
              (cl-loop until (file-exists-p temp-file)
                       do (sleep-for 0.1)
                       for times below 100
                       finally (unless (file-exists-p temp-file)
                                 (error "Download not started after 10 
seconds")))
-             (pop-to-buffer
-              (h/download-monitor :buffer-name "*hyperdrive-install*"
-                                  :path temp-file
-                                  :total-size size
-                                  :completed-fn #'kill-buffer-and-window))
+             (pop-to-buffer monitor-buffer)
              (h/message "Downloading %s from %S to %S"
                         (file-size-human-readable size) url destination)))
          (check (file-name sha256 url)



reply via email to

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