From: Dominik Csapak <d.csapak@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [pbs-devel] [PATCH proxmox] cli/text_table: calculate correct column width for unicode characters
Date: Mon, 27 Sep 2021 14:02:50 +0200 [thread overview]
Message-ID: <20210927120250.1030610-1-d.csapak@proxmox.com> (raw)
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 <d.csapak@proxmox.com>
---
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<W: Write>(
let lines: Vec<String> = 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<W: Write>(
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!("{:<width$}", line, width = column.width));
+ text.push_str(&format!("{}{:<width$}", line, "", width = padding));
}
if !options.noborder {
--
2.30.2
next reply other threads:[~2021-09-27 12:02 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-09-27 12:02 Dominik Csapak [this message]
2021-09-28 6:06 ` [pbs-devel] applied: " Dietmar Maurer
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20210927120250.1030610-1-d.csapak@proxmox.com \
--to=d.csapak@proxmox.com \
--cc=pbs-devel@lists.proxmox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal