gnu: emacs-doom-themes: Only disable breaking compilations.
[jackhill/guix/guix.git] / guix / progress.scm
CommitLineData
8c348825
LC
1;;; GNU Guix --- Functional package management for GNU
2;;; Copyright © 2017 Sou Bunnbu <iyzsong@gmail.com>
3;;; Copyright © 2015 Steve Sprang <scs@stevesprang.com>
88bc3c89 4;;; Copyright © 2017, 2018, 2019 Ludovic Courtès <ludo@gnu.org>
0289dc14 5;;; Copyright © 2018 Clément Lassieur <clement@lassieur.org>
8c348825
LC
6;;;
7;;; This file is part of GNU Guix.
8;;;
9;;; GNU Guix is free software; you can redistribute it and/or modify it
10;;; under the terms of the GNU General Public License as published by
11;;; the Free Software Foundation; either version 3 of the License, or (at
12;;; your option) any later version.
13;;;
14;;; GNU Guix is distributed in the hope that it will be useful, but
15;;; WITHOUT ANY WARRANTY; without even the implied warranty of
16;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17;;; GNU General Public License for more details.
18;;;
19;;; You should have received a copy of the GNU General Public License
20;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
21
22(define-module (guix progress)
23 #:use-module (guix records)
24 #:use-module (srfi srfi-19)
25 #:use-module (rnrs io ports)
26 #:use-module (rnrs bytevectors)
27 #:use-module (ice-9 format)
28 #:use-module (ice-9 match)
29 #:export (<progress-reporter>
30 progress-reporter
31 make-progress-reporter
32 progress-reporter?
33 call-with-progress-reporter
34
1fafa2f5
LC
35 start-progress-reporter!
36 stop-progress-reporter!
37 progress-reporter-report!
38
8c348825
LC
39 progress-reporter/silent
40 progress-reporter/file
4cdb27af 41 progress-reporter/bar
dc0f74e5 42 progress-reporter/trace
22f06a21 43 progress-report-port
8c348825 44
fe65b559 45 display-download-progress
dc0f74e5
LC
46 erase-current-line
47 progress-bar
8c348825
LC
48 byte-count->string
49 current-terminal-columns
50
51 dump-port*))
52
53;;; Commentary:
54;;;
55;;; Helper to write progress report code for downloads, etc.
56;;;
57;;; Code:
58
59(define-record-type* <progress-reporter>
60 progress-reporter make-progress-reporter progress-reporter?
61 (start progress-reporter-start) ; thunk
62 (report progress-reporter-report) ; procedure
63 (stop progress-reporter-stop)) ; thunk
64
65(define (call-with-progress-reporter reporter proc)
66 "Start REPORTER for progress reporting, and call @code{(@var{proc} report)}
67with the resulting report procedure. When @var{proc} returns, the REPORTER is
68stopped."
69 (match reporter
70 (($ <progress-reporter> start report stop)
71 (dynamic-wind start (lambda () (proc report)) stop))))
72
1fafa2f5
LC
73(define (start-progress-reporter! reporter)
74 "Low-level procedure to start REPORTER."
75 (match reporter
76 (($ <progress-reporter> start report stop)
77 (start))))
78
cfe19684 79(define (progress-reporter-report! reporter . args)
1fafa2f5
LC
80 "Low-level procedure to lead REPORTER to emit a report."
81 (match reporter
82 (($ <progress-reporter> start report stop)
cfe19684 83 (apply report args))))
1fafa2f5
LC
84
85(define (stop-progress-reporter! reporter)
86 "Low-level procedure to stop REPORTER."
87 (match reporter
88 (($ <progress-reporter> start report stop)
89 (stop))))
90
8c348825
LC
91(define progress-reporter/silent
92 (make-progress-reporter noop noop noop))
93
94\f
95;;;
96;;; File download progress report.
97;;;
98
99(cond-expand
100 (guile-2.2
101 ;; Guile 2.2.2 has a bug whereby 'time-monotonic' objects have seconds and
102 ;; nanoseconds swapped (fixed in Guile commit 886ac3e). Work around it.
103 (define time-monotonic time-tai))
104 (else #t))
105
106(define (nearest-exact-integer x)
107 "Given a real number X, return the nearest exact integer, with ties going to
108the nearest exact even integer."
109 (inexact->exact (round x)))
110
111(define (duration->seconds duration)
112 "Return the number of seconds represented by DURATION, a 'time-duration'
113object, as an inexact number."
114 (+ (time-second duration)
115 (/ (time-nanosecond duration) 1e9)))
116
117(define (seconds->string duration)
118 "Given DURATION in seconds, return a string representing it in 'mm:ss' or
119'hh:mm:ss' format, as needed."
120 (if (not (number? duration))
121 "00:00"
122 (let* ((total-seconds (nearest-exact-integer duration))
123 (extra-seconds (modulo total-seconds 3600))
124 (num-hours (quotient total-seconds 3600))
125 (hours (and (positive? num-hours) num-hours))
126 (mins (quotient extra-seconds 60))
127 (secs (modulo extra-seconds 60)))
128 (format #f "~@[~2,'0d:~]~2,'0d:~2,'0d" hours mins secs))))
129
130(define (byte-count->string size)
131 "Given SIZE in bytes, return a string representing it in a human-readable
132way."
133 (let ((KiB 1024.)
134 (MiB (expt 1024. 2))
135 (GiB (expt 1024. 3))
136 (TiB (expt 1024. 4)))
137 (cond
138 ((< size KiB) (format #f "~dB" (nearest-exact-integer size)))
139 ((< size MiB) (format #f "~dKiB" (nearest-exact-integer (/ size KiB))))
140 ((< size GiB) (format #f "~,1fMiB" (/ size MiB)))
141 ((< size TiB) (format #f "~,2fGiB" (/ size GiB)))
142 (else (format #f "~,3fTiB" (/ size TiB))))))
143
144(define (string-pad-middle left right len)
145 "Combine LEFT and RIGHT with enough padding in the middle so that the
146resulting string has length at least LEN (it may overflow). If the string
147does not overflow, the last char in RIGHT will be flush with the LEN
148column."
149 (let* ((total-used (+ (string-length left)
150 (string-length right)))
151 (num-spaces (max 1 (- len total-used)))
152 (padding (make-string num-spaces #\space)))
153 (string-append left padding right)))
154
155(define (rate-limited proc interval)
156 "Return a procedure that will forward the invocation to PROC when the time
157elapsed since the previous forwarded invocation is greater or equal to
158INTERVAL (a time-duration object), otherwise does nothing and returns #f."
159 (let ((previous-at #f))
160 (lambda args
161 (let* ((now (current-time time-monotonic))
162 (forward-invocation (lambda ()
163 (set! previous-at now)
164 (apply proc args))))
165 (if previous-at
166 (let ((elapsed (time-difference now previous-at)))
167 (if (time>=? elapsed interval)
168 (forward-invocation)
169 #f))
170 (forward-invocation))))))
171
172(define current-terminal-columns
173 ;; Number of columns of the terminal.
174 (make-parameter 80))
175
176(define* (progress-bar % #:optional (bar-width 20))
177 "Return % as a string representing an ASCII-art progress bar. The total
178width of the bar is BAR-WIDTH."
5ed534cc
LC
179 (let* ((bar-width (max 3 (- bar-width 2)))
180 (fraction (/ % 100))
8c348825
LC
181 (filled (inexact->exact (floor (* fraction bar-width))))
182 (empty (- bar-width filled)))
183 (format #f "[~a~a]"
184 (make-string filled #\#)
185 (make-string empty #\space))))
186
3e0a4297
LC
187(define (erase-current-line port)
188 "Write an ANSI erase-current-line sequence to PORT to erase the whole line and
1252dd32
LC
189move the cursor to the beginning of the line."
190 (display "\r\x1b[K" port))
191
fe65b559
LC
192(define* (display-download-progress file size
193 #:key
194 start-time (transferred 0)
195 (log-port (current-error-port)))
196 "Write the progress report to LOG-PORT. Use START-TIME (a SRFI-19 time
197object) and TRANSFERRED (a total number of bytes) to determine the
198throughput."
199 (define elapsed
200 (duration->seconds
b6f5339d
LC
201 (time-difference (current-time (time-type start-time))
202 start-time)))
203
0289dc14 204 (if (and (number? size) (not (zero? size)))
fe65b559
LC
205 (let* ((% (* 100.0 (/ transferred size)))
206 (throughput (/ transferred elapsed))
207 (left (format #f " ~a ~a" file
208 (byte-count->string size)))
209 (right (format #f "~a/s ~a ~a~6,1f%"
210 (byte-count->string throughput)
211 (seconds->string elapsed)
212 (progress-bar %) %)))
213 (erase-current-line log-port)
214 (display (string-pad-middle left right
215 (current-terminal-columns))
216 log-port)
217 (force-output log-port))
d827fd31
CL
218 ;; If we don't know the total size, the last transfer will have a 0B
219 ;; size. Don't display it.
220 (unless (zero? transferred)
221 (let* ((throughput (/ transferred elapsed))
222 (left (format #f " ~a" file))
223 (right (format #f "~a/s ~a | ~a transferred"
224 (byte-count->string throughput)
225 (seconds->string elapsed)
226 (byte-count->string transferred))))
227 (erase-current-line log-port)
228 (display (string-pad-middle left right
229 (current-terminal-columns))
230 log-port)
231 (force-output log-port)))))
fe65b559 232
dc0f74e5
LC
233(define %progress-interval
234 ;; Default interval between subsequent outputs for rate-limited displays.
88bc3c89 235 (make-time time-duration 200000000 0))
dc0f74e5 236
8c348825
LC
237(define* (progress-reporter/file file size
238 #:optional (log-port (current-output-port))
239 #:key (abbreviation basename))
240 "Return a <progress-reporter> object to show the progress of FILE's download,
241which is SIZE bytes long. The progress report is written to LOG-PORT, with
242ABBREVIATION used to shorten FILE for display."
243 (let ((start-time (current-time time-monotonic))
244 (transferred 0))
245 (define (render)
fe65b559
LC
246 (display-download-progress (abbreviation file) size
247 #:start-time start-time
248 #:transferred transferred
249 #:log-port log-port))
8c348825
LC
250
251 (progress-reporter
252 (start render)
253 ;; Report the progress every 300ms or longer.
254 (report
dc0f74e5 255 (let ((rate-limited-render (rate-limited render %progress-interval)))
8c348825
LC
256 (lambda (value)
257 (set! transferred value)
258 (rate-limited-render))))
259 ;; Don't miss the last report.
260 (stop render))))
261
4cdb27af
LC
262(define* (progress-reporter/bar total
263 #:optional
264 (prefix "")
265 (port (current-error-port)))
266 "Return a reporter that shows a progress bar every time one of the TOTAL
267tasks is performed. Write PREFIX at the beginning of the line."
268 (define done 0)
269
270 (define (report-progress)
271 (set! done (+ 1 done))
272 (unless (> done total)
273 (let* ((ratio (* 100. (/ done total))))
3e0a4297 274 (erase-current-line port)
4cdb27af
LC
275 (if (string-null? prefix)
276 (display (progress-bar ratio (current-terminal-columns)) port)
277 (let ((width (- (current-terminal-columns)
278 (string-length prefix) 3)))
279 (display prefix port)
280 (display " " port)
281 (display (progress-bar ratio width) port)))
282 (force-output port))))
283
284 (progress-reporter
285 (start (lambda ()
286 (set! done 0)))
287 (report report-progress)
288 (stop (lambda ()
3e0a4297 289 (erase-current-line port)
4cdb27af
LC
290 (unless (string-null? prefix)
291 (display prefix port)
292 (newline port))
293 (force-output port)))))
294
dc0f74e5
LC
295(define* (progress-reporter/trace file url size
296 #:optional (log-port (current-output-port)))
297 "Like 'progress-reporter/file', but instead of returning human-readable
298progress reports, write \"build trace\" lines to be processed elsewhere."
42384b51
LC
299 (define total 0) ;bytes transferred
300
dc0f74e5
LC
301 (define (report-progress transferred)
302 (define message
303 (format #f "@ download-progress ~a ~a ~a ~a~%"
304 file url (or size "-") transferred))
305
306 (display message log-port) ;should be atomic
307 (flush-output-port log-port))
308
309 (progress-reporter
310 (start (lambda ()
42384b51 311 (set! total 0)
dc0f74e5
LC
312 (display (format #f "@ download-started ~a ~a ~a~%"
313 file url (or size "-"))
314 log-port)))
42384b51
LC
315 (report (let ((report (rate-limited report-progress %progress-interval)))
316 (lambda (transferred)
317 (set! total transferred)
318 (report transferred))))
dc0f74e5 319 (stop (lambda ()
42384b51 320 (let ((size (or size total)))
1d0be47a
LC
321 (report-progress size)
322 (display (format #f "@ download-succeeded ~a ~a ~a~%"
323 file url size)
324 log-port))))))
dc0f74e5 325
8c348825
LC
326;; TODO: replace '(@ (guix build utils) dump-port))'.
327(define* (dump-port* in out
328 #:key (buffer-size 16384)
329 (reporter progress-reporter/silent))
330 "Read as much data as possible from IN and write it to OUT, using chunks of
331BUFFER-SIZE bytes. After each successful transfer of BUFFER-SIZE bytes or
332less, report the total number of bytes transferred to the REPORTER, which
333should be a <progress-reporter> object."
334 (define buffer
335 (make-bytevector buffer-size))
336
337 (call-with-progress-reporter reporter
338 (lambda (report)
339 (let loop ((total 0)
340 (bytes (get-bytevector-n! in buffer 0 buffer-size)))
341 (or (eof-object? bytes)
342 (let ((total (+ total bytes)))
343 (put-bytevector out buffer 0 bytes)
344 (report total)
345 (loop total (get-bytevector-n! in buffer 0 buffer-size))))))))
22f06a21
LC
346
347(define (progress-report-port reporter port)
348 "Return a port that continuously reports the bytes read from PORT using
349REPORTER, which should be a <progress-reporter> object."
350 (match reporter
351 (($ <progress-reporter> start report stop)
352 (let* ((total 0)
353 (read! (lambda (bv start count)
354 (let ((n (match (get-bytevector-n! port bv start count)
355 ((? eof-object?) 0)
356 (x x))))
357 (set! total (+ total n))
358 (report total)
359 n))))
360 (start)
361 (make-custom-binary-input-port "progress-port-proc"
362 read! #f #f
363 (lambda ()
364 ;; XXX: Kludge! When used through
365 ;; 'decompressed-port', this port ends
366 ;; up being closed twice: once in a
367 ;; child process early on, and at the
368 ;; end in the parent process. Ignore
369 ;; the early close so we don't output
370 ;; a spurious "download-succeeded"
371 ;; trace.
372 (unless (zero? total)
373 (stop))
374 (close-port port)))))))
375