From 119e62e2b4e3fbc95fefac0ab393049f234fef15 Mon Sep 17 00:00:00 2001 From: freshorange <5673151+freshorange@user.noreply.gitee.com> Date: Sun, 23 Feb 2025 08:57:02 +0800 Subject: [PATCH 1/2] show_empty respect use_ansi_coloring --- crates/nu-command/src/viewers/table.rs | 16 +++++++--- crates/nu-command/tests/commands/table.rs | 39 +++++++++++++++++++++++ 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/crates/nu-command/src/viewers/table.rs b/crates/nu-command/src/viewers/table.rs index 147555558e..c576fad7b3 100644 --- a/crates/nu-command/src/viewers/table.rs +++ b/crates/nu-command/src/viewers/table.rs @@ -545,8 +545,13 @@ fn handle_record(input: CmdInput, mut record: Record) -> ShellResult String { let config = stack.get_config(engine_state); if !config.table.show_empty { @@ -1099,9 +1106,10 @@ fn create_empty_placeholder( let style_computer = &StyleComputer::from_config(engine_state, stack); configure_table(&mut out, &config, style_computer, TableMode::default()); - out.table + let ret = out.table .draw(termwidth) - .expect("Could not create empty table placeholder") + .expect("Could not create empty table placeholder"); + maybe_strip_color(ret, use_ansi_coloring) } fn convert_table_to_output( diff --git a/crates/nu-command/tests/commands/table.rs b/crates/nu-command/tests/commands/table.rs index 41bd1ec531..bbfe0a3cd0 100644 --- a/crates/nu-command/tests/commands/table.rs +++ b/crates/nu-command/tests/commands/table.rs @@ -3120,6 +3120,45 @@ fn table_colors() { assert_eq!(actual.out, "╭───┬───╮│ a │ 1 ││ b │ 2 │╰───┴───╯"); } +#[test] +fn empty_table_colors() { + let actual = nu!(concat!( + "$env.config.use_ansi_coloring = true;", + "{}", + )); + assert_eq!( + actual.out, + "\u{1b}[37m╭──────────────╮\u{1b}[0m\u{1b}[37m│\u{1b}[0m \u{1b}[2mempty record\u{1b}[0m \u{1b}[37m│\u{1b}[0m\u{1b}[37m╰──────────────╯\u{1b}[0m" + ); + + let actual = nu!(concat!( + "$env.config.use_ansi_coloring = true;", + "[]", + )); + assert_eq!( + actual.out, + "\u{1b}[37m╭────────────╮\u{1b}[0m\u{1b}[37m│\u{1b}[0m \u{1b}[2mempty list\u{1b}[0m \u{1b}[37m│\u{1b}[0m\u{1b}[37m╰────────────╯\u{1b}[0m" + ); + + let actual = nu!(concat!( + "$env.config.use_ansi_coloring = false;", + "{}", + )); + assert_eq!( + actual.out, + "╭──────────────╮│ empty record │╰──────────────╯" + ); + + let actual = nu!(concat!( + "$env.config.use_ansi_coloring = false;", + "[]", + )); + assert_eq!( + actual.out, + "╭────────────╮│ empty list │╰────────────╯" + ); +} + #[test] fn table_index() { let actual = nu!("[[ index var ]; [ abc 1 ] [ def 2 ] [ ghi 3 ]] | table --width=80"); From 2c9b09af0de1c8db69435e896100e715c085f391 Mon Sep 17 00:00:00 2001 From: freshorange <5673151+freshorange@user.noreply.gitee.com> Date: Sun, 23 Feb 2025 09:51:33 +0800 Subject: [PATCH 2/2] cargo fmt --- crates/nu-command/src/viewers/table.rs | 3 ++- crates/nu-command/tests/commands/table.rs | 25 +++++------------------ 2 files changed, 7 insertions(+), 21 deletions(-) diff --git a/crates/nu-command/src/viewers/table.rs b/crates/nu-command/src/viewers/table.rs index c576fad7b3..eda28afae2 100644 --- a/crates/nu-command/src/viewers/table.rs +++ b/crates/nu-command/src/viewers/table.rs @@ -1106,7 +1106,8 @@ fn create_empty_placeholder( let style_computer = &StyleComputer::from_config(engine_state, stack); configure_table(&mut out, &config, style_computer, TableMode::default()); - let ret = out.table + let ret = out + .table .draw(termwidth) .expect("Could not create empty table placeholder"); maybe_strip_color(ret, use_ansi_coloring) diff --git a/crates/nu-command/tests/commands/table.rs b/crates/nu-command/tests/commands/table.rs index bbfe0a3cd0..af68b7a45d 100644 --- a/crates/nu-command/tests/commands/table.rs +++ b/crates/nu-command/tests/commands/table.rs @@ -3122,41 +3122,26 @@ fn table_colors() { #[test] fn empty_table_colors() { - let actual = nu!(concat!( - "$env.config.use_ansi_coloring = true;", - "{}", - )); + let actual = nu!(concat!("$env.config.use_ansi_coloring = true;", "{}",)); assert_eq!( actual.out, "\u{1b}[37m╭──────────────╮\u{1b}[0m\u{1b}[37m│\u{1b}[0m \u{1b}[2mempty record\u{1b}[0m \u{1b}[37m│\u{1b}[0m\u{1b}[37m╰──────────────╯\u{1b}[0m" ); - let actual = nu!(concat!( - "$env.config.use_ansi_coloring = true;", - "[]", - )); + let actual = nu!(concat!("$env.config.use_ansi_coloring = true;", "[]",)); assert_eq!( actual.out, "\u{1b}[37m╭────────────╮\u{1b}[0m\u{1b}[37m│\u{1b}[0m \u{1b}[2mempty list\u{1b}[0m \u{1b}[37m│\u{1b}[0m\u{1b}[37m╰────────────╯\u{1b}[0m" ); - let actual = nu!(concat!( - "$env.config.use_ansi_coloring = false;", - "{}", - )); + let actual = nu!(concat!("$env.config.use_ansi_coloring = false;", "{}",)); assert_eq!( actual.out, "╭──────────────╮│ empty record │╰──────────────╯" ); - let actual = nu!(concat!( - "$env.config.use_ansi_coloring = false;", - "[]", - )); - assert_eq!( - actual.out, - "╭────────────╮│ empty list │╰────────────╯" - ); + let actual = nu!(concat!("$env.config.use_ansi_coloring = false;", "[]",)); + assert_eq!(actual.out, "╭────────────╮│ empty list │╰────────────╯"); } #[test]