Skip to content

Commit 98bbe9b

Browse files
authored
Merge pull request #60 from quadrupleslap-forks-things/master
Use atty to check for color support
2 parents d8cdb6e + 5a32e25 commit 98bbe9b

3 files changed

Lines changed: 13 additions & 1 deletion

File tree

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ log = { version = "0.4.0", features = ["std"] }
1818
regex = { version = "0.2", optional = true }
1919
termcolor = "0.3"
2020
chrono = "0.4"
21+
atty = "0.2"
2122

2223
[[test]]
2324
name = "regexp_filter"

src/fmt.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ use std::cell::RefCell;
4242
use termcolor::{ColorSpec, ColorChoice, Buffer, BufferWriter, WriteColor};
4343
use chrono::{DateTime, Utc};
4444
use chrono::format::Item;
45+
use atty;
4546

4647
pub use termcolor::Color;
4748

@@ -232,7 +233,16 @@ impl Builder {
232233
/// Build a terminal writer.
233234
pub fn build(&mut self) -> Writer {
234235
let color_choice = match self.write_style {
235-
WriteStyle::Auto => ColorChoice::Auto,
236+
WriteStyle::Auto => {
237+
if atty::is(match self.target {
238+
Target::Stderr => atty::Stream::Stderr,
239+
Target::Stdout => atty::Stream::Stdout,
240+
}) {
241+
ColorChoice::Auto
242+
} else {
243+
ColorChoice::Never
244+
}
245+
},
236246
WriteStyle::Always => ColorChoice::Always,
237247
WriteStyle::Never => ColorChoice::Never,
238248
};

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@
155155
extern crate log;
156156
extern crate termcolor;
157157
extern crate chrono;
158+
extern crate atty;
158159

159160
use std::env;
160161
use std::borrow::Cow;

0 commit comments

Comments
 (0)