remove ansi colors if this is not a tty (#4058)

This commit is contained in:
Darren Schroeder 2021-10-01 09:00:08 -05:00 committed by GitHub
parent 5e34ef6dff
commit d395816929
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 3 deletions

2
Cargo.lock generated
View File

@ -2900,8 +2900,10 @@ dependencies = [
name = "nu-table" name = "nu-table"
version = "0.37.1" version = "0.37.1"
dependencies = [ dependencies = [
"atty",
"nu-ansi-term", "nu-ansi-term",
"regex", "regex",
"strip-ansi-escapes",
"unicode-width", "unicode-width",
] ]

View File

@ -12,7 +12,9 @@ name = "table"
path = "src/main.rs" path = "src/main.rs"
[dependencies] [dependencies]
atty = "0.2.14"
nu-ansi-term = { version = "0.37.1", path="../nu-ansi-term" } nu-ansi-term = { version = "0.37.1", path="../nu-ansi-term" }
regex = "1.4" regex = "1.4"
strip-ansi-escapes = "0.1.1"
unicode-width = "0.1.8" unicode-width = "0.1.8"

View File

@ -27,8 +27,18 @@ fn main() {
let color_hm: HashMap<String, nu_ansi_term::Style> = HashMap::new(); let color_hm: HashMap<String, nu_ansi_term::Style> = HashMap::new();
// Capture the table as a string // Capture the table as a string
let output_table = draw_table(&table, width, &color_hm); let output_table = draw_table(&table, width, &color_hm);
// Draw the table
println!("{}", output_table) if atty::is(atty::Stream::Stdout) {
// Draw the table with ansi colors
println!("{}", output_table)
} else {
// Draw the table without ansi colors
if let Ok(bytes) = strip_ansi_escapes::strip(&output_table) {
println!("{}", String::from_utf8_lossy(&bytes))
} else {
println!("{}", output_table)
}
}
} }
fn make_table_data() -> (Vec<&'static str>, Vec<&'static str>) { fn make_table_data() -> (Vec<&'static str>, Vec<&'static str>) {

View File

@ -918,7 +918,17 @@ impl WrappedTable {
output.push_str(&self.print_separator(SeparatorPosition::Bottom, color_hm)); output.push_str(&self.print_separator(SeparatorPosition::Bottom, color_hm));
} }
output if atty::is(atty::Stream::Stdout) {
// Draw the table with ansi colors
output
} else {
// Draw the table without ansi colors
if let Ok(bytes) = strip_ansi_escapes::strip(&output) {
String::from_utf8_lossy(&bytes).to_string()
} else {
output
}
}
} }
} }