Compare commits

..

No commits in common. "95fe74e3958d73f4efe3289b97bed861d3735a29" and "2fed2c8223d96091855cb087d3653a5efe0ae139" have entirely different histories.

2 changed files with 20 additions and 39 deletions

View file

@ -48,8 +48,8 @@ impl Config {
.filter(|l| !l.is_empty())
.filter(|l| !l.starts_with('#'))
{
if line.starts_with('[') && line.contains(']') {
current_section = line[1..line.find(']').unwrap()].to_string();
if line.starts_with('[') && line.ends_with(']') {
current_section = line[1..line.len() - 1].to_string();
} else if let Some((key, value)) = line.split_once('=') {
let key_trimmed_string = key.trim().replace('"', "");
let key_trimmed = key_trimmed_string.as_str();
@ -84,14 +84,13 @@ impl Config {
pub fn create(path: &PathBuf) -> Result<(), Box<dyn std::error::Error>> {
let config_str = r#"
[destinations]
pictures_dir = ""
videos_dir = ""
music_dir = ""
books_dir = ""
pictures_dir = "/home/vavakado/Pictures"
videos_dir = "/home/vavakado/Videos"
music_dir = "/home/vavakado/Music"
books_dir = "/home/vavakado/Books"
[sources]
# Put your own sources there like so:
# "Source Name" = "path"
"Pictures" = "/home/vavakado/Pictures"
"#;
std::fs::write(path, config_str)?;

View file

@ -3,7 +3,7 @@ use std::{
io::{self, Write},
path::{Path, PathBuf},
process,
sync::{Arc, Mutex, OnceLock},
sync::OnceLock,
thread,
};
@ -25,6 +25,7 @@ fn main() {
return;
}
if args.len() > 1
&& (args.contains(&"-c".to_string()) || args.contains(&"--config".to_string()))
{
@ -81,7 +82,6 @@ fn main() {
.split(" ")
.filter(|y| !y.is_empty())
.map(|y| y.replace("\n", ""))
.map(|y| y.replace("\r", ""))
.flat_map(|x| x.parse::<usize>())
.collect();
@ -117,8 +117,6 @@ fn main() {
break;
}
println!("Selected: {}", actual_selection.join(", "));
let recursive: bool;
loop {
@ -140,18 +138,14 @@ 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(files_sorted) => {
let mut num = tfs.lock().unwrap();
*num += files_sorted
}
Err(err) => eprintln!("Error sorting files: {}", err),
Ok(_) => {}
Err(err) => eprintln!("{}", err),
});
thread_pool.push(thread);
@ -160,15 +154,9 @@ 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<usize, Box<dyn std::error::Error>> {
fn sort_files(selection: String, recursive: bool) -> Result<(), Box<dyn std::error::Error>> {
let search_path = CONFIG
.get()
.and_then(|config| config.sources.get(&selection))
@ -202,8 +190,7 @@ fn sort_files(selection: String, recursive: bool) -> Result<usize, Box<dyn std::
path: &std::path::Path,
file_types: &[(&str, Vec<&str>, Option<std::path::PathBuf>)],
recursive: bool,
) -> Result<usize, Box<dyn std::error::Error>> {
let mut files_sorted: usize = 0;
) -> Result<(), Box<dyn std::error::Error>> {
for entry in fs::read_dir(path)?.flatten() {
let metadata = entry.metadata()?;
@ -215,18 +202,17 @@ fn sort_files(selection: String, recursive: bool) -> Result<usize, Box<dyn std::
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
files_sorted += process_directory(&entry.path(), file_types, recursive)?;
process_directory(&entry.path(), file_types, recursive)?;
}
}
Ok(files_sorted)
Ok(())
}
process_directory(&search_path, &file_types, recursive)
@ -239,13 +225,9 @@ fn move_file_to_directory(path: &Path, dir: &Path) {
}
if !dir.exists() {
match fs::create_dir_all(dir) {
match fs::create_dir(dir) {
Ok(_) => {}
Err(err) => eprintln!(
"Error creating destination directory({}): {}",
dir.display(),
err
),
Err(err) => eprintln!("Error creating destination directory: {}", err),
}
return;
}
@ -263,7 +245,7 @@ fn get_config_path() -> PathBuf {
if cfg!(target_os = "windows") {
let username = env::var("USERNAME").unwrap_or_else(|_| "Default".to_string());
PathBuf::from(format!(
"C:\\Users\\{}\\AppData\\Local\\filesorters.toml",
"C:\\Users\\{}\\AppData\\Local\\filesorters\\filesorters.toml",
username
))
} else {