summaryrefslogtreecommitdiffstats
path: root/src/worker.rs
diff options
context:
space:
mode:
authorBalaji Sivaraman <balaji@balajisivaraman.com>2018-02-21 22:16:45 +0530
committerAndrew Gallant <jamslam@gmail.com>2018-03-10 10:15:19 -0500
commitb006943c01561a4ae5b928081d1bb4087b599e19 (patch)
tree88c402f4edc91b808260c9c7c9ea4e0a5790c089 /src/worker.rs
parent91d0756f62790356012d692a7b340df92b54beac (diff)
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
Diffstat (limited to 'src/worker.rs')
-rw-r--r--src/worker.rs14
1 files changed, 14 insertions, 0 deletions
diff --git a/src/worker.rs b/src/worker.rs
index 952a334b..e5f7546a 100644
--- a/src/worker.rs
+++ b/src/worker.rs
@@ -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)