diff --git a/crates/nu-command/src/viewers/table.rs b/crates/nu-command/src/viewers/table.rs index e081bdb9de..bac035f94a 100644 --- a/crates/nu-command/src/viewers/table.rs +++ b/crates/nu-command/src/viewers/table.rs @@ -2,7 +2,7 @@ // overall reduce the redundant calls to StyleComputer etc. // the goal is to configure it once... -use std::{collections::VecDeque, io::IsTerminal, io::Read, path::PathBuf, str::FromStr}; +use std::{collections::VecDeque, io::Read, path::PathBuf, str::FromStr}; use lscolors::{LsColors, Style}; use url::Url; @@ -310,7 +310,7 @@ fn get_cli_args(call: &Call<'_>, state: &EngineState, stack: &mut Stack) -> Shel get_theme_flag(call, state, stack)?.unwrap_or_else(|| stack.get_config(state).table.mode); let index = get_index_flag(call, state, stack)?; - let use_ansi_coloring = state.get_config().use_ansi_coloring.get(state); + let use_ansi_coloring = stack.get_config(state).use_ansi_coloring.get(state); Ok(CLIArgs { theme, @@ -1067,8 +1067,9 @@ fn render_path_name( } fn maybe_strip_color(output: String, use_ansi_coloring: bool) -> String { - // the terminal is for when people do ls from vim, there should be no coloring there - if !use_ansi_coloring || !std::io::stdout().is_terminal() { + // only use `use_ansi_coloring` here, it already includes `std::io::stdout().is_terminal()` + // when set to "auto" + if !use_ansi_coloring { // Draw the table without ansi colors nu_utils::strip_ansi_string_likely(output) } else { diff --git a/crates/nu-command/tests/commands/table.rs b/crates/nu-command/tests/commands/table.rs index 586987970c..04473e1776 100644 --- a/crates/nu-command/tests/commands/table.rs +++ b/crates/nu-command/tests/commands/table.rs @@ -3100,3 +3100,22 @@ fn table_footer_inheritance_list_rows() { ╰───┴──────┴───────────────────────╯" ); } + +/// Test checking whether automatic table rendering correctly uses ansi coloring. +#[test] +fn table_colors() { + let actual = nu!(concat!( + "$env.config.use_ansi_coloring = true;", + "{a: 1, b: 2}", + )); + assert_eq!( + actual.out, + "\u{1b}[37m╭───┬───╮\u{1b}[0m\u{1b}[37m│\u{1b}[0m \u{1b}[1;32ma\u{1b}[0m \u{1b}[37m│\u{1b}[0m \u{1b}[37m1\u{1b}[0m \u{1b}[37m│\u{1b}[0m\u{1b}[37m│\u{1b}[0m \u{1b}[1;32mb\u{1b}[0m \u{1b}[37m│\u{1b}[0m \u{1b}[37m2\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;", + "{a: 1, b: 2}", + )); + assert_eq!(actual.out, "╭───┬───╮│ a │ 1 ││ b │ 2 │╰───┴───╯"); +}