GNU bug report logs -
#65546
[PATCH] guix: Properly compute progress bar width.
Previous Next
Reported by: Julien Lepiller <julien <at> lepiller.eu>
Date: Sat, 26 Aug 2023 06:39:02 UTC
Severity: normal
Tags: patch
Done: Julien Lepiller <julien <at> lepiller.eu>
Bug is archived. No further changes may be made.
Full log
View this message in rfc822 format
[Message part 1 (text/plain, inline)]
Your bug report
#65546: [PATCH] guix: Properly compute progress bar width.
which was filed against the guix-patches package, has been closed.
The explanation is attached below, along with your original report.
If you require more details, please reply to 65546 <at> debbugs.gnu.org.
--
65546: https://debbugs.gnu.org/cgi/bugreport.cgi?bug=65546
GNU Bug Tracking System
Contact help-debbugs <at> gnu.org with problems
[Message part 2 (message/rfc822, inline)]
Le Wed, 11 Oct 2023 23:00:07 +0200,
Ludovic Courtès <ludo <at> gnu.org> a écrit :
> Hi Julien,
>
> Julien Lepiller <julien <at> lepiller.eu> skribis:
>
> > * guix/build/syscalls.scm (terminal-width): New procedure.
> > * guix/progress.scm (progress-reporter/bar): Use it to compute
> > progress bar width.
> > * guix/git.scm (show-progress): Use it to compute progress bar
> > width.
> > * tests/syscalls.scm: Add tests.
>
> Others have already said “LGTM”, and I concur. I’ll still make a
> couple of minor suggestions but that shoudln’t stop you from going
> ahead (everyone’s waiting for it :-)).
>
> > +(define get-wchar-ffi
> > + (pointer->procedure int
> > + (dynamic-func "mbstowcs" (dynamic-link))
> > + (list '* '* size_t)))
> > +(define terminal-string-width-ffi
> > + (pointer->procedure int
> > + (dynamic-func "wcswidth" (dynamic-link))
> > + (list '* size_t)))
> > +
> > +(define (terminal-string-width str)
> > + "Return the width of a string as it would be printed on the
> > terminal. This +procedure accounts for characters that have a
> > different width than 1, such as +CJK double-width characters."
>
> I’d suggest following the style of the rest of the file, which is to
> do something like:
>
> (define terminal-string-width
> (let ((mbstowcs (syscall->procedure …))
> (wcswidth (syscall->procedure …)))
> (lambda (str)
> …)))
>
> Ideally the syscalls.scm changes would be in a commit separate from
> the progress.scm changes.
>
> Now we have the problem that OpenJDK unfortunately depends on (guix
> build syscalls), which makes this change half-of-the-world-rebuild.
> There’s another pending syscalls.scm change:
>
> https://issues.guix.gnu.org/66055
> https://issues.guix.gnu.org/66054
>
> Time to create a branch?
>
> Ludo’.
It seems OpenJDK and other packages don't depend on syscalls anymore
(and the blocking bug was marked as done), so pushed to master as
28ca80717da67f90a3b33491e9807a053cf09c2d. I tried to follow your advice
and split the patch in two. Thanks!
[Message part 3 (message/rfc822, inline)]
* guix/progress.scm (terminal-width): New procedure.
(progress-reporter/bar): Use it to compute progress bar width.
* guix/git.scm (show-progress): Use it to compute progress bar width.
---
guix/git.scm | 2 +-
guix/progress.scm | 26 +++++++++++++++++++++++++-
2 files changed, 26 insertions(+), 2 deletions(-)
diff --git a/guix/git.scm b/guix/git.scm
index dbc3b7caa7..870045cddf 100644
--- a/guix/git.scm
+++ b/guix/git.scm
@@ -153,7 +153,7 @@ (define %
;; TODO: Both should be handled & exposed by the PROGRESS-BAR API instead.
(define width
(max (- (current-terminal-columns)
- (string-length label) 7)
+ (terminal-width label) 7)
3))
(define grain
diff --git a/guix/progress.scm b/guix/progress.scm
index 33cf6f4a1a..8a84bcd1b0 100644
--- a/guix/progress.scm
+++ b/guix/progress.scm
@@ -24,6 +24,7 @@ (define-module (guix progress)
#:use-module (srfi srfi-19)
#:use-module (rnrs io ports)
#:use-module (rnrs bytevectors)
+ #:use-module (system foreign)
#:use-module (ice-9 format)
#:use-module (ice-9 match)
#:export (<progress-reporter>
@@ -47,6 +48,7 @@ (define-module (guix progress)
progress-bar
byte-count->string
current-terminal-columns
+ terminal-width
dump-port*))
@@ -166,6 +168,28 @@ (define current-terminal-columns
;; Number of columns of the terminal.
(make-parameter 80))
+(define (terminal-width str)
+ "Return the width of a string as it would be printed on the terminal. This
+procedure accounts for characters that have a different width than 1, such as
+CJK double-width characters."
+ (define get-wchar-ffi
+ (pointer->procedure int
+ (dynamic-func "mbstowcs" (dynamic-link))
+ (list '* '* size_t)))
+ (define terminal-width-ffi
+ (pointer->procedure int
+ (dynamic-func "wcswidth" (dynamic-link))
+ (list '* size_t)))
+ (define (get-wchar str)
+ (let ((wchar (make-bytevector (* (+ (string-length str) 1) 4))))
+ (get-wchar-ffi (bytevector->pointer wchar)
+ (string->pointer str)
+ (string-length str))
+ wchar))
+ (terminal-width-ffi
+ (bytevector->pointer (get-wchar str))
+ (string-length str)))
+
(define-record-type* <progress-bar-style>
progress-bar-style make-progress-bar-style progress-bar-style?
(start progress-bar-style-start)
@@ -307,7 +331,7 @@ (define (draw-bar)
(if (string-null? prefix)
(display (progress-bar ratio (current-terminal-columns)) port)
(let ((width (- (current-terminal-columns)
- (string-length prefix) 3)))
+ (terminal-width prefix) 3)))
(display prefix port)
(display " " port)
(display (progress-bar ratio width) port)))
--
2.41.0
This bug report was last modified 1 year and 193 days ago.
Previous Next
GNU bug tracking system
Copyright (C) 1999 Darren O. Benham,
1997,2003 nCipher Corporation Ltd,
1994-97 Ian Jackson.