diff options
Diffstat (limited to 'src/utils')
| -rw-r--r-- | src/utils/file_input_solve.rs | 82 | ||||
| -rw-r--r-- | src/utils/size_display.rs | 14 |
2 files changed, 96 insertions, 0 deletions
diff --git a/src/utils/file_input_solve.rs b/src/utils/file_input_solve.rs new file mode 100644 index 0000000..30d5765 --- /dev/null +++ b/src/utils/file_input_solve.rs @@ -0,0 +1,82 @@ +use std::{ + env::current_dir, + path::{Path, PathBuf}, +}; + +use just_fmt::fmt_path::fmt_path; + +pub fn parse_path_input( + files: Vec<String>, + recursive: bool, + exclude_dir: Vec<&str>, +) -> Vec<PathBuf> { + let current_dir = current_dir().unwrap(); + let files = if recursive { + let mut result: Vec<PathBuf> = Vec::new(); + for arg in files.iter().skip(1) { + if exclude_dir.contains(&arg.as_str()) { + continue; + } + let path = current_dir.join(arg); + if path.is_dir() { + if let Err(e) = collect_files_recursively(&path, &mut result) { + eprintln!("Error collecting files recursively: {}", e); + continue; + } + } else { + result.push(path); + } + } + result + } else { + let mut result = Vec::new(); + for arg in files.iter().skip(1) { + if exclude_dir.contains(&arg.as_str()) { + continue; + } + let path = current_dir.join(arg); + if path.is_dir() { + if files.len() == 2 { + for entry in std::fs::read_dir(&path) + .unwrap_or_else(|e| { + eprintln!("Error reading directory: {}", e); + std::fs::read_dir(".").unwrap() + }) + .flatten() + { + let entry_path = entry.path(); + if !entry_path.is_dir() { + result.push(entry_path); + } + } + } + } else { + result.push(path); + } + } + result + }; + files + .into_iter() + .filter_map(|path| match fmt_path(path) { + Ok(formatted_path) => Some(formatted_path), + Err(e) => { + eprintln!("Error formatting path: {}", e); + None + } + }) + .collect() +} + +fn collect_files_recursively(dir: &Path, files: &mut Vec<PathBuf>) -> std::io::Result<()> { + for entry in std::fs::read_dir(dir)? { + let entry = entry?; + let path = entry.path(); + if path.is_dir() { + collect_files_recursively(&path, files)?; + } else { + files.push(path); + } + } + Ok(()) +} diff --git a/src/utils/size_display.rs b/src/utils/size_display.rs new file mode 100644 index 0000000..3e2bc29 --- /dev/null +++ b/src/utils/size_display.rs @@ -0,0 +1,14 @@ +pub fn size_display<'a>(total_bytes: usize) -> (f64, &'a str) { + let total_bytes = total_bytes as f64; + if total_bytes >= 1024.0 * 1024.0 * 1024.0 * 1024.0 { + (total_bytes / (1024.0 * 1024.0 * 1024.0 * 1024.0), "TB") + } else if total_bytes >= 1024.0 * 1024.0 * 1024.0 { + (total_bytes / (1024.0 * 1024.0 * 1024.0), "GB") + } else if total_bytes >= 1024.0 * 1024.0 { + (total_bytes / (1024.0 * 1024.0), "MB") + } else if total_bytes >= 1024.0 { + (total_bytes / 1024.0, "KB") + } else { + (total_bytes, "B") + } +} |
