From 696b2cda4afee607540336c688568bcfff97deac Mon Sep 17 00:00:00 2001 From: Maxim Zhiburt Date: Tue, 15 Aug 2023 03:07:34 +0300 Subject: [PATCH] nu-table: Fix padding 0 width issues (#10011) close #10001 cc: @fdncred @amtoine note: make sure you rebase/squash --------- Signed-off-by: Maxim Zhiburt --- crates/nu-command/tests/commands/table.rs | 20 +++---- crates/nu-table/src/table.rs | 71 ++++++++++++----------- 2 files changed, 46 insertions(+), 45 deletions(-) diff --git a/crates/nu-command/tests/commands/table.rs b/crates/nu-command/tests/commands/table.rs index e2c8b6f93a..66e2cdb045 100644 --- a/crates/nu-command/tests/commands/table.rs +++ b/crates/nu-command/tests/commands/table.rs @@ -2596,16 +2596,16 @@ fn table_expand_padding_not_default() { let actual = nu!("$env.config.table.padding = 5; [[a b, c]; [1 2 3] [4 5 [1 2 3]]] | table -e"); assert_eq!( actual.out, - "╭───────────┬───────────┬───────────┬───────────────────────────────────╮\ - │ # │ a │ b │ c │\ - ├───────────┼───────────┼───────────┼───────────────────────────────────┤\ - │ 0 │ 1 │ 2 │ 3 │\ - │ 1 │ 4 │ 5 │ ╭───────────┬───────────╮ │\ - │ │ │ │ │ 0 │ 1 │ │\ - │ │ │ │ │ 1 │ 2 │ │\ - │ │ │ │ │ 2 │ 3 │ │\ - │ │ │ │ ╰───────────┴───────────╯ │\ - ╰───────────┴───────────┴───────────┴───────────────────────────────────╯" + "╭─────────────┬─────────────┬─────────────┬────────────────────────────────────╮\ + │ # │ a │ b │ c │\ + ├─────────────┼─────────────┼─────────────┼────────────────────────────────────┤\ + │ 0 │ 1 │ 2 │ 3 │\ + │ 1 │ 4 │ 5 │ ╭───────────┬───────────╮ │\ + │ │ │ │ │ 0 │ 1 │ │\ + │ │ │ │ │ 1 │ 2 │ │\ + │ │ │ │ │ 2 │ 3 │ │\ + │ │ │ │ ╰───────────┴───────────╯ │\ + ╰─────────────┴─────────────┴─────────────┴────────────────────────────────────╯" ); } diff --git a/crates/nu-table/src/table.rs b/crates/nu-table/src/table.rs index bf81beea23..ee69587ac3 100644 --- a/crates/nu-table/src/table.rs +++ b/crates/nu-table/src/table.rs @@ -26,13 +26,13 @@ use tabled::{ /// NuTable is a table rendering implementation. #[derive(Debug, Clone)] pub struct NuTable { - data: NuTableData, + data: NuRecords, styles: Styles, alignments: Alignments, indent: (usize, usize), } -type NuTableData = VecRecords; +pub type NuRecords = VecRecords; pub type NuTableCell = CellInfo; #[derive(Debug, Default, Clone)] @@ -158,7 +158,7 @@ impl NuTable { /// Return a total table width. pub fn total_width(&self, config: &NuTableConfig) -> usize { let config = get_config(&config.theme, false, None); - let widths = build_width(&self.data); + let widths = build_width(&self.data, self.indent.0 + self.indent.1); get_total_width2(&widths, &config) } } @@ -200,7 +200,7 @@ impl Default for NuTableConfig { } fn build_table( - mut data: NuTableData, + mut data: NuRecords, cfg: NuTableConfig, alignments: Alignments, styles: Styles, @@ -211,7 +211,8 @@ fn build_table( return Some(String::new()); } - let widths = maybe_truncate_columns(&mut data, &cfg.theme, termwidth); + let pad = indent.0 + indent.1; + let widths = maybe_truncate_columns(&mut data, &cfg.theme, termwidth, pad); if widths.is_empty() { return None; } @@ -224,7 +225,7 @@ fn build_table( } fn draw_table( - data: NuTableData, + data: NuRecords, alignments: Alignments, styles: Styles, widths: Vec, @@ -437,9 +438,10 @@ fn table_trim_columns( } fn maybe_truncate_columns( - data: &mut NuTableData, + data: &mut NuRecords, theme: &TableTheme, termwidth: usize, + pad: usize, ) -> Vec { const TERMWIDTH_THRESHOLD: usize = 120; @@ -449,20 +451,21 @@ fn maybe_truncate_columns( truncate_columns_by_content }; - truncate(data, theme, termwidth) + truncate(data, theme, pad, termwidth) } // VERSION where we are showing AS LITTLE COLUMNS AS POSSIBLE but WITH AS MUCH CONTENT AS POSSIBLE. fn truncate_columns_by_content( - data: &mut NuTableData, + data: &mut NuRecords, theme: &TableTheme, + pad: usize, termwidth: usize, ) -> Vec { const MIN_ACCEPTABLE_WIDTH: usize = 3; const TRAILING_COLUMN_WIDTH: usize = 5; let config = get_config(theme, false, None); - let mut widths = build_width(&*data); + let mut widths = build_width(&*data, pad); let total_width = get_total_width2(&widths, &config); if total_width <= termwidth { return widths; @@ -516,7 +519,7 @@ fn truncate_columns_by_content( if can_be_squeezed { push_empty_column(data); - widths.push(3 + 2); + widths.push(3 + pad); } else { if data.count_columns() == 1 { return vec![]; @@ -525,7 +528,7 @@ fn truncate_columns_by_content( truncate_columns(data, data.count_columns() - 1); push_empty_column(data); widths.pop(); - widths.push(3 + 2); + widths.push(3 + pad); } widths @@ -533,15 +536,16 @@ fn truncate_columns_by_content( // VERSION where we are showing AS MANY COLUMNS AS POSSIBLE but as a side affect they MIGHT CONTAIN AS LITTLE CONTENT AS POSSIBLE fn truncate_columns_by_columns( - data: &mut NuTableData, + data: &mut NuRecords, theme: &TableTheme, + pad: usize, termwidth: usize, ) -> Vec { - const ACCEPTABLE_WIDTH: usize = 10 + 2; - const TRAILING_COLUMN_WIDTH: usize = 3 + 2; + let acceptable_width = 10 + pad; + let trailing_column_width = 3 + pad; let config = get_config(theme, false, None); - let mut widths = build_width(&*data); + let mut widths = build_width(&*data, pad); let total_width = get_total_width2(&widths, &config); if total_width <= termwidth { return widths; @@ -550,7 +554,7 @@ fn truncate_columns_by_columns( let widths_total = widths.iter().sum::(); let min_widths = widths .iter() - .map(|w| min(*w, ACCEPTABLE_WIDTH)) + .map(|w| min(*w, acceptable_width)) .sum::(); let mut min_total = total_width - widths_total + min_widths; @@ -564,7 +568,7 @@ fn truncate_columns_by_columns( i += 1; let column = data.count_columns() - 1 - i; - let width = min(widths[column], ACCEPTABLE_WIDTH); + let width = min(widths[column], acceptable_width); min_total -= width; if config.get_borders().has_vertical() { @@ -585,9 +589,9 @@ fn truncate_columns_by_columns( // Append columns with a trailing column let diff = termwidth - min_total; - if diff > TRAILING_COLUMN_WIDTH { + if diff > trailing_column_width { push_empty_column(data); - widths.push(3 + 2); + widths.push(3 + pad); } else { if data.count_columns() == 1 { return vec![]; @@ -596,7 +600,7 @@ fn truncate_columns_by_columns( truncate_columns(data, data.count_columns() - 1); push_empty_column(data); widths.pop(); - widths.push(3 + 2); + widths.push(3 + pad); } widths @@ -631,7 +635,7 @@ fn get_config(theme: &TableTheme, with_header: bool, color: Option