Omit files and print only match options added

These are essential for using the tool for scripting purposes.
This commit is contained in:
Cameron Cordes
2020-05-20 17:54:46 -04:00
parent 1229c05d8d
commit a403dbda76
2 changed files with 28 additions and 4 deletions

View File

@@ -7,6 +7,8 @@ pub struct Config {
pub ignore_case: bool, pub ignore_case: bool,
pub path: String, pub path: String,
pub use_gitignore: bool, pub use_gitignore: bool,
pub omit_filenames: bool,
pub match_only: bool,
} }
impl Config { impl Config {
@@ -36,6 +38,16 @@ impl Config {
.long("no-gitignore") .long("no-gitignore")
.help("Include results that are specified in .gitignore"), .help("Include results that are specified in .gitignore"),
) )
.arg(
Arg::with_name("omit filenames")
.short("h")
.help("Omit file names in results"),
)
.arg(
Arg::with_name("only match")
.short("o")
.help("Only print matched text")
)
.get_matches(); .get_matches();
Config { Config {
@@ -43,6 +55,8 @@ impl Config {
ignore_case: matches.is_present("ignore case"), ignore_case: matches.is_present("ignore case"),
path: String::from(matches.value_of("path").unwrap()), path: String::from(matches.value_of("path").unwrap()),
use_gitignore: !matches.is_present("gitignore"), use_gitignore: !matches.is_present("gitignore"),
omit_filenames: matches.is_present("omit filenames"),
match_only: matches.is_present("only match"),
} }
} }
} }

View File

@@ -43,23 +43,33 @@ impl<'a> Printer for SimplePrinter<'a> {
impl<'a> SimplePrinter<'a> { impl<'a> SimplePrinter<'a> {
fn print_file_name(&self) { fn print_file_name(&self) {
if !self.config.omit_filenames {
println!("{}:", self.file_match.file_name); println!("{}:", self.file_match.file_name);
} }
}
fn print_linenumber(&self, mat: &Match) { fn print_linenumber(&self, mat: &Match) {
if !self.config.omit_filenames {
print!("{}: ", mat.line_number); print!("{}: ", mat.line_number);
} }
}
fn print_first_part_of_line(&self, mat: &Match, current_position: usize, match_start: usize) { fn print_first_part_of_line(&self, mat: &Match, current_position: usize, match_start: usize) {
if !self.config.match_only {
print!("{}", mat.text.get(current_position..match_start).unwrap()); print!("{}", mat.text.get(current_position..match_start).unwrap());
} }
}
fn print_match(&self, mat: &Match, index: &MatchIndex) { fn print_match(&self, mat: &Match, index: &MatchIndex) {
print!("{}", mat.text.get(index.start..index.end).unwrap()); print!("{}", mat.text.get(index.start..index.end).unwrap());
} }
fn print_end_of_line(&self, mat: &Match, index: &MatchIndex) { fn print_end_of_line(&self, mat: &Match, index: &MatchIndex) {
if !self.config.match_only {
println!("{}", mat.text.get(index.end..mat.text.len()).unwrap()); println!("{}", mat.text.get(index.end..mat.text.len()).unwrap());
} else {
println!();
}
} }
} }