1
1
use std:: io:: { self , Write } ;
2
2
3
+ use term:: { StdoutTerminal , Terminal } ;
4
+ #[ cfg( windows) ]
5
+ use term:: WinConsole ;
6
+
7
+ use printer:: Writer ;
8
+
3
9
/// Out controls the actual output of all search results for a particular file
4
10
/// to the end user.
5
11
///
@@ -8,15 +14,32 @@ use std::io::{self, Write};
8
14
/// file as a whole. For example, it knows when to print a file separator.)
9
15
pub struct Out < W : io:: Write > {
10
16
wtr : io:: BufWriter < W > ,
17
+ term : Option < Box < StdoutTerminal > > ,
11
18
printed : bool ,
12
19
file_separator : Option < Vec < u8 > > ,
13
20
}
14
21
22
+ /// This is like term::stdout, but on Windows always uses WinConsole instead
23
+ /// of trying for a TerminfoTerminal. This may be a mistake.
24
+ #[ cfg( windows) ]
25
+ fn term_stdout ( ) -> Option < Box < StdoutTerminal > > {
26
+ WinConsole :: new ( io:: stdout ( ) )
27
+ . ok ( )
28
+ . map ( |t| Box :: new ( t) as Box < StdoutTerminal > )
29
+ }
30
+
31
+ #[ cfg( not( windows) ) ]
32
+ fn term_stdout ( ) -> Option < Box < StdoutTerminal > > {
33
+ // We never use this crap on *nix.
34
+ None
35
+ }
36
+
15
37
impl < W : io:: Write > Out < W > {
16
38
/// Create a new Out that writes to the wtr given.
17
39
pub fn new ( wtr : W ) -> Out < W > {
18
40
Out {
19
41
wtr : io:: BufWriter :: new ( wtr) ,
42
+ term : term_stdout ( ) ,
20
43
printed : false ,
21
44
file_separator : None ,
22
45
}
@@ -33,14 +56,31 @@ impl<W: io::Write> Out<W> {
33
56
34
57
/// Write the search results of a single file to the underlying wtr and
35
58
/// flush wtr.
36
- pub fn write ( & mut self , buf : & [ u8 ] ) {
59
+ pub fn write ( & mut self , buf : & Writer < Vec < u8 > > ) {
37
60
if let Some ( ref sep) = self . file_separator {
38
61
if self . printed {
39
62
let _ = self . wtr . write_all ( sep) ;
40
63
let _ = self . wtr . write_all ( b"\n " ) ;
41
64
}
42
65
}
43
- let _ = self . wtr . write_all ( buf) ;
66
+ match * buf {
67
+ Writer :: Colored ( ref tt) => {
68
+ let _ = self . wtr . write_all ( tt. get_ref ( ) ) ;
69
+ }
70
+ Writer :: Windows ( ref w) => {
71
+ match self . term {
72
+ None => {
73
+ let _ = self . wtr . write_all ( w. get_ref ( ) ) ;
74
+ }
75
+ Some ( ref mut stdout) => {
76
+ w. print_stdout ( stdout) ;
77
+ }
78
+ }
79
+ }
80
+ Writer :: NoColor ( ref buf) => {
81
+ let _ = self . wtr . write_all ( buf) ;
82
+ }
83
+ }
44
84
let _ = self . wtr . flush ( ) ;
45
85
self . printed = true ;
46
86
}
0 commit comments