Skip to content

Commit

Permalink
Replace internal atty module with atty crate.
Browse files Browse the repository at this point in the history
This removes all use of explicit unsafe in ripgrep proper except for
one: accessing the contents of a memory map. (Which may never go away.)
  • Loading branch information
BurntSushi committed Jan 15, 2017
1 parent b1d1cd2 commit f5a2d02
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 152 deletions.
12 changes: 12 additions & 0 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
Expand Up @@ -25,6 +25,7 @@ name = "integration"
path = "tests/tests.rs"

[dependencies]
atty = "0.2.2"
bytecount = "0.1.4"
clap = "2.19.0"
env_logger = { version = "0.3", default-features = false }
Expand Down
33 changes: 27 additions & 6 deletions src/args.rs
Expand Up @@ -394,8 +394,8 @@ impl<'a> ArgMatches<'a> {
self.values_of_os("file").map_or(false, |mut files| {
files.any(|f| f == "-")
});
let search_cwd = atty::on_stdin()
|| !atty::stdin_is_readable()
let search_cwd = atty::is(atty::Stream::Stdin)
|| !stdin_is_readable()
|| (self.is_present("file") && file_is_stdin)
|| self.is_present("files")
|| self.is_present("type-list");
Expand Down Expand Up @@ -584,7 +584,7 @@ impl<'a> ArgMatches<'a> {
} else {
self.is_present("line-number")
|| self.is_present("column")
|| atty::on_stdout()
|| atty::is(atty::Stream::Stdout)
|| self.is_present("pretty")
|| self.is_present("vimgrep")
}
Expand All @@ -602,7 +602,7 @@ impl<'a> ArgMatches<'a> {
false
} else {
self.is_present("heading")
|| atty::on_stdout()
|| atty::is(atty::Stream::Stdout)
|| self.is_present("pretty")
}
}
Expand Down Expand Up @@ -667,7 +667,7 @@ impl<'a> ArgMatches<'a> {
} else if self.is_present("vimgrep") {
false
} else if preference == "auto" {
atty::on_stdout() || self.is_present("pretty")
atty::is(atty::Stream::Stdout) || self.is_present("pretty")
} else {
false
}
Expand All @@ -687,7 +687,7 @@ impl<'a> ArgMatches<'a> {
} else if self.is_present("vimgrep") {
termcolor::ColorChoice::Never
} else if preference == "auto" {
if atty::on_stdout() || self.is_present("pretty") {
if atty::is(atty::Stream::Stdout) || self.is_present("pretty") {
termcolor::ColorChoice::Auto
} else {
termcolor::ColorChoice::Never
Expand Down Expand Up @@ -869,3 +869,24 @@ impl QuietMatched {
}
}
}

/// Returns true if and only if stdin is deemed searchable.
#[cfg(unix)]
fn stdin_is_readable() -> bool {
use std::os::unix::fs::FileTypeExt;
use same_file::Handle;

let ft = match Handle::stdin().and_then(|h| h.as_file().metadata()) {
Err(_) => return false,
Ok(md) => md.file_type(),
};
ft.is_file() || ft.is_fifo()
}

/// Returns true if and only if stdin is deemed searchable.
#[cfg(windows)]
fn stdin_is_readable() -> bool {
// On Windows, it's not clear what the possibilities are to me, so just
// always return true.
true
}
145 changes: 0 additions & 145 deletions src/atty.rs

This file was deleted.

2 changes: 1 addition & 1 deletion src/main.rs
@@ -1,3 +1,4 @@
extern crate atty;
extern crate bytecount;
#[macro_use]
extern crate clap;
Expand Down Expand Up @@ -46,7 +47,6 @@ macro_rules! eprintln {

mod app;
mod args;
mod atty;
mod pathutil;
mod printer;
mod search_buffer;
Expand Down

0 comments on commit f5a2d02

Please sign in to comment.