diff --git a/crates/nu-command/tests/commands/table.rs b/crates/nu-command/tests/commands/table.rs index 9c4f34ebfd..16d5eb54ab 100644 --- a/crates/nu-command/tests/commands/table.rs +++ b/crates/nu-command/tests/commands/table.rs @@ -2889,3 +2889,9 @@ fn table_list() { let actual = nu!("table --list --theme basic"); assert_eq!(actual.out, "╭────┬────────────────╮│ 0 │ basic ││ 1 │ compact ││ 2 │ compact_double ││ 3 │ default ││ 4 │ heavy ││ 5 │ light ││ 6 │ none ││ 7 │ reinforced ││ 8 │ rounded ││ 9 │ thin ││ 10 │ with_love ││ 11 │ psql ││ 12 │ markdown ││ 13 │ dots ││ 14 │ restructured ││ 15 │ ascii_rounded ││ 16 │ basic_compact │╰────┴────────────────╯"); } + +#[test] +fn table_kv_header_on_separator_trim_algorithm() { + let actual = nu!("$env.config.table.header_on_separator = true; {key1: '111111111111111111111111111111111111111111111111111111111111'} | table --width=60 --theme basic"); + assert_eq!(actual.out, "+------+---------------------------------------------------+| key1 | 1111111111111111111111111111111111111111111111111 || | 11111111111 |+------+---------------------------------------------------+"); +} diff --git a/crates/nu-table/src/table.rs b/crates/nu-table/src/table.rs index e1b1269874..e39284c2ea 100644 --- a/crates/nu-table/src/table.rs +++ b/crates/nu-table/src/table.rs @@ -355,11 +355,14 @@ impl TableOption, ColoredConfig> for let total_width = get_total_width2(&self.width, cfg); if total_width > self.max { + let has_header = self.cfg.with_header && rec.count_rows() > 1; + let trim_as_head = has_header && self.cfg.header_on_border; + TableTrim { max: self.max, strategy: self.cfg.trim, width: self.width, - do_as_head: self.cfg.header_on_border, + trim_as_head, } .change(rec, cfg, dim); } else if self.cfg.expand && self.max > total_width { @@ -375,7 +378,7 @@ struct TableTrim { width: Vec, strategy: TrimStrategy, max: usize, - do_as_head: bool, + trim_as_head: bool, } impl TableOption, ColoredConfig> for TableTrim { @@ -387,7 +390,7 @@ impl TableOption, ColoredConfig> for ) { // we already must have been estimated that it's safe to do. // and all dims will be suffitient - if self.do_as_head { + if self.trim_as_head { if recs.is_empty() { return; } @@ -544,7 +547,10 @@ fn maybe_truncate_columns( const TERMWIDTH_THRESHOLD: usize = 120; let preserve_content = termwidth > TERMWIDTH_THRESHOLD; - let truncate = if cfg.header_on_border { + let has_header = cfg.with_header && data.count_rows() > 1; + let is_header_on_border = has_header && cfg.header_on_border; + + let truncate = if is_header_on_border { truncate_columns_by_head } else if preserve_content { truncate_columns_by_columns