diff options
author | Andrew Gallant <jamslam@gmail.com> | 2018-08-29 20:53:52 -0400 |
---|---|---|
committer | Andrew Gallant <jamslam@gmail.com> | 2018-09-04 23:18:55 -0400 |
commit | 4846d63539690047fa58ec582d94bcba16da1c09 (patch) | |
tree | 61a2cf9de3d62ea6524659893ab9a2c7800c3286 /grep | |
parent | 13c47530a6e685d2dee1953a64f055936e6a2ba8 (diff) |
grep-cli: introduce new grep-cli crate
This commit moves a lot of "utility" code from ripgrep core into
grep-cli. Any one of these things might not be worth creating a new
crate, but combining everything together results in a fair number of a
convenience routines that make up a decent sized crate.
There is potentially more we could move into the crate, but much of what
remains in ripgrep core is almost entirely dealing with the number of
flags we support.
In the course of doing moving things to the grep-cli crate, we clean up
a lot of gunk and improve failure modes in a number of cases. In
particular, we've fixed a bug where other processes could deadlock if
they write too much to stderr.
Fixes #990
Diffstat (limited to 'grep')
-rw-r--r-- | grep/Cargo.toml | 1 | ||||
-rw-r--r-- | grep/examples/simplegrep.rs | 34 | ||||
-rw-r--r-- | grep/src/lib.rs | 1 |
3 files changed, 10 insertions, 26 deletions
diff --git a/grep/Cargo.toml b/grep/Cargo.toml index 3ffaeae3..64ae2273 100644 --- a/grep/Cargo.toml +++ b/grep/Cargo.toml @@ -13,6 +13,7 @@ keywords = ["regex", "grep", "egrep", "search", "pattern"] license = "Unlicense/MIT" [dependencies] +grep-cli = { version = "0.1.0", path = "../grep-cli" } grep-matcher = { version = "0.1.0", path = "../grep-matcher" } grep-pcre2 = { version = "0.1.0", path = "../grep-pcre2", optional = true } grep-printer = { version = "0.1.0", path = "../grep-printer" } diff --git a/grep/examples/simplegrep.rs b/grep/examples/simplegrep.rs index fb2d4001..d4bdef48 100644 --- a/grep/examples/simplegrep.rs +++ b/grep/examples/simplegrep.rs @@ -1,19 +1,18 @@ -extern crate atty; extern crate grep; extern crate termcolor; extern crate walkdir; use std::env; -use std::error; use std::ffi::OsString; use std::path::Path; use std::process; use std::result; +use grep::cli; use grep::printer::{ColorSpecs, StandardBuilder}; use grep::regex::RegexMatcher; use grep::searcher::{BinaryDetection, SearcherBuilder}; -use termcolor::{ColorChoice, StandardStream}; +use termcolor::ColorChoice; use walkdir::WalkDir; macro_rules! fail { @@ -22,7 +21,7 @@ macro_rules! fail { } } -type Result<T> = result::Result<T, Box<error::Error>>; +type Result<T> = result::Result<T, Box<::std::error::Error>>; fn main() { if let Err(err) = try_main() { @@ -39,26 +38,18 @@ fn try_main() -> Result<()> { if args.len() == 2 { args.push(OsString::from("./")); } - let pattern = match args[1].clone().into_string() { - Ok(pattern) => pattern, - Err(_) => { - fail!( - "pattern is not valid UTF-8: '{:?}'", - args[1].to_string_lossy() - ); - } - }; - search(&pattern, &args[2..]) + search(cli::pattern_from_os(&args[1])?, &args[2..]) } fn search(pattern: &str, paths: &[OsString]) -> Result<()> { let matcher = RegexMatcher::new_line_matcher(&pattern)?; let mut searcher = SearcherBuilder::new() .binary_detection(BinaryDetection::quit(b'\x00')) + .line_number(false) .build(); let mut printer = StandardBuilder::new() - .color_specs(colors()) - .build(StandardStream::stdout(color_choice())); + .color_specs(ColorSpecs::default_with_color()) + .build(cli::stdout(color_choice())); for path in paths { for result in WalkDir::new(path) { @@ -90,18 +81,9 @@ fn search(pattern: &str, paths: &[OsString]) -> Result<()> { } fn color_choice() -> ColorChoice { - if atty::is(atty::Stream::Stdout) { + if cli::is_tty_stdout() { ColorChoice::Auto } else { ColorChoice::Never } } - -fn colors() -> ColorSpecs { - ColorSpecs::new(&[ - "path:fg:magenta".parse().unwrap(), - "line:fg:green".parse().unwrap(), - "match:fg:red".parse().unwrap(), - "match:style:bold".parse().unwrap(), - ]) -} diff --git a/grep/src/lib.rs b/grep/src/lib.rs index ab0d78eb..13eaee25 100644 --- a/grep/src/lib.rs +++ b/grep/src/lib.rs @@ -14,6 +14,7 @@ A cookbook and a guide are planned. #![deny(missing_docs)] +pub extern crate grep_cli as cli; pub extern crate grep_matcher as matcher; #[cfg(feature = "pcre2")] pub extern crate grep_pcre2 as pcre2; |