diff options
author | Charles Blake <cb@cblake.net> | 2018-07-13 09:54:51 -0400 |
---|---|---|
committer | Andrew Gallant <jamslam@gmail.com> | 2018-07-21 17:25:12 -0400 |
commit | 231456c409ff38c75c39d01b781b569965ddf808 (patch) | |
tree | 592b89acd3172b0c23acdfe6113bcd70ea11aa3a /src/worker.rs | |
parent | 1d09d4d31ba3ac2eb09edf31e8ec46b2b5cec388 (diff) |
ripgrep: add --pre flag
The preprocessor flag accepts a command program and executes this
program for every input file that is searched. Instead of searching the
file directly, ripgrep will instead search the stdout contents of the
program.
Closes #978, Closes #981
Diffstat (limited to 'src/worker.rs')
-rw-r--r-- | src/worker.rs | 24 |
1 files changed, 22 insertions, 2 deletions
diff --git a/src/worker.rs b/src/worker.rs index a8327cda..5b7ef0a4 100644 --- a/src/worker.rs +++ b/src/worker.rs @@ -1,6 +1,6 @@ use std::fs::File; use std::io; -use std::path::Path; +use std::path::{Path, PathBuf}; use encoding_rs::Encoding; use grep::Grep; @@ -10,6 +10,7 @@ use termcolor::WriteColor; use decoder::DecodeReader; use decompressor::{self, DecompressionReader}; +use preprocessor::PreprocessorReader; use pathutil::strip_prefix; use printer::Printer; use search_buffer::BufferSearcher; @@ -45,6 +46,7 @@ struct Options { no_messages: bool, quiet: bool, text: bool, + preprocessor: Option<PathBuf>, search_zip_files: bool } @@ -68,6 +70,7 @@ impl Default for Options { quiet: false, text: false, search_zip_files: false, + preprocessor: None, } } } @@ -222,6 +225,12 @@ impl WorkerBuilder { self.opts.search_zip_files = yes; self } + + /// If non-empty, search output of preprocessor run on each file + pub fn preprocessor(mut self, command: Option<PathBuf>) -> Self { + self.opts.preprocessor = command; + self + } } /// Worker is responsible for executing searches on file paths, while choosing @@ -250,7 +259,18 @@ impl Worker { } Work::DirEntry(dent) => { let mut path = dent.path(); - if self.opts.search_zip_files + if self.opts.preprocessor.is_some() { + let cmd = self.opts.preprocessor.clone().unwrap(); + match PreprocessorReader::from_cmd_path(cmd, path) { + Ok(reader) => self.search(printer, path, reader), + Err(err) => { + if !self.opts.no_messages { + eprintln!("{}", err); + } + return 0; + } + } + } else if self.opts.search_zip_files && decompressor::is_compressed(path) { match DecompressionReader::from_path(path) { |