2016-08-28 07:37:12 +02:00
|
|
|
use std::io;
|
|
|
|
use std::path::Path;
|
|
|
|
|
|
|
|
macro_rules! wln {
|
|
|
|
($($tt:tt)*) => {
|
|
|
|
let _ = writeln!($($tt)*);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-30 04:44:15 +02:00
|
|
|
macro_rules! w {
|
|
|
|
($($tt:tt)*) => {
|
|
|
|
let _ = write!($($tt)*);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-28 07:37:12 +02:00
|
|
|
pub struct Printer<W> {
|
|
|
|
wtr: W,
|
2016-09-02 03:56:23 +02:00
|
|
|
has_printed: bool,
|
2016-08-28 07:37:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<W: io::Write> Printer<W> {
|
|
|
|
pub fn new(wtr: W) -> Printer<W> {
|
|
|
|
Printer {
|
|
|
|
wtr: wtr,
|
2016-09-02 03:56:23 +02:00
|
|
|
has_printed: false,
|
2016-08-28 07:37:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-02 03:56:23 +02:00
|
|
|
pub fn has_printed(&self) -> bool {
|
|
|
|
self.has_printed
|
|
|
|
}
|
|
|
|
|
2016-08-28 07:37:12 +02:00
|
|
|
pub fn into_inner(self) -> W {
|
|
|
|
self.wtr
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn path<P: AsRef<Path>>(&mut self, path: P) {
|
|
|
|
wln!(&mut self.wtr, "{}", path.as_ref().display());
|
|
|
|
}
|
|
|
|
|
2016-08-29 02:18:34 +02:00
|
|
|
pub fn path_count<P: AsRef<Path>>(&mut self, path: P, count: u64) {
|
|
|
|
wln!(&mut self.wtr, "{}:{}", path.as_ref().display(), count);
|
|
|
|
}
|
|
|
|
|
2016-08-28 07:37:12 +02:00
|
|
|
pub fn count(&mut self, count: u64) {
|
|
|
|
wln!(&mut self.wtr, "{}", count);
|
|
|
|
}
|
|
|
|
|
2016-09-02 03:56:23 +02:00
|
|
|
pub fn context_separator(&mut self) {
|
|
|
|
wln!(&mut self.wtr, "--");
|
|
|
|
}
|
|
|
|
|
2016-08-28 07:37:12 +02:00
|
|
|
pub fn matched<P: AsRef<Path>>(
|
|
|
|
&mut self,
|
|
|
|
path: P,
|
|
|
|
buf: &[u8],
|
2016-08-30 04:44:15 +02:00
|
|
|
start: usize,
|
|
|
|
end: usize,
|
|
|
|
line_number: Option<u64>,
|
2016-08-28 07:37:12 +02:00
|
|
|
) {
|
2016-08-30 04:44:15 +02:00
|
|
|
self.write(path.as_ref().to_string_lossy().as_bytes());
|
|
|
|
self.write(b":");
|
|
|
|
if let Some(line_number) = line_number {
|
|
|
|
self.write(line_number.to_string().as_bytes());
|
|
|
|
self.write(b":");
|
|
|
|
}
|
|
|
|
self.write(&buf[start..end]);
|
|
|
|
self.write(b"\n");
|
2016-08-28 07:37:12 +02:00
|
|
|
}
|
|
|
|
|
2016-09-01 02:02:59 +02:00
|
|
|
pub fn context<P: AsRef<Path>>(
|
|
|
|
&mut self,
|
|
|
|
path: P,
|
|
|
|
buf: &[u8],
|
|
|
|
start: usize,
|
|
|
|
end: usize,
|
|
|
|
line_number: Option<u64>,
|
|
|
|
) {
|
|
|
|
self.write(path.as_ref().to_string_lossy().as_bytes());
|
|
|
|
self.write(b"-");
|
|
|
|
if let Some(line_number) = line_number {
|
|
|
|
self.write(line_number.to_string().as_bytes());
|
|
|
|
self.write(b"-");
|
|
|
|
}
|
|
|
|
self.write(&buf[start..end]);
|
|
|
|
self.write(b"\n");
|
|
|
|
}
|
|
|
|
|
2016-08-28 07:37:12 +02:00
|
|
|
pub fn binary_matched<P: AsRef<Path>>(&mut self, path: P) {
|
|
|
|
wln!(&mut self.wtr, "binary file {} matches", path.as_ref().display());
|
|
|
|
}
|
2016-08-30 04:44:15 +02:00
|
|
|
|
|
|
|
fn write(&mut self, buf: &[u8]) {
|
2016-09-02 03:56:23 +02:00
|
|
|
self.has_printed = true;
|
2016-08-30 04:44:15 +02:00
|
|
|
let _ = self.wtr.write_all(buf);
|
|
|
|
}
|
2016-08-28 07:37:12 +02:00
|
|
|
}
|