Skip to content

Commit

Permalink
#48 support feature isatty on stdout
Browse files Browse the repository at this point in the history
  • Loading branch information
sitkevij committed Nov 16, 2020
1 parent 2e34704 commit 94cee26
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 56 deletions.
111 changes: 55 additions & 56 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ opt-level = 3
[dependencies]
clap = "2.31.1"
ansi_term = "0.11"
atty = "0.2"

[dev-dependencies]
assert_cmd = "1.0.1"
7 changes: 7 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
extern crate ansi_term;
extern crate clap;

use atty::Stream;
use clap::ArgMatches;
use std::env;
use std::error::Error;
Expand Down Expand Up @@ -276,6 +277,12 @@ pub fn run(matches: ArgMatches) -> Result<(), Box<dyn Error>> {
}
}

// prevent term color codes being sent to stdout
// test: cat Cargo.toml | target/debug/hx | more
if !atty::is(Stream::Stdout) {
colorize = false;
}

// array output mode is mutually exclusive
if let Some(array) = matches.value_of(ARG_ARR) {
output_array(array, buf, truncate_len, column_width)?;
Expand Down

0 comments on commit 94cee26

Please sign in to comment.