Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve find example to use WalkParallel #37

Merged
merged 1 commit into from
Dec 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions examples/find.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@
//! matching can be done while the picker is populated.
use std::{borrow::Cow, env::args, io, path::PathBuf, thread::spawn};

use ignore::{DirEntry, Walk};
use ignore::{DirEntry, WalkBuilder, WalkState};
use nucleo_picker::{nucleo::Config, PickerOptions, Render};

#[derive(Clone)]
pub struct DirEntryRender;

impl Render<DirEntry> for DirEntryRender {
type Str<'a> = Cow<'a, str>;

// Render a `DirEntry` using its internal path buffer.
/// Render a `DirEntry` using its internal path buffer.
fn render<'a>(&self, value: &'a DirEntry) -> Self::Str<'a> {
value.path().to_string_lossy()
}
Expand All @@ -36,9 +35,15 @@ fn main() -> io::Result<()> {
// populate from a separate thread to avoid locking the picker interface
let injector = picker.injector();
spawn(move || {
for entry in Walk::new(root).filter_map(Result::ok) {
injector.push(entry);
}
WalkBuilder::new(root).build_parallel().run(|| {
let injector = injector.clone();
Box::new(move |walk_res| {
if let Ok(dir) = walk_res {
injector.push(dir);
}
WalkState::Continue
})
});
});

match picker.pick()? {
Expand Down
Loading