mirror of
https://github.com/BurntSushi/ripgrep.git
synced 2025-05-19 01:30:21 -07:00
simplegrep: touch up
This commit is contained in:
parent
0e2f8f7b47
commit
3797a2a5cb
@ -3,10 +3,9 @@ extern crate termcolor;
|
|||||||
extern crate walkdir;
|
extern crate walkdir;
|
||||||
|
|
||||||
use std::env;
|
use std::env;
|
||||||
|
use std::error::Error;
|
||||||
use std::ffi::OsString;
|
use std::ffi::OsString;
|
||||||
use std::path::Path;
|
|
||||||
use std::process;
|
use std::process;
|
||||||
use std::result;
|
|
||||||
|
|
||||||
use grep::cli;
|
use grep::cli;
|
||||||
use grep::printer::{ColorSpecs, StandardBuilder};
|
use grep::printer::{ColorSpecs, StandardBuilder};
|
||||||
@ -15,14 +14,6 @@ use grep::searcher::{BinaryDetection, SearcherBuilder};
|
|||||||
use termcolor::ColorChoice;
|
use termcolor::ColorChoice;
|
||||||
use walkdir::WalkDir;
|
use walkdir::WalkDir;
|
||||||
|
|
||||||
macro_rules! fail {
|
|
||||||
($($tt:tt)*) => {
|
|
||||||
return Err(From::from(format!($($tt)*)));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type Result<T> = result::Result<T, Box<::std::error::Error>>;
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
if let Err(err) = try_main() {
|
if let Err(err) = try_main() {
|
||||||
eprintln!("{}", err);
|
eprintln!("{}", err);
|
||||||
@ -30,10 +21,10 @@ fn main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn try_main() -> Result<()> {
|
fn try_main() -> Result<(), Box<Error>> {
|
||||||
let mut args: Vec<OsString> = env::args_os().collect();
|
let mut args: Vec<OsString> = env::args_os().collect();
|
||||||
if args.len() < 2 {
|
if args.len() < 2 {
|
||||||
fail!("Usage: simplegrep <pattern> [<path> ...]");
|
return Err("Usage: simplegrep <pattern> [<path> ...]".into());
|
||||||
}
|
}
|
||||||
if args.len() == 2 {
|
if args.len() == 2 {
|
||||||
args.push(OsString::from("./"));
|
args.push(OsString::from("./"));
|
||||||
@ -41,7 +32,7 @@ fn try_main() -> Result<()> {
|
|||||||
search(cli::pattern_from_os(&args[1])?, &args[2..])
|
search(cli::pattern_from_os(&args[1])?, &args[2..])
|
||||||
}
|
}
|
||||||
|
|
||||||
fn search(pattern: &str, paths: &[OsString]) -> Result<()> {
|
fn search(pattern: &str, paths: &[OsString]) -> Result<(), Box<Error>> {
|
||||||
let matcher = RegexMatcher::new_line_matcher(&pattern)?;
|
let matcher = RegexMatcher::new_line_matcher(&pattern)?;
|
||||||
let mut searcher = SearcherBuilder::new()
|
let mut searcher = SearcherBuilder::new()
|
||||||
.binary_detection(BinaryDetection::quit(b'\x00'))
|
.binary_detection(BinaryDetection::quit(b'\x00'))
|
||||||
@ -49,18 +40,20 @@ fn search(pattern: &str, paths: &[OsString]) -> Result<()> {
|
|||||||
.build();
|
.build();
|
||||||
let mut printer = StandardBuilder::new()
|
let mut printer = StandardBuilder::new()
|
||||||
.color_specs(ColorSpecs::default_with_color())
|
.color_specs(ColorSpecs::default_with_color())
|
||||||
.build(cli::stdout(color_choice()));
|
.build(cli::stdout(
|
||||||
|
if cli::is_tty_stdout() {
|
||||||
|
ColorChoice::Auto
|
||||||
|
} else {
|
||||||
|
ColorChoice::Never
|
||||||
|
}
|
||||||
|
));
|
||||||
|
|
||||||
for path in paths {
|
for path in paths {
|
||||||
for result in WalkDir::new(path) {
|
for result in WalkDir::new(path) {
|
||||||
let dent = match result {
|
let dent = match result {
|
||||||
Ok(dent) => dent,
|
Ok(dent) => dent,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
eprintln!(
|
eprintln!("{}", err);
|
||||||
"{}: {}",
|
|
||||||
err.path().unwrap_or(Path::new("error")).display(),
|
|
||||||
err,
|
|
||||||
);
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -79,11 +72,3 @@ fn search(pattern: &str, paths: &[OsString]) -> Result<()> {
|
|||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn color_choice() -> ColorChoice {
|
|
||||||
if cli::is_tty_stdout() {
|
|
||||||
ColorChoice::Auto
|
|
||||||
} else {
|
|
||||||
ColorChoice::Never
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user