From 6e6ef862c500345c8bca3c74775b56b8b15d1175 Mon Sep 17 00:00:00 2001 From: Maxim Zhiburt Date: Wed, 8 Feb 2023 05:01:31 +0300 Subject: [PATCH] Address #7997 (#8000) Hi there, The case which was presented must be addressed. But I did not test it properly... I'd encourage you to do so. Take care. --------- Signed-off-by: Maxim Zhiburt --- crates/nu-command/src/viewers/table.rs | 57 ++++++++------- crates/nu-command/tests/commands/table.rs | 86 +++++++++++++++++++++++ 2 files changed, 118 insertions(+), 25 deletions(-) diff --git a/crates/nu-command/src/viewers/table.rs b/crates/nu-command/src/viewers/table.rs index 70225e683f..9694d1a71b 100644 --- a/crates/nu-command/src/viewers/table.rs +++ b/crates/nu-command/src/viewers/table.rs @@ -1333,24 +1333,31 @@ fn convert_to_table2_entry( return value_to_styled_string(item, config, style_computer); } - let table = match &item { + match &item { Value::Record { span, cols, vals } => { if cols.is_empty() && vals.is_empty() { return value_to_styled_string(item, config, style_computer); } - convert_to_table2( - 0, - std::iter::once(item), + // we verify what is the structure of a Record cause it might represent + + let table = build_expanded_table( + cols.clone(), + vals.clone(), + *span, ctrlc.clone(), config, - *span, style_computer, + width, deep.map(|i| i - 1), flatten, flatten_sep, - width, - ) + ); + + match table { + Ok(Some(table)) => (table, TextStyle::default()), + _ => value_to_styled_string(item, config, style_computer), + } } Value::List { vals, span } => { if flatten { @@ -1363,7 +1370,7 @@ fn convert_to_table2_entry( } } - convert_to_table2( + let table = convert_to_table2( 0, vals.iter(), ctrlc.clone(), @@ -1374,24 +1381,24 @@ fn convert_to_table2_entry( flatten, flatten_sep, width, - ) + ); + + let (table, whead, windex) = match table { + Ok(Some(out)) => out, + _ => return value_to_styled_string(item, config, style_computer), + }; + + let count_rows = table.count_rows(); + let table_config = + create_table_config(config, style_computer, count_rows, whead, windex, false); + + let table = table.draw(table_config, usize::MAX); + match table { + Some(table) => (table, TextStyle::default()), + None => value_to_styled_string(item, config, style_computer), + } } - _ => return value_to_styled_string(item, config, style_computer), // unknown type. - }; - - let (table, whead, windex) = match table { - Ok(Some(out)) => out, - _ => return value_to_styled_string(item, config, style_computer), - }; - - let count_rows = table.count_rows(); - let table_config = - create_table_config(config, style_computer, count_rows, whead, windex, false); - - let table = table.draw(table_config, usize::MAX); - match table { - Some(table) => (table, TextStyle::default()), - None => value_to_styled_string(item, config, style_computer), + _ => value_to_styled_string(item, config, style_computer), // unknown type. } } diff --git a/crates/nu-command/tests/commands/table.rs b/crates/nu-command/tests/commands/table.rs index c17ceb5b49..50435941c2 100644 --- a/crates/nu-command/tests/commands/table.rs +++ b/crates/nu-command/tests/commands/table.rs @@ -180,6 +180,92 @@ fn table_expand_flatten_and_deep_1() { ); } +#[test] +fn table_expand_record_0() { + let actual = nu!(r#"[{c: {d: 1}}] | table --expand"#); + + assert_eq!( + actual.out, + "╭───┬───────────╮\ + │ # │ c │\ + ├───┼───────────┤\ + │ 0 │ ╭───┬───╮ │\ + │ │ │ d │ 1 │ │\ + │ │ ╰───┴───╯ │\ + ╰───┴───────────╯" + ); +} + +#[test] +fn table_expand_record_1() { + let actual = + nu!(r#"[[a b, c]; [1 2 3] [4 5 [1 2 {a: 123, b: 234, c: 345}]]] | table --expand"#); + + assert_eq!( + actual.out, + "╭───┬───┬───┬─────────────────────╮\ + │ # │ a │ b │ c │\ + ├───┼───┼───┼─────────────────────┤\ + │ 0 │ 1 │ 2 │ 3 │\ + │ 1 │ 4 │ 5 │ ╭───┬─────────────╮ │\ + │ │ │ │ │ 0 │ 1 │ │\ + │ │ │ │ │ 1 │ 2 │ │\ + │ │ │ │ │ 2 │ ╭───┬─────╮ │ │\ + │ │ │ │ │ │ │ a │ 123 │ │ │\ + │ │ │ │ │ │ │ b │ 234 │ │ │\ + │ │ │ │ │ │ │ c │ 345 │ │ │\ + │ │ │ │ │ │ ╰───┴─────╯ │ │\ + │ │ │ │ ╰───┴─────────────╯ │\ + ╰───┴───┴───┴─────────────────────╯" + ); +} + +#[test] +fn table_expand_record_2() { + let structure = "{\ + field1: [ a, b, c ],\ + field2: [ 123, 234, 345 ],\ + field3: [ [ head1, head2, head3 ]; [ 1 2 3 ] [ 79 79 79 ] [ { f1: 'a string', f2: 1000 }, 1, 2 ] ],\ + field4: { f1: 1, f2: 3, f3: { f1: f1, f2: f2, f3: f3 } }\ + }"; + let actual = nu!(format!("{} | table --expand", structure)); + + assert_eq!( + actual.out, + "╭────────┬───────────────────────────────────────────╮\ + │ │ ╭───┬───╮ │\ + │ field1 │ │ 0 │ a │ │\ + │ │ │ 1 │ b │ │\ + │ │ │ 2 │ c │ │\ + │ │ ╰───┴───╯ │\ + │ │ ╭───┬─────╮ │\ + │ field2 │ │ 0 │ 123 │ │\ + │ │ │ 1 │ 234 │ │\ + │ │ │ 2 │ 345 │ │\ + │ │ ╰───┴─────╯ │\ + │ │ ╭───┬───────────────────┬───────┬───────╮ │\ + │ field3 │ │ # │ head1 │ head2 │ head3 │ │\ + │ │ ├───┼───────────────────┼───────┼───────┤ │\ + │ │ │ 0 │ 1 │ 2 │ 3 │ │\ + │ │ │ 1 │ 79 │ 79 │ 79 │ │\ + │ │ │ 2 │ ╭────┬──────────╮ │ 1 │ 2 │ │\ + │ │ │ │ │ f1 │ a string │ │ │ │ │\ + │ │ │ │ │ f2 │ 1000 │ │ │ │ │\ + │ │ │ │ ╰────┴──────────╯ │ │ │ │\ + │ │ ╰───┴───────────────────┴───────┴───────╯ │\ + │ │ ╭────┬─────────────╮ │\ + │ field4 │ │ f1 │ 1 │ │\ + │ │ │ f2 │ 3 │ │\ + │ │ │ │ ╭────┬────╮ │ │\ + │ │ │ f3 │ │ f1 │ f1 │ │ │\ + │ │ │ │ │ f2 │ f2 │ │ │\ + │ │ │ │ │ f3 │ f3 │ │ │\ + │ │ │ │ ╰────┴────╯ │ │\ + │ │ ╰────┴─────────────╯ │\ + ╰────────┴───────────────────────────────────────────╯" + ); +} + #[test] #[cfg(not(windows))] fn external_with_too_much_stdout_should_not_hang_nu() {