Fix kv table width issue with header_on_border configuration (#13325)

GOOD CATCH.............................................................
SORRY

I've added a test to catch regression just in case.

close #13319

cc: @fdncred
This commit is contained in:
Maxim Zhiburt 2024-07-09 17:49:04 +03:00 committed by GitHub
parent 1964dacaef
commit 4cdceca1f7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 16 additions and 4 deletions

View File

@ -2889,3 +2889,9 @@ fn table_list() {
let actual = nu!("table --list --theme basic"); 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 │╰────┴────────────────╯"); 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 |+------+---------------------------------------------------+");
}

View File

@ -355,11 +355,14 @@ impl TableOption<NuRecords, CompleteDimensionVecRecords<'_>, ColoredConfig> for
let total_width = get_total_width2(&self.width, cfg); let total_width = get_total_width2(&self.width, cfg);
if total_width > self.max { 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 { TableTrim {
max: self.max, max: self.max,
strategy: self.cfg.trim, strategy: self.cfg.trim,
width: self.width, width: self.width,
do_as_head: self.cfg.header_on_border, trim_as_head,
} }
.change(rec, cfg, dim); .change(rec, cfg, dim);
} else if self.cfg.expand && self.max > total_width { } else if self.cfg.expand && self.max > total_width {
@ -375,7 +378,7 @@ struct TableTrim {
width: Vec<usize>, width: Vec<usize>,
strategy: TrimStrategy, strategy: TrimStrategy,
max: usize, max: usize,
do_as_head: bool, trim_as_head: bool,
} }
impl TableOption<NuRecords, CompleteDimensionVecRecords<'_>, ColoredConfig> for TableTrim { impl TableOption<NuRecords, CompleteDimensionVecRecords<'_>, ColoredConfig> for TableTrim {
@ -387,7 +390,7 @@ impl TableOption<NuRecords, CompleteDimensionVecRecords<'_>, ColoredConfig> for
) { ) {
// we already must have been estimated that it's safe to do. // we already must have been estimated that it's safe to do.
// and all dims will be suffitient // and all dims will be suffitient
if self.do_as_head { if self.trim_as_head {
if recs.is_empty() { if recs.is_empty() {
return; return;
} }
@ -544,7 +547,10 @@ fn maybe_truncate_columns(
const TERMWIDTH_THRESHOLD: usize = 120; const TERMWIDTH_THRESHOLD: usize = 120;
let preserve_content = termwidth > TERMWIDTH_THRESHOLD; 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 truncate_columns_by_head
} else if preserve_content { } else if preserve_content {
truncate_columns_by_columns truncate_columns_by_columns