Support for quieting progresses.

This commit is contained in:
Dominik Pantůček 2023-04-11 21:22:21 +02:00
parent ee47971450
commit 990340ded9

View file

@ -28,6 +28,8 @@
(module (module
progress progress
( (
*progress-quiet*
run-with-progress run-with-progress
progress-advance progress-advance
with-progress with-progress
@ -47,13 +49,17 @@
(chicken port) (chicken port)
util-time) util-time)
;; Suppress all output if #t
(define *progress-quiet* (make-parameter #f))
;; Parameterized current progress string ;; Parameterized current progress string
(define *current-progress* (make-parameter #f)) (define *current-progress* (make-parameter #f))
;; Prints current progress. ;; Prints current progress.
(define (print-current-progress . args) (define (print-current-progress . args)
(let ((cp (*current-progress*))) (let ((cp (*current-progress*)))
(when cp (when (and cp
(not (*progress-quiet*)))
(display (sprintf "\r\x1b[K~A" cp))))) (display (sprintf "\r\x1b[K~A" cp)))))
;; Adds something to current progress and refreshes the display. ;; Adds something to current progress and refreshes the display.
@ -71,13 +77,15 @@
(print-current-progress) (print-current-progress)
(let ((result (thunk))) (let ((result (thunk)))
(print-current-progress) (print-current-progress)
(when echo? (when (and echo?
(not (*progress-quiet*)))
(print post-msg)) (print post-msg))
result))) result)))
;; Allows printing output when progress is advancing. ;; Allows printing output when progress is advancing.
(define (run-progress-break thunk) (define (run-progress-break thunk)
(when (*current-progress*) (when (and (*current-progress*)
(not (*progress-quiet*)))
(display "\r\x1b[K")) (display "\r\x1b[K"))
(thunk) (thunk)
(print-current-progress)) (print-current-progress))
@ -109,6 +117,7 @@
;; Unconditionally prints the current progress. ;; Unconditionally prints the current progress.
(define (print-current-progress% . eols) (define (print-current-progress% . eols)
(when (not (*progress-quiet*))
(let ((eol (if (null? eols) (let ((eol (if (null? eols)
"" ""
(car eols)))) (car eols))))
@ -131,7 +140,7 @@
(make-string swidth #\space) (make-string swidth #\space)
ivalue% ivalue%
(*current-progress%*) (*current-progress%*)
eol)))))) eol)))))))
;; If the new value is different-enough from the current one, updates ;; If the new value is different-enough from the current one, updates
;; it and re-prints the progress% ;; it and re-prints the progress%
@ -156,7 +165,8 @@
(let ((result (thunk)) (let ((result (thunk))
(stop (current-util-milliseconds))) (stop (current-util-milliseconds)))
(print-current-progress% (sprintf " ~A ms" (- stop start))) (print-current-progress% (sprintf " ~A ms" (- stop start)))
(when echo? (when (and echo?
(not (*progress-quiet*)))
(newline)) (newline))
result)))) result))))