all messages for Guix-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* [bug#59975] [PATCH] guix: Show better progress bars.
@ 2022-12-11 18:00 Julien Lepiller
  2022-12-21 16:20 ` Julien Lepiller
                   ` (2 more replies)
  0 siblings, 3 replies; 12+ messages in thread
From: Julien Lepiller @ 2022-12-11 18:00 UTC (permalink / raw)
  To: 59975

[-- Attachment #1: Type: text/plain, Size: 603 bytes --]

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.

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.

Thoughts?

[-- Attachment #2: 0001-guix-Show-better-progress-bars.patch --]
[-- Type: text/x-patch, Size: 2794 bytes --]

From c428c80fd628797ae80029a0a22678ef55c68d6c Mon Sep 17 00:00:00 2001
From: Julien Lepiller <julien@lepiller.eu>
Date: Sun, 11 Dec 2022 18:51:13 +0100
Subject: [PATCH] guix: Show better progress bars.

* guix/progress.scm (progress-bar): When supported, use unicode variant.
---
 guix/progress.scm | 43 ++++++++++++++++++++++++++++++++++++-------
 1 file changed, 36 insertions(+), 7 deletions(-)

diff --git a/guix/progress.scm b/guix/progress.scm
index 4f8e98edc0..527bf72839 100644
--- a/guix/progress.scm
+++ b/guix/progress.scm
@@ -166,16 +166,45 @@ (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
+          (default #\x2595))
+  (stop   progress-bar-style-stop
+          (default #\x258f))
+  (filled progress-bar-style-filled
+          (default #\x2588))
+  (steps  progress-bar-style-steps
+          (default '(#\x258F #\x258E #\x258D #\x258C #\x258B #\x258A #\x2589))))
+
+(define unicode-bar-style (progress-bar-style))
+(define ascii-bar-style
+  (progress-bar-style
+    (start #\[)
+    (stop #\])
+    (filled #\#)
+    (steps '())))
+
 (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))))
+    (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


^ permalink raw reply related	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2023-02-19  9:14 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-12-11 18:00 [bug#59975] [PATCH] guix: Show better progress bars Julien Lepiller
2022-12-21 16:20 ` Julien Lepiller
2023-01-06 17:45   ` Simon Tournier
2023-01-13 17:07 ` Ludovic Courtès
2023-01-15 10:12   ` Julien Lepiller
2023-01-17  9:06     ` Ludovic Courtès
2023-01-27 18:05       ` Simon Tournier
2023-02-03 11:56 ` [bug#59975] [PATCH v3 0/2] " Julien Lepiller
2023-02-03 11:56   ` [bug#59975] [PATCH v3 1/2] gnu: Use unifont by default in TTYs Julien Lepiller
2023-02-03 11:56   ` [bug#59975] [PATCH v3 2/2] guix: Show better progress bars Julien Lepiller
2023-02-12 10:02   ` [bug#59975] [PATCH v3 0/2] " Christopher Baines
2023-02-19  9:11     ` bug#59975: " Julien Lepiller

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/guix.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.