Skip to content

Commit ed6d1c5

Browse files
committed
feat: add ignore flag for ignoring directories
1 parent 65cd829 commit ed6d1c5

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

src/main.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use ::std::sync::Arc;
2222
use ::std::thread::park_timeout;
2323
use ::std::{thread, time};
2424
use ::structopt::StructOpt;
25+
use nix::dir;
2526

2627
use ::tui::backend::Backend;
2728
use crossterm::event::KeyModifiers;
@@ -53,6 +54,9 @@ pub struct Opt {
5354
/// The folder to scan
5455
folder: Option<PathBuf>,
5556
#[structopt(short, long)]
57+
// The directories to ignore
58+
ignore: Vec<PathBuf>,
59+
#[structopt(short, long)]
5660
/// Show file sizes rather than their block usage on disk
5761
apparent_size: bool,
5862
#[structopt(short, long)]
@@ -85,10 +89,13 @@ fn try_main() -> Result<(), failure::Error> {
8589
if !folder.as_path().is_dir() {
8690
failure::bail!("Folder '{}' does not exist", folder.to_string_lossy())
8791
}
92+
// TODO: maybe validate the ignore flag
93+
8894
start(
8995
terminal_backend,
9096
Box::new(terminal_events),
9197
folder,
98+
opts.ignore,
9299
opts.apparent_size,
93100
opts.disable_delete_confirmation,
94101
);
@@ -103,6 +110,7 @@ pub fn start<B>(
103110
terminal_backend: B,
104111
terminal_events: Box<dyn Iterator<Item = BackEvent> + Send>,
105112
path: PathBuf,
113+
ignore: Vec<PathBuf>,
106114
show_apparent_size: bool,
107115
disable_delete_confirmation: bool,
108116
) where
@@ -188,6 +196,30 @@ pub fn start<B>(
188196
let loaded = loaded.clone();
189197
move || {
190198
'scanning: for entry in WalkDir::new(&path)
199+
.process_read_dir({
200+
let ignore = ignore.clone();
201+
move |_read_dir_state, children| {
202+
if !ignore.is_empty() {
203+
children.retain(|dir_entry_result| {
204+
dir_entry_result
205+
.as_ref()
206+
.map(|dir_entry| {
207+
let entry_path = dir_entry.path();
208+
let entry_absolute =
209+
entry_path.canonicalize().unwrap_or(entry_path);
210+
!ignore.iter().any(|ignore_path| {
211+
let ignore_absolute =
212+
ignore_path.canonicalize().unwrap_or_else(
213+
|_| ignore_path.to_path_buf(),
214+
);
215+
entry_absolute.starts_with(&ignore_absolute)
216+
})
217+
})
218+
.unwrap_or(false)
219+
});
220+
}
221+
}
222+
})
191223
.parallelism(if SHOULD_SCAN_HD_FILES_IN_MULTIPLE_THREADS {
192224
RayonDefaultPool
193225
} else {

0 commit comments

Comments
 (0)