use std::io; use std::path::Path; use grep::Match; macro_rules! wln { ($($tt:tt)*) => { let _ = writeln!($($tt)*); } } pub struct Printer { wtr: W, } impl Printer { pub fn new(wtr: W) -> Printer { Printer { wtr: wtr, } } pub fn into_inner(self) -> W { self.wtr } pub fn path>(&mut self, path: P) { wln!(&mut self.wtr, "{}", path.as_ref().display()); } pub fn count(&mut self, count: u64) { wln!(&mut self.wtr, "{}", count); } pub fn matched>( &mut self, path: P, buf: &[u8], m: &Match, ) { let _ = self.wtr.write(path.as_ref().to_string_lossy().as_bytes()); let _ = self.wtr.write(b":"); let _ = self.wtr.write(&buf[m.start()..m.end()]); let _ = self.wtr.write(b"\n"); } pub fn binary_matched>(&mut self, path: P) { wln!(&mut self.wtr, "binary file {} matches", path.as_ref().display()); } }