From ffa135d2e8995c54a0d1d494c1675366d7a5fca8 Mon Sep 17 00:00:00 2001 From: Alexander Fedotov Date: Fri, 6 Dec 2024 15:42:33 +0000 Subject: [PATCH] Report on time elapsed and matches found This commit adds a report on the time elapsed and the number of matches found after a search is completed. The report is printed to the console. --- crates/teamsearch/src/commands/find.rs | 27 +++++++++++++++------ crates/teamsearch_workspace/src/resolver.rs | 1 + 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/crates/teamsearch/src/commands/find.rs b/crates/teamsearch/src/commands/find.rs index c0403c8..a1380fb 100644 --- a/crates/teamsearch/src/commands/find.rs +++ b/crates/teamsearch/src/commands/find.rs @@ -1,9 +1,9 @@ //! Implementation of the `find` command. -use std::{fs::File, path::PathBuf}; +use std::{fs::File, path::PathBuf, time::Instant}; use anyhow::Result; -use grep_printer::{ColorSpecs, Standard, StandardBuilder}; +use grep_printer::{ColorSpecs, Standard, StandardBuilder, Stats}; use grep_regex::RegexMatcher; use grep_searcher::SearcherBuilder; use itertools::Itertools; @@ -58,7 +58,7 @@ pub fn find( || find_files_in_paths(files, &settings), log::Level::Info, |duration, result| { - info!("Resolved {} files in {:?}", result.as_ref().map_or(0, |f| f.len()), duration) + info!("resolved {} files in {:?}", result.as_ref().map_or(0, |f| f.len()), duration) }, )?; @@ -70,6 +70,7 @@ pub fn find( // @@Todo: integrate a cache system here, we should be able to avoid re-linting // already existent files and just skip them. let matcher = RegexMatcher::new(pattern.as_str())?; + let mut stats = Stats::new(); let mut printer = StandardBuilder::new() .heading(true) @@ -81,11 +82,12 @@ pub fn find( for entry in files { match entry? { ResolvedFile::Nested(file) | ResolvedFile::Root(file) => { - let _ = find_matches(&matcher, &file, &mut printer); + stats += find_matches(&matcher, &file, &mut printer)?; } } } + info!("found {} matches in {:?}", stats.matches(), stats.elapsed()); Ok(()) } @@ -97,12 +99,21 @@ fn find_matches( matcher: &RegexMatcher, path: &PathBuf, printer: &mut Standard, -) -> Result<()> { +) -> Result { + let start = Instant::now(); let mut searcher = SearcherBuilder::new().multi_line(true).before_context(1).after_context(1).build(); let file = File::open(path)?; - searcher.search_file(matcher, &file, printer.sink_with_path(&matcher, path))?; - - Ok(()) + let mut sink = printer.sink_with_path(&matcher, path); + searcher.search_file(matcher, &file, &mut sink)?; + + if let Some(stats) = sink.stats() { + Ok(stats.clone()) + } else { + // We can at least return the elapsed time. + let mut stats = Stats::new(); + stats.add_elapsed(start.elapsed()); + Ok(stats) + } } diff --git a/crates/teamsearch_workspace/src/resolver.rs b/crates/teamsearch_workspace/src/resolver.rs index fe7f8c9..0b3895e 100644 --- a/crates/teamsearch_workspace/src/resolver.rs +++ b/crates/teamsearch_workspace/src/resolver.rs @@ -143,6 +143,7 @@ impl<'s, 'config> ignore::ParallelVisitor for FilesVisitor<'s, 'config> { if let Some(file_name) = path.file_name() { let file_path = Candidate::new(path); let file_basename = Candidate::new(file_name); + if match_candidate_exclusion( &file_path, &file_basename,