From patchwork Sat Sep 9 17:20:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Julien Lepiller X-Patchwork-Id: 53694 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id 979B527BBE9; Sat, 9 Sep 2023 18:22:24 +0100 (BST) X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-2.7 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,SPF_HELO_PASS,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.6 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id 74D4A27BBE2 for ; Sat, 9 Sep 2023 18:22:19 +0100 (BST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qf1ej-0003hy-MB; Sat, 09 Sep 2023 13:22:01 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qf1eh-0003hU-Gw for guix-patches@gnu.org; Sat, 09 Sep 2023 13:21:59 -0400 Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qf1eg-0002VB-V7 for guix-patches@gnu.org; Sat, 09 Sep 2023 13:21:59 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1qf1ej-00056l-Ij for guix-patches@gnu.org; Sat, 09 Sep 2023 13:22:01 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#65546] [PATCH v2] guix: Properly compute progress bar width. References: <20230826063655.2227-1-julien@lepiller.eu> In-Reply-To: <20230826063655.2227-1-julien@lepiller.eu> Resent-From: Julien Lepiller Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Sat, 09 Sep 2023 17:22:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 65546 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 65546@debbugs.gnu.org Received: via spool by 65546-submit@debbugs.gnu.org id=B65546.169428007119579 (code B ref 65546); Sat, 09 Sep 2023 17:22:01 +0000 Received: (at 65546) by debbugs.gnu.org; 9 Sep 2023 17:21:11 +0000 Received: from localhost ([127.0.0.1]:48426 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qf1du-00055h-GZ for submit@debbugs.gnu.org; Sat, 09 Sep 2023 13:21:11 -0400 Received: from lepiller.eu ([89.234.186.109]:38870 helo=localhost) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qf1dn-00054p-Rw for 65546@debbugs.gnu.org; Sat, 09 Sep 2023 13:21:08 -0400 Received: from localhost (localhost [127.0.0.1]) by localhost (OpenSMTPD) with ESMTP id b9fbc2d9 for <65546@debbugs.gnu.org>; Sat, 9 Sep 2023 17:20:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed; d=lepiller.eu; h=from:to :subject:date:message-id:mime-version:content-type :content-transfer-encoding; s=dkim; bh=LdVX5+Zv4Rb6amxuYAWgI3Jlg YdOr/3k39cfQLfcH6w=; b=LJcrGwU2VC6XS58O0qsgk1ryi+al8UjYeQoAPvrRG iWC5eRDpNMNn8e9t0JvBOSF/2zomItRZq1kXhG+cWkTpDvzyg3kE7HuLUKAB9YzK 5HkHyjgIiPSjkijfuAQugdP2QZdT8cgYR515h7OQgcr3vgLLIfVAXi2nn1W/CCKT mELSKiK/VE7z/bkGkJpNNCeQaTiYJWJnrP6m0TMSYsba8nkfJLRRWjf4MyFrW9nJ 2zGzpaoP0adiNuZqzDyR89S+2ZC+QfFeTj8nhbVuAjkxej/KG80LBLIP/p7IjrlT bIk/N+taMsFrei1MlJPbFWmpl7OdU6k99UKoodjiTBIhg== Received: by localhost (OpenSMTPD) with ESMTPSA id b600ca53 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO) for <65546@debbugs.gnu.org>; Sat, 9 Sep 2023 17:20:57 +0000 (UTC) From: Julien Lepiller Date: Sat, 9 Sep 2023 19:20:42 +0200 Message-ID: <20230909172047.6254-1-julien@lepiller.eu> X-Mailer: git-send-email 2.41.0 MIME-Version: 1.0 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org Sender: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org X-getmail-retrieved-from-mailbox: Patches * 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. --- guix/build/syscalls.scm | 24 ++++++++++++++++++++++++ guix/git.scm | 4 +++- guix/progress.scm | 5 ++++- tests/syscalls.scm | 6 ++++++ 4 files changed, 37 insertions(+), 2 deletions(-) diff --git a/guix/build/syscalls.scm b/guix/build/syscalls.scm index d947b010d3..a1365cc68c 100644 --- a/guix/build/syscalls.scm +++ b/guix/build/syscalls.scm @@ -192,6 +192,7 @@ (define-module (guix build syscalls) terminal-window-size terminal-columns terminal-rows + terminal-string-width openpty login-tty @@ -2335,6 +2336,29 @@ (define* (terminal-rows #:optional (port (current-output-port))) always a positive integer." (terminal-dimension window-size-rows port (const 25))) +(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." + (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-string-width-ffi + (bytevector->pointer (get-wchar str)) + (string-length str))) + (define openpty (let ((proc (syscall->procedure int "openpty" '(* * * * *) #:library "libutil"))) diff --git a/guix/git.scm b/guix/git.scm index 1cb87a4560..728b761e62 100644 --- a/guix/git.scm +++ b/guix/git.scm @@ -29,6 +29,8 @@ (define-module (guix git) #:use-module (gcrypt hash) #:use-module ((guix build utils) #:select (mkdir-p delete-file-recursively)) + #:use-module ((guix build syscalls) + #:select (terminal-string-width)) #:use-module (guix store) #:use-module (guix utils) #:use-module (guix records) @@ -153,7 +155,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-string-width label) 7) 3)) (define grain diff --git a/guix/progress.scm b/guix/progress.scm index 33cf6f4a1a..e7cf7e168a 100644 --- a/guix/progress.scm +++ b/guix/progress.scm @@ -21,9 +21,12 @@ (define-module (guix progress) #:use-module (guix records) + #:use-module ((guix build syscalls) + #:select (terminal-string-width)) #: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 ( @@ -307,7 +310,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-string-width prefix) 3))) (display prefix port) (display " " port) (display (progress-bar ratio width) port))) diff --git a/tests/syscalls.scm b/tests/syscalls.scm index c9e011f453..eb85b358c4 100644 --- a/tests/syscalls.scm +++ b/tests/syscalls.scm @@ -583,6 +583,12 @@ (define perform-container-tests? (test-assert "terminal-rows" (> (terminal-rows) 0)) +(test-assert "terminal-string-width English" + (= (terminal-string-width "hello") 5)) + +(test-assert "terminal-string-width Japanese" + (= (terminal-string-width "今日は") 6)) + (test-assert "openpty" (let ((head inferior (openpty))) (and (integer? head) (integer? inferior)