optionally remove table output color (#455)

This commit is contained in:
Darren Schroeder 2021-12-09 10:00:26 -06:00 committed by GitHub
parent a7a213b3f2
commit a8e5cb871e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 11 deletions

1
Cargo.lock generated
View File

@ -1642,6 +1642,7 @@ name = "nu-table"
version = "0.36.0"
dependencies = [
"ansi-cut",
"atty",
"nu-ansi-term 0.39.0",
"nu-protocol",
"regex",

View File

@ -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()?;
}
_ => {}
}

View File

@ -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"
atty = "0.2.14"

View File

@ -883,10 +883,11 @@ impl WrappedTable {
fn print_table(&self, color_hm: &HashMap<String, Style>, 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,8 +953,19 @@ impl WrappedTable {
output.push_str(&self.print_separator(SeparatorPosition::Bottom, color_hm));
}
// 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
}
}
}
fn process_table(table: &Table) -> ProcessedTable {