progress: Add 'display-download-progress'.

* guix/progress.scm (display-download-progress): New procedure.
(progress-reporter/file)[render]: Rewrite in terms of
'display-download-progress'.
This commit is contained in:
Ludovic Courtès 2018-09-25 10:30:21 +02:00
parent 363d7d6608
commit fe65b559a6
No known key found for this signature in database
GPG Key ID: 090B11993D9AEBB5
1 changed files with 41 additions and 31 deletions

View File

@ -39,6 +39,7 @@
progress-reporter/file progress-reporter/file
progress-reporter/bar progress-reporter/bar
display-download-progress
byte-count->string byte-count->string
current-terminal-columns current-terminal-columns
@ -183,24 +184,20 @@ width of the bar is BAR-WIDTH."
move the cursor to the beginning of the line." move the cursor to the beginning of the line."
(display "\r\x1b[K" port)) (display "\r\x1b[K" port))
(define* (progress-reporter/file file size (define* (display-download-progress file size
#:optional (log-port (current-output-port)) #:key
#:key (abbreviation basename)) start-time (transferred 0)
"Return a <progress-reporter> object to show the progress of FILE's download, (log-port (current-error-port)))
which is SIZE bytes long. The progress report is written to LOG-PORT, with "Write the progress report to LOG-PORT. Use START-TIME (a SRFI-19 time
ABBREVIATION used to shorten FILE for display." object) and TRANSFERRED (a total number of bytes) to determine the
(let ((start-time (current-time time-monotonic)) throughput."
(transferred 0))
(define (render)
"Write the progress report to LOG-PORT."
(define elapsed (define elapsed
(duration->seconds (duration->seconds
(time-difference (current-time time-monotonic) start-time))) (time-difference (current-time time-monotonic) start-time)))
(if (number? size) (if (number? size)
(let* ((% (* 100.0 (/ transferred size))) (let* ((% (* 100.0 (/ transferred size)))
(throughput (/ transferred elapsed)) (throughput (/ transferred elapsed))
(left (format #f " ~a ~a" (left (format #f " ~a ~a" file
(abbreviation file)
(byte-count->string size))) (byte-count->string size)))
(right (format #f "~a/s ~a ~a~6,1f%" (right (format #f "~a/s ~a ~a~6,1f%"
(byte-count->string throughput) (byte-count->string throughput)
@ -212,8 +209,7 @@ ABBREVIATION used to shorten FILE for display."
log-port) log-port)
(force-output log-port)) (force-output log-port))
(let* ((throughput (/ transferred elapsed)) (let* ((throughput (/ transferred elapsed))
(left (format #f " ~a" (left (format #f " ~a" file))
(abbreviation file)))
(right (format #f "~a/s ~a | ~a transferred" (right (format #f "~a/s ~a | ~a transferred"
(byte-count->string throughput) (byte-count->string throughput)
(seconds->string elapsed) (seconds->string elapsed)
@ -224,6 +220,20 @@ ABBREVIATION used to shorten FILE for display."
log-port) log-port)
(force-output log-port)))) (force-output log-port))))
(define* (progress-reporter/file file size
#:optional (log-port (current-output-port))
#:key (abbreviation basename))
"Return a <progress-reporter> object to show the progress of FILE's download,
which is SIZE bytes long. The progress report is written to LOG-PORT, with
ABBREVIATION used to shorten FILE for display."
(let ((start-time (current-time time-monotonic))
(transferred 0))
(define (render)
(display-download-progress (abbreviation file) size
#:start-time start-time
#:transferred transferred
#:log-port log-port))
(progress-reporter (progress-reporter
(start render) (start render)
;; Report the progress every 300ms or longer. ;; Report the progress every 300ms or longer.