search: add -b/--byte-offset flag

This commit adds support for printing 0-based byte offset before each
line. We handle corner cases such as `-o/--only-matching` and
`-C/--context` as well.

Closes #812
This commit is contained in:
Balaji Sivaraman
2018-02-21 22:16:45 +05:30
committed by Andrew Gallant
parent 91d0756f62
commit b006943c01
8 changed files with 176 additions and 17 deletions

View File

@@ -33,6 +33,7 @@ struct Options {
encoding: Option<&'static Encoding>,
after_context: usize,
before_context: usize,
byte_offset: bool,
count: bool,
files_with_matches: bool,
files_without_matches: bool,
@@ -53,6 +54,7 @@ impl Default for Options {
encoding: None,
after_context: 0,
before_context: 0,
byte_offset: false,
count: false,
files_with_matches: false,
files_without_matches: false,
@@ -106,6 +108,16 @@ impl WorkerBuilder {
self
}
/// If enabled, searching will print a 0-based offset of the
/// matching line (or the actual match if -o is specified) before
/// printing the line itself.
///
/// Disabled by default.
pub fn byte_offset(mut self, yes: bool) -> Self {
self.opts.byte_offset = yes;
self
}
/// If enabled, searching will print a count instead of each match.
///
/// Disabled by default.
@@ -283,6 +295,7 @@ impl Worker {
searcher
.after_context(self.opts.after_context)
.before_context(self.opts.before_context)
.byte_offset(self.opts.byte_offset)
.count(self.opts.count)
.files_with_matches(self.opts.files_with_matches)
.files_without_matches(self.opts.files_without_matches)
@@ -322,6 +335,7 @@ impl Worker {
}
let searcher = BufferSearcher::new(printer, &self.grep, path, buf);
Ok(searcher
.byte_offset(self.opts.byte_offset)
.count(self.opts.count)
.files_with_matches(self.opts.files_with_matches)
.files_without_matches(self.opts.files_without_matches)