progress: Factorize erase-in-line.
authorLudovic Courtès <ludo@gnu.org>
Wed, 22 Nov 2017 13:05:35 +0000 (14:05 +0100)
committerLudovic Courtès <ludo@gnu.org>
Fri, 1 Dec 2017 15:00:14 +0000 (16:00 +0100)
* guix/progress.scm (erase-in-line): New procedure.
(progress-reporter/file): Use it.

guix/progress.scm

index beca2c2..1993c74 100644 (file)
@@ -153,6 +153,11 @@ width of the bar is BAR-WIDTH."
             (make-string filled #\#)
             (make-string empty #\space))))
 
+(define (erase-in-line port)
+  "Write an ANSI erase-in-line sequence to PORT to erase the whole line and
+move the cursor to the beginning of the line."
+  (display "\r\x1b[K" port))
+
 (define* (progress-reporter/file file size
                                  #:optional (log-port (current-output-port))
                                  #:key (abbreviation basename))
@@ -176,7 +181,7 @@ ABBREVIATION used to shorten FILE for display."
                                      (byte-count->string throughput)
                                      (seconds->string elapsed)
                                      (progress-bar %) %)))
-            (display "\r\x1b[K" log-port)
+            (erase-in-line log-port)
             (display (string-pad-middle left right
                                         (current-terminal-columns))
                      log-port)
@@ -188,7 +193,7 @@ ABBREVIATION used to shorten FILE for display."
                                      (byte-count->string throughput)
                                      (seconds->string elapsed)
                                      (byte-count->string transferred))))
-            (display "\r\x1b[K" log-port)
+            (erase-in-line log-port)
             (display (string-pad-middle left right
                                         (current-terminal-columns))
                      log-port)