Created
November 5, 2025 15:52
-
-
Save su8/e4bb9949663ad5a336c1b60b582e6fad to your computer and use it in GitHub Desktop.
main6.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use std::fs::{self, DirEntry, Metadata}; | |
| use std::io::{self, BufRead, BufReader, Write}; | |
| use std::path::Path; | |
| use std::sync::{Arc, Mutex}; | |
| use std::thread; | |
| static mut CURRENT_DIR_NUM: std::collections::HashMap<String, usize> = std::collections::HashMap::new(); | |
| fn walk_multiple_dirs(folder: &str, opt: char) { | |
| let mut count = 0; | |
| for entry in fs::read_dir(folder).unwrap() { | |
| let entry = entry.unwrap(); | |
| let path = entry.path(); | |
| if opt == 'b' { | |
| if path.is_dir() { | |
| continue; | |
| } | |
| let metadata: Metadata = fs::metadata(&path).unwrap(); | |
| println!("{} {} bytes", path.file_name().unwrap().to_str().unwrap(), metadata.len()); | |
| } | |
| unsafe { | |
| CURRENT_DIR_NUM.entry(folder.to_string()).and_modify(|e| *e += 1).or_insert(1); | |
| } | |
| count += 1; | |
| } | |
| let current_path = Path::new(folder); | |
| if folder.starts_with(".") { | |
| println!("{} {} items", fs::current_dir().unwrap().join(current_path).to_str().unwrap(), count); | |
| } else { | |
| println!("{} {} items", current_path.to_str().unwrap(), count); | |
| } | |
| } | |
| fn main() { | |
| let args: Vec<String> = std::env::args().collect(); | |
| if args.len() > 2 && (args[1].chars().nth(1) == Some('m') || args[1].chars().nth(1) == Some('b')) { | |
| let mut threads = Vec::new(); | |
| for i in 2..args.len() { | |
| let folder = args[i].clone(); | |
| let opt = args[1].chars().nth(1).unwrap(); | |
| threads.push(thread::spawn(move || walk_multiple_dirs(&folder, opt))); | |
| } | |
| for thread in threads { | |
| thread.join().unwrap(); | |
| } | |
| } else if args.len() > 2 && args[1].chars().nth(1) == Some('l') { | |
| let mut count = 0; | |
| let stdin = io::stdin(); | |
| let reader = BufReader::new(stdin.lock()); | |
| for _ in reader.lines() { | |
| count += 1; | |
| } | |
| println!("{} items", count); | |
| } | |
| } | |
| use std::collections::HashMap; | |
| use std::fs::{self, DirEntry}; | |
| use std::io::{self, BufRead}; | |
| use std::path::Path; | |
| use std::sync::{Arc, Mutex}; | |
| use std::thread; | |
| static mut CUR_DIR_NUM: HashMap<String, u64> = HashMap::new(); | |
| lazy_static::lazy_static! { | |
| static ref MUTE: Mutex<()> = Mutex::new(()); | |
| } | |
| fn main() { | |
| let args: Vec<String> = std::env::args().collect(); | |
| if args.len() > 1 && (args[1].chars().nth(1).unwrap() == 'm' || args[1].chars().nth(1).unwrap() == 'b') { | |
| let mut handles = vec![]; | |
| for folder in &args[2..] { | |
| let opt = args[1].chars().nth(1).unwrap(); | |
| let folder_clone = folder.clone(); | |
| handles.push(thread::spawn(move || { | |
| walk_multiple_dirs(&folder_clone, opt); | |
| })); | |
| unsafe { | |
| CUR_DIR_NUM.insert(folder_clone.clone(), 0); | |
| } | |
| } | |
| for handle in handles { | |
| let _ = handle.join(); | |
| } | |
| return; | |
| } | |
| if args.len() > 1 && args[1].chars().nth(1).unwrap() == 'l' { | |
| let stdin = io::stdin(); | |
| let count = stdin.lock().lines().count(); | |
| println!("{} items", count); | |
| } | |
| } | |
| fn walk_multiple_dirs(folder: &str, opt: char) { | |
| let entries = fs::read_dir(folder); | |
| match entries { | |
| Ok(entries) => { | |
| for entry in entries.filter_map(Result::ok) { | |
| let lock = MUTE.lock().unwrap(); | |
| let file_str = entry.file_name().into_string().unwrap(); | |
| if opt == 'b' { | |
| let path = Path::new(&file_str); | |
| if path.exists() && path.is_dir() { | |
| continue; | |
| } | |
| let size = fs::metadata(&file_str).map(|m| m.len()).unwrap_or(0); | |
| println!("{} {} bytes", file_str, size); | |
| } | |
| unsafe { | |
| *CUR_DIR_NUM.get_mut(folder).unwrap() += 1; | |
| } | |
| } | |
| let cur_folder = if folder.starts_with('.') { | |
| std::env::current_dir().unwrap() | |
| } else { | |
| Path::new(folder).to_path_buf() | |
| }; | |
| println!("{} {} items", cur_folder.display(), unsafe { CUR_DIR_NUM[folder] }); | |
| } | |
| Err(e) => { | |
| let _lock = MUTE.lock().unwrap(); | |
| eprintln!("Error: {}", e); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment