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,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<W: io::Write> Printer<W> {
|
|
|
|
pub fn new(wtr: W) -> Printer<W> {
|
|
|
|
Printer {
|
|
|
|
wtr: wtr,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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]) {
|
|
|
|
let _ = self.wtr.write_all(buf);
|
|
|
|
}
|
2016-08-28 07:37:12 +02:00
|
|
|
}
|