Skip to content

Commit 0042dce

Browse files
committed
Hack in Windows console coloring.
The code has suffered and needs refactoring/commenting. BUT... IT WORKS!
1 parent ca058d7 commit 0042dce

File tree

6 files changed

+215
-31
lines changed

6 files changed

+215
-31
lines changed

src/main.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ use std::thread;
3535
use crossbeam::sync::chase_lev::{self, Steal, Stealer};
3636
use grep::Grep;
3737
use memmap::{Mmap, Protection};
38+
use term::Terminal;
3839
use walkdir::DirEntry;
3940

4041
use args::Args;
@@ -198,11 +199,11 @@ impl Worker {
198199
let mut printer = self.args.printer(outbuf);
199200
self.do_work(&mut printer, work);
200201
let outbuf = printer.into_inner();
201-
if !outbuf.is_empty() {
202+
if !outbuf.get_ref().is_empty() {
202203
let mut out = self.out.lock().unwrap();
203204
out.write(&outbuf);
204205
}
205-
self.outbuf = Some(outbuf);
206+
self.outbuf = Some(outbuf.into_inner());
206207
}
207208
self.match_count
208209
}

src/out.rs

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
use std::io::{self, Write};
22

3+
use term::{StdoutTerminal, Terminal};
4+
#[cfg(windows)]
5+
use term::WinConsole;
6+
7+
use printer::Writer;
8+
39
/// Out controls the actual output of all search results for a particular file
410
/// to the end user.
511
///
@@ -8,15 +14,32 @@ use std::io::{self, Write};
814
/// file as a whole. For example, it knows when to print a file separator.)
915
pub struct Out<W: io::Write> {
1016
wtr: io::BufWriter<W>,
17+
term: Option<Box<StdoutTerminal>>,
1118
printed: bool,
1219
file_separator: Option<Vec<u8>>,
1320
}
1421

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+
1537
impl<W: io::Write> Out<W> {
1638
/// Create a new Out that writes to the wtr given.
1739
pub fn new(wtr: W) -> Out<W> {
1840
Out {
1941
wtr: io::BufWriter::new(wtr),
42+
term: term_stdout(),
2043
printed: false,
2144
file_separator: None,
2245
}
@@ -33,14 +56,31 @@ impl<W: io::Write> Out<W> {
3356

3457
/// Write the search results of a single file to the underlying wtr and
3558
/// flush wtr.
36-
pub fn write(&mut self, buf: &[u8]) {
59+
pub fn write(&mut self, buf: &Writer<Vec<u8>>) {
3760
if let Some(ref sep) = self.file_separator {
3861
if self.printed {
3962
let _ = self.wtr.write_all(sep);
4063
let _ = self.wtr.write_all(b"\n");
4164
}
4265
}
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+
}
4484
let _ = self.wtr.flush();
4585
self.printed = true;
4686
}

0 commit comments

Comments
 (0)