guix-patches
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[bug#65546] [PATCH] guix: Properly compute progress bar width.


From: Ludovic Courtès
Subject: [bug#65546] [PATCH] guix: Properly compute progress bar width.
Date: Sat, 09 Sep 2023 15:48:23 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux)

Hi Julien,

Julien Lepiller <julien@lepiller.eu> skribis:

> * 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.

[...]

> +(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)))

Neat!  However, could you move it to (guix build syscalls), next to
‘terminal-columns’ (making sure ‘pointer->procedure’ is called only once
rather than once per call), and with a test or two in
‘tests/syscalls.scm’?

For clarity, perhaps rename it to ‘terminal-string-width’?

TIA!

Ludo’.





reply via email to

[Prev in Thread] Current Thread [Next in Thread]