From 2b2117173c4b5beeb092bcff5963e51ebc942866 Mon Sep 17 00:00:00 2001 From: Maxim Zhiburt Date: Wed, 13 Jul 2022 14:49:43 +0300 Subject: [PATCH] nu-table: Restore atty check (#6036) Signed-off-by: Maxim Zhiburt --- crates/nu-table/src/table.rs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/crates/nu-table/src/table.rs b/crates/nu-table/src/table.rs index a683a139d..d2753ad70 100644 --- a/crates/nu-table/src/table.rs +++ b/crates/nu-table/src/table.rs @@ -81,7 +81,23 @@ pub fn draw_table( &config.trim_strategy, ); - Some(table.to_string()) + Some(print_table(table, config)) +} + +fn print_table(table: tabled::Table, config: &Config) -> String { + let output = table.to_string(); + + // the atty is for when people do ls from vim, there should be no coloring there + if !config.use_ansi_coloring || !atty::is(atty::Stream::Stdout) { + // Draw the table without ansi colors + match strip_ansi_escapes::strip(&output) { + Ok(bytes) => String::from_utf8_lossy(&bytes).to_string(), + Err(_) => output, // we did our best; so return at least something + } + } else { + // Draw the table with ansi colors + output + } } fn count_columns_on_table(mut table: tabled::Table) -> (usize, tabled::Table) {