diff --git a/Cargo.lock b/Cargo.lock index 2fe88514f7..90eee98719 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1642,6 +1642,7 @@ name = "nu-table" version = "0.36.0" dependencies = [ "ansi-cut", + "atty", "nu-ansi-term 0.39.0", "nu-protocol", "regex", diff --git a/crates/nu-protocol/src/config.rs b/crates/nu-protocol/src/config.rs index fbbc488859..4635d90eec 100644 --- a/crates/nu-protocol/src/config.rs +++ b/crates/nu-protocol/src/config.rs @@ -14,6 +14,7 @@ pub struct Config { pub footer_mode: FooterMode, pub animate_prompt: bool, pub float_precision: i64, + pub without_color: bool, } impl Default for Config { @@ -27,6 +28,7 @@ impl Default for Config { footer_mode: FooterMode::Never, animate_prompt: ANIMATE_PROMPT_DEFAULT, float_precision: 4, + without_color: false, } } } @@ -84,12 +86,13 @@ impl Value { }; } "animate_prompt" => { - let val = value.as_bool()?; - config.animate_prompt = val; + config.animate_prompt = value.as_bool()?; } "float_precision" => { - let val = value.as_integer()?; - config.float_precision = val; + config.float_precision = value.as_integer()?; + } + "without_color" => { + config.without_color = value.as_bool()?; } _ => {} } diff --git a/crates/nu-table/Cargo.toml b/crates/nu-table/Cargo.toml index 18cb076316..6138139625 100644 --- a/crates/nu-table/Cargo.toml +++ b/crates/nu-table/Cargo.toml @@ -12,10 +12,10 @@ name = "table" path = "src/main.rs" [dependencies] -# nu-ansi-term = "0.39.0" nu-ansi-term = { path = "../nu-ansi-term" } nu-protocol = { path = "../nu-protocol"} regex = "1.4" unicode-width = "0.1.8" strip-ansi-escapes = "0.1.1" -ansi-cut = "0.1.1" \ No newline at end of file +ansi-cut = "0.1.1" +atty = "0.2.14" diff --git a/crates/nu-table/src/table.rs b/crates/nu-table/src/table.rs index e73f24e587..e66f8294bf 100644 --- a/crates/nu-table/src/table.rs +++ b/crates/nu-table/src/table.rs @@ -883,10 +883,11 @@ impl WrappedTable { fn print_table(&self, color_hm: &HashMap, config: &Config) -> String { let mut output = String::new(); - #[cfg(windows)] - { - let _ = nu_ansi_term::enable_ansi_support(); - } + // TODO: This may be unnecessary after JTs changes. Let's remove it and see. + // #[cfg(windows)] + // { + // let _ = nu_ansi_term::enable_ansi_support(); + // } if self.data.is_empty() { return output; @@ -952,7 +953,18 @@ impl WrappedTable { output.push_str(&self.print_separator(SeparatorPosition::Bottom, color_hm)); } - output + // the atty is for when people do ls from vim, there should be no coloring there + if config.without_color || !atty::is(atty::Stream::Stdout) { + // Draw the table without ansi colors + if let Ok(bytes) = strip_ansi_escapes::strip(&output) { + String::from_utf8_lossy(&bytes).to_string() + } else { + output + } + } else { + // Draw the table with ansi colors + output + } } }