Skip to content

Commit 1459fac

Browse files
author
Scrim
committed
feat: add flag to exclude directories from filtering
1 parent 2403afb commit 1459fac

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

src/builder.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ pub struct SearchBuilder {
2525
hidden: bool,
2626
/// Filters Vector, defaults to empty vec
2727
filters: Vec<FilterType>,
28+
/// Apply filters to files and directories or files only.
29+
filter_dirs: bool,
2830
}
2931

3032
impl SearchBuilder {
@@ -42,6 +44,7 @@ impl SearchBuilder {
4244
self.ignore_case,
4345
self.hidden,
4446
self.filters.clone(),
47+
self.filter_dirs,
4548
)
4649
}
4750

@@ -233,6 +236,11 @@ impl SearchBuilder {
233236
);
234237
self
235238
}
239+
240+
pub fn filter_dirs(mut self, value: bool) -> Self {
241+
self.filter_dirs = value;
242+
self
243+
}
236244
}
237245

238246
impl Default for SearchBuilder {
@@ -249,6 +257,7 @@ impl Default for SearchBuilder {
249257
ignore_case: false,
250258
hidden: false,
251259
filters: vec![],
260+
filter_dirs: true,
252261
}
253262
}
254263
}

src/filter.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@ pub enum FilterType {
1414
}
1515

1616
impl FilterType {
17-
pub fn apply(&self, dir: &DirEntry) -> bool {
17+
pub fn apply(&self, dir: &DirEntry, filter_dirs: bool) -> bool {
1818
if let Ok(m) = dir.metadata() {
19+
if !filter_dirs && m.file_type().is_dir() {
20+
return true;
21+
}
1922
match self {
2023
Self::Created(cmp, time) => {
2124
if let Ok(created) = m.created() {

src/search.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ impl Search {
7171
ignore_case: bool,
7272
with_hidden: bool,
7373
filters: Vec<FilterType>,
74+
filter_dirs: bool,
7475
) -> Self {
7576
let regex_search_input =
7677
utils::build_regex_search_input(search_input, file_ext, strict, ignore_case);
@@ -85,7 +86,7 @@ impl Search {
8586

8687
// filters getting applied to walker
8788
// only if all filters are true then the walker will return the file
88-
walker.filter_entry(move |dir| filters.iter().all(|f| f.apply(dir)));
89+
walker.filter_entry(move |dir| filters.iter().all(|f| f.apply(dir, filter_dirs)));
8990

9091
if let Some(locations) = more_locations {
9192
for location in locations {

0 commit comments

Comments
 (0)