From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id F1D6D6D1D5 for ; Mon, 27 Sep 2021 14:02:52 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id E26F4216CE for ; Mon, 27 Sep 2021 14:02:52 +0200 (CEST) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [94.136.29.106]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS id B2C01216C2 for ; Mon, 27 Sep 2021 14:02:51 +0200 (CEST) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 7E53044AF7 for ; Mon, 27 Sep 2021 14:02:51 +0200 (CEST) From: Dominik Csapak To: pbs-devel@lists.proxmox.com Date: Mon, 27 Sep 2021 14:02:50 +0200 Message-Id: <20210927120250.1030610-1-d.csapak@proxmox.com> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.203 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment POISEN_SPAM_PILL 0.1 Meta: its spam POISEN_SPAM_PILL_1 0.1 random spam to be learned in bayes POISEN_SPAM_PILL_3 0.1 random spam to be learned in bayes SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Subject: [pbs-devel] [PATCH proxmox] cli/text_table: calculate correct column width for unicode characters X-BeenThere: pbs-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Backup Server development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 27 Sep 2021 12:02:53 -0000 When printing unicode text, a glyph can take up more (or less) space than a single column. To handle that, use the 'unicode-width' crate which calculates the width by the unicode standard. This makes the text tables correctly aligned when printing unicode characters (e.g. in a datastore/user/syncjob comment). 'unicode-width' is used itself in the rust compiler to format errors (see e.g. the Cargo.toml in /compiler/rustc_errors of the rust git) Signed-off-by: Dominik Csapak --- not strictly necessary, but was easy enough to do, makes printing with unicode characters nicer and only needs one dependency which must be available anyway... proxmox/Cargo.toml | 1 + proxmox/src/api/cli/text_table.rs | 8 +++++--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/proxmox/Cargo.toml b/proxmox/Cargo.toml index b901969..73da51c 100644 --- a/proxmox/Cargo.toml +++ b/proxmox/Cargo.toml @@ -17,6 +17,7 @@ anyhow = "1.0" lazy_static = "1.4" libc = "0.2" nix = "0.19.1" +unicode-width ="0.1.8" # tools module: base32 = { version = "0.4", optional = true } diff --git a/proxmox/src/api/cli/text_table.rs b/proxmox/src/api/cli/text_table.rs index 84d9f34..d136629 100644 --- a/proxmox/src/api/cli/text_table.rs +++ b/proxmox/src/api/cli/text_table.rs @@ -2,6 +2,7 @@ use std::io::Write; use anyhow::*; use serde_json::Value; +use unicode_width::UnicodeWidthStr; use crate::api::schema::*; @@ -472,7 +473,7 @@ fn format_table( let lines: Vec = text .lines() .map(|line| { - let width = line.chars().count(); + let width = UnicodeWidthStr::width(line); if width > max_width { max_width = width; } @@ -564,10 +565,11 @@ fn render_table( text.push(' '); } + let padding = column.width - UnicodeWidthStr::width(line.as_str()); if column.right_align { - text.push_str(&format!("{:>width$}", line, width = column.width)); + text.push_str(&format!("{:>width$}{}", "", line, width = padding)); } else { - text.push_str(&format!("{: