Skip to content

Commit

Permalink
Hack in Windows console coloring.
Browse files Browse the repository at this point in the history
The code has suffered and needs refactoring/commenting. BUT... IT WORKS!
  • Loading branch information
BurntSushi committed Sep 8, 2016
1 parent ca058d7 commit 0042dce
Show file tree
Hide file tree
Showing 6 changed files with 215 additions and 31 deletions.
5 changes: 3 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ use std::thread;
use crossbeam::sync::chase_lev::{self, Steal, Stealer};
use grep::Grep;
use memmap::{Mmap, Protection};
use term::Terminal;
use walkdir::DirEntry;

use args::Args;
Expand Down Expand Up @@ -198,11 +199,11 @@ impl Worker {
let mut printer = self.args.printer(outbuf);
self.do_work(&mut printer, work);
let outbuf = printer.into_inner();
if !outbuf.is_empty() {
if !outbuf.get_ref().is_empty() {
let mut out = self.out.lock().unwrap();
out.write(&outbuf);
}
self.outbuf = Some(outbuf);
self.outbuf = Some(outbuf.into_inner());
}
self.match_count
}
Expand Down
44 changes: 42 additions & 2 deletions src/out.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
use std::io::{self, Write};

use term::{StdoutTerminal, Terminal};
#[cfg(windows)]
use term::WinConsole;

use printer::Writer;

/// Out controls the actual output of all search results for a particular file
/// to the end user.
///
Expand All @@ -8,15 +14,32 @@ use std::io::{self, Write};
/// file as a whole. For example, it knows when to print a file separator.)
pub struct Out<W: io::Write> {
wtr: io::BufWriter<W>,
term: Option<Box<StdoutTerminal>>,
printed: bool,
file_separator: Option<Vec<u8>>,
}

/// This is like term::stdout, but on Windows always uses WinConsole instead
/// of trying for a TerminfoTerminal. This may be a mistake.
#[cfg(windows)]
fn term_stdout() -> Option<Box<StdoutTerminal>> {
WinConsole::new(io::stdout())
.ok()
.map(|t| Box::new(t) as Box<StdoutTerminal>)
}

#[cfg(not(windows))]
fn term_stdout() -> Option<Box<StdoutTerminal>> {
// We never use this crap on *nix.
None
}

impl<W: io::Write> Out<W> {
/// Create a new Out that writes to the wtr given.
pub fn new(wtr: W) -> Out<W> {
Out {
wtr: io::BufWriter::new(wtr),
term: term_stdout(),
printed: false,
file_separator: None,
}
Expand All @@ -33,14 +56,31 @@ impl<W: io::Write> Out<W> {

/// Write the search results of a single file to the underlying wtr and
/// flush wtr.
pub fn write(&mut self, buf: &[u8]) {
pub fn write(&mut self, buf: &Writer<Vec<u8>>) {
if let Some(ref sep) = self.file_separator {
if self.printed {
let _ = self.wtr.write_all(sep);
let _ = self.wtr.write_all(b"\n");
}
}
let _ = self.wtr.write_all(buf);
match *buf {
Writer::Colored(ref tt) => {
let _ = self.wtr.write_all(tt.get_ref());
}
Writer::Windows(ref w) => {
match self.term {
None => {
let _ = self.wtr.write_all(w.get_ref());
}
Some(ref mut stdout) => {
w.print_stdout(stdout);
}
}
}
Writer::NoColor(ref buf) => {
let _ = self.wtr.write_all(buf);
}
}
let _ = self.wtr.flush();
self.printed = true;
}
Expand Down
Loading

0 comments on commit 0042dce

Please sign in to comment.