From 3797a2a5cb2bd92cb6940d83fa3e61b2b570a473 Mon Sep 17 00:00:00 2001 From: Andrew Gallant Date: Wed, 5 Sep 2018 11:51:53 -0400 Subject: simplegrep: touch up --- grep/examples/simplegrep.rs | 39 ++++++++++++--------------------------- 1 file changed, 12 insertions(+), 27 deletions(-) diff --git a/grep/examples/simplegrep.rs b/grep/examples/simplegrep.rs index d4bdef48..85e2f778 100644 --- a/grep/examples/simplegrep.rs +++ b/grep/examples/simplegrep.rs @@ -3,10 +3,9 @@ extern crate termcolor; extern crate walkdir; use std::env; +use std::error::Error; use std::ffi::OsString; -use std::path::Path; use std::process; -use std::result; use grep::cli; use grep::printer::{ColorSpecs, StandardBuilder}; @@ -15,14 +14,6 @@ use grep::searcher::{BinaryDetection, SearcherBuilder}; use termcolor::ColorChoice; use walkdir::WalkDir; -macro_rules! fail { - ($($tt:tt)*) => { - return Err(From::from(format!($($tt)*))); - } -} - -type Result = result::Result>; - fn main() { if let Err(err) = try_main() { eprintln!("{}", err); @@ -30,10 +21,10 @@ fn main() { } } -fn try_main() -> Result<()> { +fn try_main() -> Result<(), Box> { let mut args: Vec = env::args_os().collect(); if args.len() < 2 { - fail!("Usage: simplegrep [ ...]"); + return Err("Usage: simplegrep [ ...]".into()); } if args.len() == 2 { args.push(OsString::from("./")); @@ -41,7 +32,7 @@ fn try_main() -> Result<()> { search(cli::pattern_from_os(&args[1])?, &args[2..]) } -fn search(pattern: &str, paths: &[OsString]) -> Result<()> { +fn search(pattern: &str, paths: &[OsString]) -> Result<(), Box> { let matcher = RegexMatcher::new_line_matcher(&pattern)?; let mut searcher = SearcherBuilder::new() .binary_detection(BinaryDetection::quit(b'\x00')) @@ -49,18 +40,20 @@ fn search(pattern: &str, paths: &[OsString]) -> Result<()> { .build(); let mut printer = StandardBuilder::new() .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 result in WalkDir::new(path) { let dent = match result { Ok(dent) => dent, Err(err) => { - eprintln!( - "{}: {}", - err.path().unwrap_or(Path::new("error")).display(), - err, - ); + eprintln!("{}", err); continue; } }; @@ -79,11 +72,3 @@ fn search(pattern: &str, paths: &[OsString]) -> Result<()> { } Ok(()) } - -fn color_choice() -> ColorChoice { - if cli::is_tty_stdout() { - ColorChoice::Auto - } else { - ColorChoice::Never - } -} -- cgit v1.2.3