parent
ef339297c3
commit
95fe74e395
1 changed files with 21 additions and 9 deletions
30
src/main.rs
30
src/main.rs
|
@ -3,7 +3,7 @@ use std::{
|
|||
io::{self, Write},
|
||||
path::{Path, PathBuf},
|
||||
process,
|
||||
sync::OnceLock,
|
||||
sync::{Arc, Mutex, OnceLock},
|
||||
thread,
|
||||
};
|
||||
|
||||
|
@ -140,14 +140,18 @@ fn main() {
|
|||
println!("Invalid answer");
|
||||
}
|
||||
|
||||
println!("Selected: {}", actual_selection.join(", "));
|
||||
|
||||
let mut thread_pool: Vec<thread::JoinHandle<()>> = Vec::new();
|
||||
|
||||
let total_files_sorted: Arc<Mutex<usize>> = Arc::new(Mutex::new(0));
|
||||
|
||||
for selection in actual_selection {
|
||||
let tfs = Arc::clone(&total_files_sorted);
|
||||
let thread = thread::spawn(move || match sort_files(selection, recursive) {
|
||||
Ok(_) => {}
|
||||
Err(err) => eprintln!("{}", err),
|
||||
Ok(files_sorted) => {
|
||||
let mut num = tfs.lock().unwrap();
|
||||
*num += files_sorted
|
||||
}
|
||||
Err(err) => eprintln!("Error sorting files: {}", err),
|
||||
});
|
||||
|
||||
thread_pool.push(thread);
|
||||
|
@ -156,9 +160,15 @@ fn main() {
|
|||
for thread in thread_pool {
|
||||
thread.join().unwrap();
|
||||
}
|
||||
|
||||
if *total_files_sorted.lock().unwrap() == 0 {
|
||||
println!("No sortable files found!");
|
||||
} else {
|
||||
println!("Sorted files: {}", total_files_sorted.lock().unwrap());
|
||||
}
|
||||
}
|
||||
|
||||
fn sort_files(selection: String, recursive: bool) -> Result<(), Box<dyn std::error::Error>> {
|
||||
fn sort_files(selection: String, recursive: bool) -> Result<usize, Box<dyn std::error::Error>> {
|
||||
let search_path = CONFIG
|
||||
.get()
|
||||
.and_then(|config| config.sources.get(&selection))
|
||||
|
@ -192,7 +202,8 @@ fn sort_files(selection: String, recursive: bool) -> Result<(), Box<dyn std::err
|
|||
path: &std::path::Path,
|
||||
file_types: &[(&str, Vec<&str>, Option<std::path::PathBuf>)],
|
||||
recursive: bool,
|
||||
) -> Result<(), Box<dyn std::error::Error>> {
|
||||
) -> Result<usize, Box<dyn std::error::Error>> {
|
||||
let mut files_sorted: usize = 0;
|
||||
for entry in fs::read_dir(path)?.flatten() {
|
||||
let metadata = entry.metadata()?;
|
||||
|
||||
|
@ -204,17 +215,18 @@ fn sort_files(selection: String, recursive: bool) -> Result<(), Box<dyn std::err
|
|||
if let Some(target_dir) = target_dir_option {
|
||||
if valid_extensions.contains(&extension) {
|
||||
move_file_to_directory(&file_path, target_dir);
|
||||
files_sorted += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if metadata.is_dir() && recursive {
|
||||
// Recurse into the directory
|
||||
process_directory(&entry.path(), file_types, recursive)?;
|
||||
files_sorted += process_directory(&entry.path(), file_types, recursive)?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
Ok(files_sorted)
|
||||
}
|
||||
|
||||
process_directory(&search_path, &file_types, recursive)
|
||||
|
|
Loading…
Reference in a new issue