From 7a6145342d3230200070c6edb9a87e552ad94882 Mon Sep 17 00:00:00 2001 From: objz Date: Mon, 24 Aug 2026 19:14:49 +0200 Subject: [PATCH] fix(cli): pad table columns by display width Byte-length padding misaligned columns for non-ASCII names; widths and padding now use display width via ratatui's Span. --- src/cli/output.rs | 25 ++++++++++++++++++------- src/cli/tests/output.rs | 21 +++++++++++++++++++++ 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/src/cli/output.rs b/src/cli/output.rs index 18f9f84..8041220 100644 --- a/src/cli/output.rs +++ b/src/cli/output.rs @@ -1,6 +1,10 @@ +// SPDX-FileCopyrightText: 2026 Constantin Bauer +// SPDX-License-Identifier: GPL-3.0-only + // plain-text table rendering for CLI output. no fancy box-drawing, // just left-aligned columns with two-space gaps. keeps it pipeable. use chrono::{DateTime, Utc}; +use ratatui::text::Span; pub fn print_table(headers: &[&str], rows: &[Vec]) { print!("{}", render_table(headers, rows)); @@ -37,9 +41,15 @@ pub fn active_marker(active: bool) -> &'static str { if active { ">" } else { " " } } +// display width (not byte length): instance and pack names are often +// non-ASCII, and padding by bytes misaligns every row below them. +fn display_width(value: &str) -> usize { + Span::raw(value).width() +} + // find the widest value in each column to pad everything evenly fn column_widths(headers: &[&str], rows: &[Vec]) -> Vec { - let mut widths: Vec = headers.iter().map(|header| header.len()).collect(); + let mut widths: Vec = headers.iter().map(|header| display_width(header)).collect(); for row in rows { if row.len() > widths.len() { @@ -47,7 +57,7 @@ fn column_widths(headers: &[&str], rows: &[Vec]) -> Vec { } for (index, value) in row.iter().enumerate() { - widths[index] = widths[index].max(value.len()); + widths[index] = widths[index].max(display_width(value)); } } @@ -59,11 +69,12 @@ fn render_row(row: &[String], widths: &[usize]) -> String { .iter() .enumerate() .map(|(index, width)| { - format!( - "{:>() .join(" ") diff --git a/src/cli/tests/output.rs b/src/cli/tests/output.rs index 2e00a4d..860afea 100644 --- a/src/cli/tests/output.rs +++ b/src/cli/tests/output.rs @@ -1,3 +1,6 @@ +// SPDX-FileCopyrightText: 2026 Constantin Bauer +// SPDX-License-Identifier: GPL-3.0-only + use super::{format_datetime, render_table}; use chrono::{TimeZone, Utc}; @@ -18,6 +21,24 @@ fn render_table_aligns_columns() { assert_eq!(lines[3], "Longer Name off "); } +#[test] +fn render_table_aligns_multibyte_names() { + // padding must use display width, not byte length: "Öctra" is 6 bytes + // but 5 columns wide, and byte-based padding pushed every later column. + let rendered = render_table( + &["Name", "State"], + &[ + vec!["Öctra".to_string(), "enabled".to_string()], + vec!["Longer Name".to_string(), "off".to_string()], + ], + ); + + let lines: Vec<&str> = rendered.lines().collect(); + assert_eq!(lines[0], "Name State "); + assert_eq!(lines[2], "Öctra enabled"); + assert_eq!(lines[3], "Longer Name off "); +} + #[test] fn formats_datetime_consistently() { let dt = Utc.with_ymd_and_hms(2024, 1, 2, 3, 4, 5).unwrap();