diff options
author | Andrew Gallant <jamslam@gmail.com> | 2016-09-26 19:21:17 -0400 |
---|---|---|
committer | Andrew Gallant <jamslam@gmail.com> | 2016-09-26 19:21:17 -0400 |
commit | 7a3fd1f23fe7a4d679fb1fb7d13884a653e5d4f5 (patch) | |
tree | 22e9bf417c21f52ba39590825b49d02b01b72855 /src/printer.rs | |
parent | d30640344090fc3ffde6d58e0c5a4c928fb00990 (diff) |
Add a --null flag.
This flag causes a NUL byte to follow any file path in ripgrep's output.
Closes #89.
Diffstat (limited to 'src/printer.rs')
-rw-r--r-- | src/printer.rs | 41 |
1 files changed, 36 insertions, 5 deletions
diff --git a/src/printer.rs b/src/printer.rs index 0645b7e4..07c96754 100644 --- a/src/printer.rs +++ b/src/printer.rs @@ -33,6 +33,9 @@ pub struct Printer<W> { line_per_match: bool, /// Whether to suppress all output. quiet: bool, + /// Whether to print NUL bytes after a file path instead of new lines + /// or `:`. + null: bool, /// A string to use as a replacement of each match in a matching line. replace: Option<Vec<u8>>, /// Whether to prefix each match with the corresponding file name. @@ -51,6 +54,7 @@ impl<W: Terminal + Send> Printer<W> { heading: false, line_per_match: false, quiet: false, + null: false, replace: None, with_filename: false, } @@ -89,6 +93,13 @@ impl<W: Terminal + Send> Printer<W> { self } + /// Whether to cause NUL bytes to follow file paths instead of other + /// visual separators (like `:`, `-` and `\n`). + pub fn null(mut self, yes: bool) -> Printer<W> { + self.null = yes; + self + } + /// When set, all output is suppressed. pub fn quiet(mut self, yes: bool) -> Printer<W> { self.quiet = yes; @@ -146,14 +157,22 @@ impl<W: Terminal + Send> Printer<W> { pub fn path<P: AsRef<Path>>(&mut self, path: P) { let path = strip_prefix("./", path.as_ref()).unwrap_or(path.as_ref()); self.write_path(path); - self.write_eol(); + if self.null { + self.write(b"\x00"); + } else { + self.write_eol(); + } } /// Prints the given path and a count of the number of matches found. pub fn path_count<P: AsRef<Path>>(&mut self, path: P, count: u64) { if self.with_filename { self.write_path(path); - self.write(b":"); + if self.null { + self.write(b"\x00"); + } else { + self.write(b":"); + } } self.write(count.to_string().as_bytes()); self.write_eol(); @@ -214,7 +233,11 @@ impl<W: Terminal + Send> Printer<W> { self.write_heading(path.as_ref()); } else if !self.heading && self.with_filename { self.write_path(path.as_ref()); - self.write(b":"); + if self.null { + self.write(b"\x00"); + } else { + self.write(b":"); + } } if let Some(line_number) = line_number { self.line_number(line_number, b':'); @@ -264,7 +287,11 @@ impl<W: Terminal + Send> Printer<W> { self.write_heading(path.as_ref()); } else if !self.heading && self.with_filename { self.write_path(path.as_ref()); - self.write(b"-"); + if self.null { + self.write(b"\x00"); + } else { + self.write(b"-"); + } } if let Some(line_number) = line_number { self.line_number(line_number, b'-'); @@ -281,7 +308,11 @@ impl<W: Terminal + Send> Printer<W> { let _ = self.wtr.attr(Attr::Bold); } self.write_path(path.as_ref()); - self.write_eol(); + if self.null { + self.write(b"\x00"); + } else { + self.write_eol(); + } if self.wtr.supports_color() { let _ = self.wtr.reset(); } |