Subject: Re: bug#59975: [PATCH] guix: Show better progress bars.

From: Julien Lepiller <[email-masked]>
Date: Sun, 15 Jan 2023 11:12:02 +0100

Le Fri, 13 Jan 2023 18:07:42 +0100,
Ludovic Courtès <[email-masked]> a écrit :

> Hello!
> 
> Julien Lepiller <[email-masked]> skribis:
> 
> > Hi Guix!
> >
> > The attached patch is a small improvement on our progress bars.
> > Instead of our cute ASCII art:
> >
> > 1.2MiB/s 00:04 [###               ]  18.5%
> >
> > We get something a little more smooth:
> >
> > 1.1MiB/s 00:04 ▕███               ▏  17.1%
> > 1.2MiB/s 00:05 ▕███▋              ▏  20.7%
> >
> > Using unicode characters that can represent 1/8 of a character
> > width.  
> 
> Woow, fancy!  Love it!!  Too bad I was too late to have it under the
> Newtonmas tree. 🎄
> 
> > I used port-encoding to detect when the output supports unicode, but
> > maybe there's something more dedicated to figuring that out? When
> > the port encoding is not UTF-8, we fall back to the ASCII version.  
> 
> One question: how likely is it that people won’t have a font with
> those glyphs to display it correctly?
> 
> It would be good to check in xterm, Linux console with some default
> font, and GNOME/Xfce terminals with defaults.
> 
> (Works for me in xterm and in Emacs, FWIW.)

I think it's pretty common to have these characters, since they are
used by many other projects. However, testing on a tty, I can only see
the filled characters, but not the semi-filled ones, so I get something
like:

?███?     ?

What do you think?

Attached v2.
>From 7e4c8fbcc49068ce5a9a592d8daf7b0039ce6680 Mon Sep 17 00:00:00 2001
From: Julien Lepiller <[email-masked]>
Date: Sun, 11 Dec 2022 18:51:13 +0100
Subject: [PATCH] guix: Show better progress bars.

Style provides information on the characters to use before and after the
progress bar content (`[` and `]` for the ascii style), as well as the
character for filled step (`#` for ascii style).  When supported, it
provides intermediate steps.  This is used for unicode style, to show
better precision.

* guix/progress.scm (<progress-bar-style>): New record type.
(ascii-bar-style, unicode-bar-style): New variables.
(progress-bar): Draw progress depending on style.  When supported, use
unicode style.  Fall back to ascii style.
---
 guix/progress.scm | 45 ++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 38 insertions(+), 7 deletions(-)

diff --git a/guix/progress.scm b/guix/progress.scm
index 4f8e98edc0..33cf6f4a1a 100644
--- a/guix/progress.scm
+++ b/guix/progress.scm
@@ -166,16 +166,47 @@ (define current-terminal-columns
   ;; Number of columns of the terminal.
   (make-parameter 80))
 
+(define-record-type* <progress-bar-style>
+  progress-bar-style make-progress-bar-style progress-bar-style?
+  (start  progress-bar-style-start)
+  (stop   progress-bar-style-stop)
+  (filled progress-bar-style-filled)
+  (steps  progress-bar-style-steps))
+
+(define ascii-bar-style
+  (progress-bar-style
+    (start #\[)
+    (stop #\])
+    (filled #\#)
+    (steps '())))
+
+(define unicode-bar-style
+  (progress-bar-style
+    (start #\x2595)
+    (stop #\x258f)
+    (filled #\x2588)
+    (steps '(#\x258F #\x258E #\x258D #\x258C #\x258B #\x258A #\x2589))))
+
 (define* (progress-bar % #:optional (bar-width 20))
   "Return % as a string representing an ASCII-art progress bar.  The total
 width of the bar is BAR-WIDTH."
-  (let* ((bar-width (max 3 (- bar-width 2)))
-         (fraction (/ % 100))
-         (filled   (inexact->exact (floor (* fraction bar-width))))
-         (empty    (- bar-width filled)))
-    (format #f "[~a~a]"
-            (make-string filled #\#)
-            (make-string empty #\space))))
+  (let* ((bar-style (if (equal? (port-encoding (current-output-port)) "UTF-8")
+                        unicode-bar-style
+                        ascii-bar-style))
+         (bar-width (max 3 (- bar-width 2)))
+         (intermediates (+ (length (progress-bar-style-steps bar-style)) 1))
+         (step     (inexact->exact (floor (/ (* % bar-width intermediates) 100))))
+         (filled   (quotient step intermediates))
+         (intermediate
+           (list-ref (cons #f (progress-bar-style-steps bar-style))
+                     (modulo step intermediates)))
+         (empty    (- bar-width filled (if intermediate 1 0))))
+    (simple-format #f "~a~a~a~a~a"
+                   (string (progress-bar-style-start bar-style))
+                   (make-string filled (progress-bar-style-filled bar-style))
+                   (if intermediate (string intermediate) "")
+                   (make-string empty #\space)
+                   (string (progress-bar-style-stop bar-style)))))
 
 (define (erase-current-line port)
   "Write an ANSI erase-current-line sequence to PORT to erase the whole line and
-- 
2.38.1