2016-09-05 00:52:23 -04:00
|
|
|
use std::io::{self, Write};
|
|
|
|
|
2016-09-07 21:54:28 -04:00
|
|
|
use term::{StdoutTerminal, Terminal};
|
|
|
|
#[cfg(windows)]
|
|
|
|
use term::WinConsole;
|
|
|
|
|
|
|
|
use printer::Writer;
|
|
|
|
|
2016-09-05 00:52:23 -04:00
|
|
|
/// Out controls the actual output of all search results for a particular file
|
|
|
|
/// to the end user.
|
|
|
|
///
|
|
|
|
/// (The difference between Out and Printer is that a Printer works with
|
|
|
|
/// individual search results where as Out works with search results for each
|
|
|
|
/// file as a whole. For example, it knows when to print a file separator.)
|
|
|
|
pub struct Out<W: io::Write> {
|
|
|
|
wtr: io::BufWriter<W>,
|
2016-09-07 21:54:28 -04:00
|
|
|
term: Option<Box<StdoutTerminal>>,
|
2016-09-05 00:52:23 -04:00
|
|
|
printed: bool,
|
2016-09-05 17:36:41 -04:00
|
|
|
file_separator: Option<Vec<u8>>,
|
2016-09-05 00:52:23 -04:00
|
|
|
}
|
|
|
|
|
2016-09-07 21:54:28 -04:00
|
|
|
/// 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
|
|
|
|
}
|
|
|
|
|
2016-09-05 00:52:23 -04:00
|
|
|
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),
|
2016-09-07 21:54:28 -04:00
|
|
|
term: term_stdout(),
|
2016-09-05 00:52:23 -04:00
|
|
|
printed: false,
|
2016-09-05 17:36:41 -04:00
|
|
|
file_separator: None,
|
2016-09-05 00:52:23 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// If set, the separator is printed between matches from different files.
|
|
|
|
/// By default, no separator is printed.
|
|
|
|
///
|
|
|
|
/// If sep is empty, then no file separator is printed.
|
|
|
|
pub fn file_separator(mut self, sep: Vec<u8>) -> Out<W> {
|
2016-09-05 17:36:41 -04:00
|
|
|
self.file_separator = Some(sep);
|
2016-09-05 00:52:23 -04:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Write the search results of a single file to the underlying wtr and
|
|
|
|
/// flush wtr.
|
2016-09-07 21:54:28 -04:00
|
|
|
pub fn write(&mut self, buf: &Writer<Vec<u8>>) {
|
2016-09-05 17:36:41 -04:00
|
|
|
if let Some(ref sep) = self.file_separator {
|
|
|
|
if self.printed {
|
|
|
|
let _ = self.wtr.write_all(sep);
|
|
|
|
let _ = self.wtr.write_all(b"\n");
|
|
|
|
}
|
2016-09-05 00:52:23 -04:00
|
|
|
}
|
2016-09-07 21:54:28 -04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2016-09-05 00:52:23 -04:00
|
|
|
let _ = self.wtr.flush();
|
|
|
|
self.printed = true;
|
|
|
|
}
|
|
|
|
}
|