From patchwork Sat Aug 26 06:36:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Julien Lepiller X-Patchwork-Id: 53225 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 590BD27BBE2; Sat, 26 Aug 2023 07:39:36 +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 D3C5327BBE9 for ; Sat, 26 Aug 2023 07:39:32 +0100 (BST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qZmwr-0004ok-TV; Sat, 26 Aug 2023 02:39:05 -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 1qZmwp-0004nz-IR for guix-patches@gnu.org; Sat, 26 Aug 2023 02:39:03 -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 1qZmwj-0006ny-AZ for guix-patches@gnu.org; Sat, 26 Aug 2023 02:39:02 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1qZmwo-0001ns-9j for guix-patches@gnu.org; Sat, 26 Aug 2023 02:39:02 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#65546] [PATCH] guix: Properly compute progress bar width. Resent-From: Julien Lepiller Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Sat, 26 Aug 2023 06:39:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 65546 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 65546@debbugs.gnu.org X-Debbugs-Original-To: guix-patches@gnu.org Received: via spool by submit@debbugs.gnu.org id=B.16930319216900 (code B ref -1); Sat, 26 Aug 2023 06:39:02 +0000 Received: (at submit) by debbugs.gnu.org; 26 Aug 2023 06:38:41 +0000 Received: from localhost ([127.0.0.1]:41576 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qZmwT-0001nE-DR for submit@debbugs.gnu.org; Sat, 26 Aug 2023 02:38:41 -0400 Received: from lists.gnu.org ([2001:470:142::17]:60516) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qZmwO-0001my-Sk for submit@debbugs.gnu.org; Sat, 26 Aug 2023 02:38:40 -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 1qZmwC-0004lI-FV for guix-patches@gnu.org; Sat, 26 Aug 2023 02:38:25 -0400 Received: from lepiller.eu ([2a00:5884:8208::1] helo=localhost) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qZmw8-0006VS-2Q for guix-patches@gnu.org; Sat, 26 Aug 2023 02:38:23 -0400 Received: from localhost (localhost [127.0.0.1]) by localhost (OpenSMTPD) with ESMTP id f6952dbe for ; Sat, 26 Aug 2023 06:37:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed; d=lepiller.eu; h=from:to :subject:date:message-id:mime-version:content-transfer-encoding; s=dkim; bh=1xlwRh/ihmH4yclBriW/mA/2tgGwt1xRWCBwDoc1+nA=; b=gSZN CdOqCqnfs6mn35neSTApG29XNsNQo8uC3JD0BGyQ9GoIfhd++GHkwE4pH2f4YLpx nE9TnsBSU9glTiG8E5fD0M7IWHGyszntZCN7TyCgyM9OLNp9gfQfUoVNBVTZR70Q EXH28mKFKyzDALDXGnipVqdrI3BHDKgkfwndt2V6tc4cQb8VC23wCIDy3RqzkStQ 1ROKyxtrPRT+ximrnzPcpEijdkPkGGboq2QViEokDuPFjR67cmJ1lFihDDu6/BCC PAa44i1YT8NbUBRbtBIeu06D4k1p5ktsRfZ/ZcJE6xQr2Fe0jir5mQenGOBW5GE6 i8QH4jC7/XjlRRbylg== Received: by localhost (OpenSMTPD) with ESMTPSA id e76155e3 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO) for ; Sat, 26 Aug 2023 06:37:09 +0000 (UTC) From: Julien Lepiller Date: Sat, 26 Aug 2023 08:36:55 +0200 Message-ID: <20230826063655.2227-1-julien@lepiller.eu> X-Mailer: git-send-email 2.41.0 MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:5884:8208::1; envelope-from=julien@lepiller.eu; helo=localhost X-Spam_score_int: 17 X-Spam_score: 1.7 X-Spam_bar: + X-Spam_report: (1.7 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FSL_HELO_NON_FQDN_1=0.001, HELO_LOCALHOST=3.828, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action 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/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 ( @@ -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 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)))