@@ -3,10 +3,9 @@ extern crate termcolor;
3
3
extern crate walkdir;
4
4
5
5
use std:: env;
6
+ use std:: error:: Error ;
6
7
use std:: ffi:: OsString ;
7
- use std:: path:: Path ;
8
8
use std:: process;
9
- use std:: result;
10
9
11
10
use grep:: cli;
12
11
use grep:: printer:: { ColorSpecs , StandardBuilder } ;
@@ -15,52 +14,46 @@ use grep::searcher::{BinaryDetection, SearcherBuilder};
15
14
use termcolor:: ColorChoice ;
16
15
use walkdir:: WalkDir ;
17
16
18
- macro_rules! fail {
19
- ( $( $tt: tt) * ) => {
20
- return Err ( From :: from( format!( $( $tt) * ) ) ) ;
21
- }
22
- }
23
-
24
- type Result < T > = result:: Result < T , Box < :: std:: error:: Error > > ;
25
-
26
17
fn main ( ) {
27
18
if let Err ( err) = try_main ( ) {
28
19
eprintln ! ( "{}" , err) ;
29
20
process:: exit ( 1 ) ;
30
21
}
31
22
}
32
23
33
- fn try_main ( ) -> Result < ( ) > {
24
+ fn try_main ( ) -> Result < ( ) , Box < Error > > {
34
25
let mut args: Vec < OsString > = env:: args_os ( ) . collect ( ) ;
35
26
if args. len ( ) < 2 {
36
- fail ! ( "Usage: simplegrep <pattern> [<path> ...]" ) ;
27
+ return Err ( "Usage: simplegrep <pattern> [<path> ...]" . into ( ) ) ;
37
28
}
38
29
if args. len ( ) == 2 {
39
30
args. push ( OsString :: from ( "./" ) ) ;
40
31
}
41
32
search ( cli:: pattern_from_os ( & args[ 1 ] ) ?, & args[ 2 ..] )
42
33
}
43
34
44
- fn search ( pattern : & str , paths : & [ OsString ] ) -> Result < ( ) > {
35
+ fn search ( pattern : & str , paths : & [ OsString ] ) -> Result < ( ) , Box < Error > > {
45
36
let matcher = RegexMatcher :: new_line_matcher ( & pattern) ?;
46
37
let mut searcher = SearcherBuilder :: new ( )
47
38
. binary_detection ( BinaryDetection :: quit ( b'\x00' ) )
48
39
. line_number ( false )
49
40
. build ( ) ;
50
41
let mut printer = StandardBuilder :: new ( )
51
42
. color_specs ( ColorSpecs :: default_with_color ( ) )
52
- . build ( cli:: stdout ( color_choice ( ) ) ) ;
43
+ . build ( cli:: stdout (
44
+ if cli:: is_tty_stdout ( ) {
45
+ ColorChoice :: Auto
46
+ } else {
47
+ ColorChoice :: Never
48
+ }
49
+ ) ) ;
53
50
54
51
for path in paths {
55
52
for result in WalkDir :: new ( path) {
56
53
let dent = match result {
57
54
Ok ( dent) => dent,
58
55
Err ( err) => {
59
- eprintln ! (
60
- "{}: {}" ,
61
- err. path( ) . unwrap_or( Path :: new( "error" ) ) . display( ) ,
62
- err,
63
- ) ;
56
+ eprintln ! ( "{}" , err) ;
64
57
continue ;
65
58
}
66
59
} ;
@@ -79,11 +72,3 @@ fn search(pattern: &str, paths: &[OsString]) -> Result<()> {
79
72
}
80
73
Ok ( ( ) )
81
74
}
82
-
83
- fn color_choice ( ) -> ColorChoice {
84
- if cli:: is_tty_stdout ( ) {
85
- ColorChoice :: Auto
86
- } else {
87
- ColorChoice :: Never
88
- }
89
- }
0 commit comments