From 9a0ae7c4c0efaa2be670234319c6ac695cb0ad63 Mon Sep 17 00:00:00 2001 From: Maxim Zhiburt Date: Wed, 22 Jan 2025 15:49:25 +0300 Subject: [PATCH] Fix #14842 (#14885) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sorry was a little bit busy close #14842 I've added a test but I'd check if it solved it. cc: @fdncred __________________________ **Unrelated** Recently got a pretty good format idea (https://github.com/zhiburt/tabled/issues/472) Just wanna highlight that we could probably experiment with it, if it being a bit elaborated. It's sort of KV table which nushell already has, But it's more for a default table where each row/record being rendered as a KV table. It's not something super nice I guess but maybe it could get some appliance. So yes pointing it out just in case. Like these. ``` ┌──────────────┬───────────────────────────────────────────────┐ │ Field │ Value │ ├──────────────┼───────────────────────────────────────────────┤ │ Company │ INTEL CORP │ ├──────────────┼───────────────────────────────────────────────┤ │ Street │ 2200 MISSION COLLEGE BLVD, RNB-4-151 │ ├──────────────┼───────────────────────────────────────────────┤ │ City │ SANTA CLARA │ ├──────────────┼───────────────────────────────────────────────┤ │ ZIP code │ 95054 │ ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫ │ Company │ Apple Inc. │ ├──────────────┼───────────────────────────────────────────────┤ │ Street │ ONE APPLE PARK WAY │ ├──────────────┼───────────────────────────────────────────────┤ │ City │ CUPERTINO │ ├──────────────┼───────────────────────────────────────────────┤ │ ZIP code │ 95014 │ └──────────────┴───────────────────────────────────────────────┘ ┌──────────────────────────────────────────────────────────────┐ │ INTEL CORP │ ├──────────────┬───────────────────────────────────────────────┤ │ Street │ 2200 MISSION COLLEGE BLVD, RNB-4-151 │ ├──────────────┼───────────────────────────────────────────────┤ │ City │ SANTA CLARA │ ├──────────────┼───────────────────────────────────────────────┤ │ ZIP code │ 95054 │ ├──────────────┴───────────────────────────────────────────────┤ │ Apple Inc. │ ├──────────────┬───────────────────────────────────────────────┤ │ Street │ ONE APPLE PARK WAY │ ├──────────────┼───────────────────────────────────────────────┤ │ City │ CUPERTINO │ ├──────────────┼───────────────────────────────────────────────┤ │ ZIP code │ 95014 │ └──────────────┴───────────────────────────────────────────────┘ ``` PS: Now thinking about it, it's sort of like doing a iteration over rows and building a current KV table, Which is interesting cause we could do it row by row, in which case doing CTRLC would not ruin build but got some data rendered. All though it's a different kind of approach. Just saying. --- crates/nu-command/tests/commands/table.rs | 30 +++++++++++++++++++++++ crates/nu-table/src/types/general.rs | 27 +++++++++++--------- 2 files changed, 46 insertions(+), 11 deletions(-) diff --git a/crates/nu-command/tests/commands/table.rs b/crates/nu-command/tests/commands/table.rs index 04473e1776..41bd1ec531 100644 --- a/crates/nu-command/tests/commands/table.rs +++ b/crates/nu-command/tests/commands/table.rs @@ -3119,3 +3119,33 @@ fn table_colors() { )); assert_eq!(actual.out, "╭───┬───╮│ a │ 1 ││ b │ 2 │╰───┴───╯"); } + +#[test] +fn table_index() { + let actual = nu!("[[ index var ]; [ abc 1 ] [ def 2 ] [ ghi 3 ]] | table --width=80"); + assert_eq!( + actual.out, + "╭─────┬─────╮\ + │ # │ var │\ + ├─────┼─────┤\ + │ abc │ 1 │\ + │ def │ 2 │\ + │ ghi │ 3 │\ + ╰─────┴─────╯" + ); +} + +#[test] +fn table_index_expand() { + let actual = nu!("[[ index var ]; [ abc 1 ] [ def 2 ] [ ghi 3 ]] | table --width=80 --expand"); + assert_eq!( + actual.out, + "╭─────┬─────╮\ + │ # │ var │\ + ├─────┼─────┤\ + │ abc │ 1 │\ + │ def │ 2 │\ + │ ghi │ 3 │\ + ╰─────┴─────╯" + ); +} diff --git a/crates/nu-table/src/types/general.rs b/crates/nu-table/src/types/general.rs index c2ac2a00e4..81d4906783 100644 --- a/crates/nu-table/src/types/general.rs +++ b/crates/nu-table/src/types/general.rs @@ -236,20 +236,25 @@ fn get_table_row_index(item: &Value, config: &Config, row: usize, offset: usize) fn collect_headers(headers: Vec, index: bool) -> Vec { // The header with the INDEX is removed from the table headers since // it is added to the natural table index - - if !index { - headers - .into_iter() - .filter(|header| header != INDEX_COLUMN_NAME) - .map(NuRecordsValue::new) - .collect() + let length = if index { + headers.len() + 1 } else { - let mut v = Vec::with_capacity(headers.len() + 1); + headers.len() + }; + + let mut v = Vec::with_capacity(length); + + if index { v.insert(0, NuRecordsValue::new("#".into())); - for text in headers { - v.push(NuRecordsValue::new(text)); + } + + for text in headers { + if text == INDEX_COLUMN_NAME { + continue; } - v + v.push(NuRecordsValue::new(text)); } + + v }