Use progress-advance.

This commit is contained in:
Dominik Pantůček 2023-03-20 16:16:40 +01:00
parent ce348ae901
commit 08ad923f6a
3 changed files with 43 additions and 38 deletions

View file

@ -23,7 +23,7 @@
;; CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
;;
(declare (unit configuration))
(declare (unit progress))
(module
progress
@ -43,26 +43,31 @@
;; Prints current progress.
(define (print-current-progress . args)
(display (sprintf "\r\x1b[K~A" (*current-progress*))))
(let ((cp (*current-progress*)))
(when cp
(display (sprintf "\r\x1b[K~A" cp)))))
;; Adds something to current progress and refreshes the display.
(define (progress-advance str)
(*current-progress* (string-append (*current-progress*) (sprintf "~A" str)))
(print-current-progress))
(when (*current-progress*)
(*current-progress* (string-append (*current-progress*) (sprintf "~A" str)))
(print-current-progress)))
;; Runs given procedure within progress environment
(define (run-with-progress pre-msg post-msg thunk)
(parameterize ((*current-progress* pre-msg))
(define (run-with-progress echo? pre-msg post-msg thunk)
(parameterize ((*current-progress* (if echo? pre-msg #f)))
(print-current-progress)
(thunk)
(print-current-progress)
(print post-msg)))
(let ((result (thunk)))
(print-current-progress)
(when echo?
(print post-msg))
result)))
;; Friendly syntax wrapper.
(define-syntax with-progress
(syntax-rules ()
((_ pre post body ...)
(run-with-progress pre post (lambda () body ...)))))
((_ echo? pre post body ...)
(run-with-progress echo? pre post (lambda () body ...)))))
;; If the program uses progress module, disable buffering
(set-buffering-mode! (current-output-port) #:none)