[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)
- [nongnu] elpa/hyperdrive 472e01a805 01/19: Fix: (h/install) Use temp file in temporary-file-directory, (continued)
- [nongnu] elpa/hyperdrive 472e01a805 01/19: Fix: (h/install) Use temp file in temporary-file-directory, ELPA Syncer, 2024/08/01
- [nongnu] elpa/hyperdrive 95f814ddb7 09/19: Tidy, improve, ELPA Syncer, 2024/08/01
- [nongnu] elpa/hyperdrive 481df10760 05/19: Fix: Timer, ELPA Syncer, 2024/08/01
- [nongnu] elpa/hyperdrive 4af29a6156 06/19: Fix: Completing download, ELPA Syncer, 2024/08/01
- [nongnu] elpa/hyperdrive a2120e57e9 03/19: Fix: Size, ELPA Syncer, 2024/08/01
- [nongnu] elpa/hyperdrive 53ce1cf3f4 04/19: Fix: Wait for download to start, ELPA Syncer, 2024/08/01
- [nongnu] elpa/hyperdrive 84b59e1f8f 02/19: WIP: hyperdrive-download-monitor, ELPA Syncer, 2024/08/01
- [nongnu] elpa/hyperdrive c35423cdd4 12/19: Use special-mode, ELPA Syncer, 2024/08/01
- [nongnu] elpa/hyperdrive 015917ecd3 14/19: Fix: (h//download-monitor) Cancel timer if buffer killed, ELPA Syncer, 2024/08/01
- [nongnu] elpa/hyperdrive 4742b430fa 07/19: Fix: Killing monitor buffer, ELPA Syncer, 2024/08/01
- [nongnu] elpa/hyperdrive 95323b4bf6 08/19: Refactoring, fixes,
ELPA Syncer <=
- [nongnu] elpa/hyperdrive 35d04e8cc5 18/19: Fix: Close monitor from ready hook; ensure buffer is live, ELPA Syncer, 2024/08/01
- [nongnu] elpa/hyperdrive 4c5fc6ca76 19/19: Merge: Download monitor fixes/improvements, ELPA Syncer, 2024/08/01
- [nongnu] elpa/hyperdrive 9156b1a7ac 16/19: Fix: Ensure timer before canceling, ELPA Syncer, 2024/08/01
- [nongnu] elpa/hyperdrive 34e27bca27 10/19: Show elapsed time, ELPA Syncer, 2024/08/01
- [nongnu] elpa/hyperdrive 36744a367d 15/19: Add: Restart gateway button in monitor buffer, ELPA Syncer, 2024/08/01
- [nongnu] elpa/hyperdrive 5434098b4a 11/19: Show speed, ELPA Syncer, 2024/08/01
- [nongnu] elpa/hyperdrive 04caa99d91 13/19: Merge: Download monitor for installing gateway, ELPA Syncer, 2024/08/01
- [nongnu] elpa/hyperdrive fc3b754ca4 17/19: Fix: (hyperdrive-menu) Make install command non-transient, ELPA Syncer, 2024/08/01