Skip to content
Open
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
28 changes: 28 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ pub struct Opt {
/// The folder to scan
folder: Option<PathBuf>,
#[structopt(short, long)]
/// The directories to ignore
ignore: Vec<PathBuf>,

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have opted to use an ignore option but wondering if we would prefer to use exclude with shorthand x, I have seen that preference voiced in other issues.

#[structopt(short, long)]
/// Show file sizes rather than their block usage on disk
apparent_size: bool,
#[structopt(short, long)]
Expand Down Expand Up @@ -85,10 +88,16 @@ fn try_main() -> Result<(), failure::Error> {
if !folder.as_path().is_dir() {
failure::bail!("Folder '{}' does not exist", folder.to_string_lossy())
}
for ignore_path in &opts.ignore {
if !ignore_path.as_path().is_dir() {
failure::bail!("Ignored path '{}' is not a directory", ignore_path.to_string_lossy())
}
}
start(
terminal_backend,
Box::new(terminal_events),
folder,
opts.ignore,
opts.apparent_size,
opts.disable_delete_confirmation,
);
Expand All @@ -103,6 +112,7 @@ pub fn start<B>(
terminal_backend: B,

@elliottcrif elliottcrif Dec 28, 2024 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently, the start function takes a growing list of parameters for various options and flags (e.g., show_apparent_size, disable_delete_confirmation). As we continue to add more options, this signature risks becoming unwieldy and harder to maintain. The challenge became especially apparent when updating the UI tests, which required significant changes due to the expanded parameter list.

To address this, I’ve started a WIP implementation that uses the Opt struct as the last argument in start. This encapsulates all optional parameters into a single structure, aiming to reduce churn when adding or modifying options.

I’d appreciate input on the following:

  • Is encapsulating options in a configuration struct like Opt an idiomatic approach in Rust?
  • Would a builder pattern provide better flexibility and maintainability for this use case?
  • Are there other patterns or techniques in Rust that would align well with the goal of minimizing signature changes?

I’m especially interested in feedback on how this impacts code readability, maintainability, and idiomatic usage in Rust.

terminal_events: Box<dyn Iterator<Item = BackEvent> + Send>,
path: PathBuf,
ignore: Vec<PathBuf>,
show_apparent_size: bool,
disable_delete_confirmation: bool,
) where
Expand Down Expand Up @@ -188,6 +198,24 @@ pub fn start<B>(
let loaded = loaded.clone();
move || {
'scanning: for entry in WalkDir::new(&path)
.process_read_dir({
let ignore = ignore.clone();
move |_read_dir_state, children| {
if !ignore.is_empty() {
children.retain(|dir_entry_result| {
dir_entry_result
.as_ref()
.map(|dir_entry| {
let entry_path = dir_entry.path();
!ignore.iter().any(|ignore_path| {
entry_path.starts_with(&ignore_path)
})
})
.unwrap_or(false)
});
}
}
})
.parallelism(if SHOULD_SCAN_HD_FILES_IN_MULTIPLE_THREADS {
RayonDefaultPool
} else {
Expand Down