Skip to content

Commit

Permalink
refactor: use the atty crate for isatty() detection
Browse files Browse the repository at this point in the history
Not only does this remove some unsafe code from clap itself, `atty` does the
right thing on Windows too. This isn't relevant now since we don't currently
support colorized output on Windows, but will come in handy if/when we
implement that feature (clap-rs#836).
  • Loading branch information
pkgw committed Feb 19, 2017
1 parent 6f9a0ba commit 68e0368
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 16 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@ term_size = { version = "0.2.2", optional = true }
libc = { version = "0.2.20", optional = true }
yaml-rust = { version = "0.3.5", optional = true }
clippy = { version = "~0.0.112", optional = true }
atty = { version = "0.2.2", optional = true }

[dev-dependencies]
regex = "~0.1.80"

[features]
default = ["suggestions", "color", "wrap_help"]
suggestions = ["strsim"]
color = ["ansi_term", "libc"]
color = ["ansi_term", "atty"]
wrap_help = ["libc", "term_size"]
yaml = ["yaml-rust"]
unstable = [] # for building with unstable clap features (doesn't require nightly Rust) (currently none)
Expand Down
20 changes: 7 additions & 13 deletions src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,9 @@ use ansi_term::ANSIString;
use ansi_term::Colour::{Green, Red, Yellow};

#[cfg(feature = "color")]
use libc;
use atty;
use std::fmt;

#[cfg(all(feature = "color", not(target_os = "windows")))]
const STDERR: i32 = libc::STDERR_FILENO;
#[cfg(all(feature = "color", not(target_os = "windows")))]
const STDOUT: i32 = libc::STDOUT_FILENO;

#[cfg(target_os = "windows")]
const STDERR: i32 = 0;
#[cfg(target_os = "windows")]
const STDOUT: i32 = 0;

#[doc(hidden)]
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum ColorWhen {
Expand All @@ -29,8 +19,12 @@ pub enum ColorWhen {
#[cfg(feature = "color")]
pub fn is_a_tty(stderr: bool) -> bool {
debugln!("is_a_tty: stderr={:?}", stderr);
let fd = if stderr { STDERR } else { STDOUT };
unsafe { libc::isatty(fd) != 0 }
let stream = if stderr {
atty::Stream::Stderr
} else {
atty::Stream::Stdout
};
atty::is(stream)
}

#[cfg(not(feature = "color"))]
Expand Down
6 changes: 4 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@
//! #### Features enabled by default
//!
//! * **"suggestions"**: Turns on the `Did you mean '--myoption'?` feature for when users make typos. (builds dependency `strsim`)
//! * **"color"**: Turns on colored error messages. This feature only works on non-Windows OSs. (builds dependency `ansi-term` and `libc`)
//! * **"color"**: Turns on colored error messages. This feature only works on non-Windows OSs. (builds dependency `ansi-term` and `atty`)
//! * **"wrap_help"**: Wraps the help at the actual terminal width when available, instead of 120 chracters. (builds dependency `term_size`, and `libc`)
//!
//! To disable these, add this to your `Cargo.toml`:
Expand Down Expand Up @@ -533,7 +533,7 @@ extern crate strsim;
extern crate ansi_term;
#[cfg(feature = "yaml")]
extern crate yaml_rust;
#[cfg(any(feature = "wrap_help", feature = "color"))]
#[cfg(feature = "wrap_help")]
extern crate libc;
extern crate unicode_width;
#[macro_use]
Expand All @@ -542,6 +542,8 @@ extern crate vec_map;
#[cfg(feature = "wrap_help")]
extern crate term_size;
extern crate unicode_segmentation;
#[cfg(feature = "color")]
extern crate atty;

#[cfg(feature = "yaml")]
pub use yaml_rust::YamlLoader;
Expand Down

0 comments on commit 68e0368

Please sign in to comment.