From 3d62753e80f31e05c54e80ddc0708b43fd90da2f Mon Sep 17 00:00:00 2001 From: LazyPluto <74699152+LazyPluto@users.noreply.github.com> Date: Wed, 14 May 2025 17:10:15 +0530 Subject: [PATCH] fix: empty tables now respect `$env.config.use_ansi_coloring` (closes #14896) (#15751) - fixes #14896 - related to #15163 # Description This PR fixes the presence of ansi color codes in empty tables, when `$env.config.table.show_empty = true` and `$env.config.use_ansi_coloring = false` # User-Facing Changes Empty tables respect `$env.config.use_ansi_coloring` # Tests + Formatting Added a test for this case. # After Submitting --- crates/nu-command/src/viewers/table.rs | 15 ++++++++++++-- crates/nu-command/tests/commands/table.rs | 24 +++++++++++++++++++++++ crates/nu-table/src/table.rs | 12 +++++++++++- 3 files changed, 48 insertions(+), 3 deletions(-) diff --git a/crates/nu-command/src/viewers/table.rs b/crates/nu-command/src/viewers/table.rs index 698de056e4..599b192409 100644 --- a/crates/nu-command/src/viewers/table.rs +++ b/crates/nu-command/src/viewers/table.rs @@ -544,8 +544,13 @@ fn handle_record(input: CmdInput, mut record: Record) -> ShellResult String { let config = stack.get_config(engine_state); if !config.table.show_empty { @@ -1098,6 +1105,10 @@ fn create_empty_placeholder( let style_computer = &StyleComputer::from_config(engine_state, stack); configure_table(&mut out, &config, style_computer, TableMode::default()); + if !use_ansi_coloring { + out.table.clear_all_colors(); + } + out.table .draw(termwidth) .expect("Could not create empty table placeholder") diff --git a/crates/nu-command/tests/commands/table.rs b/crates/nu-command/tests/commands/table.rs index 80edb72e9a..5426ec5a0a 100644 --- a/crates/nu-command/tests/commands/table.rs +++ b/crates/nu-command/tests/commands/table.rs @@ -3847,6 +3847,30 @@ fn table_colors() { assert_eq!(actual.out, "╭───┬───╮│ a │ 1 ││ b │ 2 │╰───┴───╯"); } +#[test] +fn table_empty_colors() { + let actual = nu!("$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!("$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!("$env.config.use_ansi_coloring = false; []"); + assert_eq!(actual.out, "╭────────────╮│ empty list │╰────────────╯"); + + let actual = nu!("$env.config.use_ansi_coloring = false; {}"); + assert_eq!( + actual.out, + "╭──────────────╮│ empty record │╰──────────────╯", + ); +} + #[test] fn table_index() { let actual = nu!( diff --git a/crates/nu-table/src/table.rs b/crates/nu-table/src/table.rs index 5b961a0000..0723efc5d6 100644 --- a/crates/nu-table/src/table.rs +++ b/crates/nu-table/src/table.rs @@ -16,7 +16,8 @@ use tabled::{ ansi::ANSIBuf, colors::Colors, config::{ - AlignmentHorizontal, ColoredConfig, Entity, Indent, Position, Sides, SpannedConfig, + AlignmentHorizontal, ColoredConfig, Entity, EntityMap, Indent, Position, Sides, + SpannedConfig, }, dimension::{CompleteDimensionVecRecords, SpannedGridDimension}, records::{ @@ -228,12 +229,21 @@ impl NuTable { self.config.border_color = (!color.is_plain()).then_some(color); } + pub fn clear_border_color(&mut self) { + self.config.border_color = None; + } + // NOTE: BE CAREFUL TO KEEP WIDTH UNCHANGED // TODO: fix interface pub fn get_records_mut(&mut self) -> &mut [Vec] { &mut self.data } + pub fn clear_all_colors(&mut self) { + self.clear_border_color(); + self.styles.cfg.set_colors(EntityMap::default()); + } + /// Converts a table to a String. /// /// It returns None in case where table cannot be fit to a terminal width.