Add support for printing column numbers.

This commit is contained in:
Andrew Gallant
2016-09-06 19:50:27 -04:00
parent feff1849c8
commit 5938bed339
2 changed files with 24 additions and 0 deletions

View File

@@ -22,6 +22,8 @@ pub struct Printer<W> {
wtr: Writer<W>,
/// Whether anything has been printed to wtr yet.
has_printed: bool,
/// Whether to show column numbers for the first match or not.
column: bool,
/// The string to use to separate non-contiguous runs of context lines.
context_separator: Vec<u8>,
/// The end-of-line terminator used by the printer. In general, eols are
@@ -48,6 +50,7 @@ impl<W: Send + io::Write> Printer<W> {
Printer {
wtr: Writer::new(wtr, color),
has_printed: false,
column: false,
context_separator: "--".to_string().into_bytes(),
eol: b'\n',
heading: false,
@@ -57,6 +60,13 @@ impl<W: Send + io::Write> Printer<W> {
}
}
/// When set, column numbers will be printed for the first match on each
/// line.
pub fn column(mut self, yes: bool) -> Printer<W> {
self.column = yes;
self
}
/// Set the context separator. The default is `--`.
pub fn context_separator(mut self, sep: Vec<u8>) -> Printer<W> {
self.context_separator = sep;
@@ -173,6 +183,11 @@ impl<W: Send + io::Write> Printer<W> {
if let Some(line_number) = line_number {
self.line_number(line_number, b':');
}
if self.column {
let c = re.find(&buf[start..end]).map(|(s, _)| s + 1).unwrap_or(0);
self.write(c.to_string().as_bytes());
self.write(b":");
}
if self.replace.is_some() {
let line = re.replace_all(
&buf[start..end], &**self.replace.as_ref().unwrap());