Skip to content

Commit

Permalink
Report on time elapsed and matches found
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
feds01 committed Dec 6, 2024
1 parent 89a1a51 commit ffa135d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
27 changes: 19 additions & 8 deletions crates/teamsearch/src/commands/find.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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)
},
)?;

Expand All @@ -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)
Expand All @@ -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(())
}

Expand All @@ -97,12 +99,21 @@ fn find_matches(
matcher: &RegexMatcher,
path: &PathBuf,
printer: &mut Standard<StandardStream>,
) -> Result<()> {
) -> Result<Stats> {
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)
}
}
1 change: 1 addition & 0 deletions crates/teamsearch_workspace/src/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit ffa135d

Please sign in to comment.