From 9516a7858443d16bb88871e76fb8848987c7a7a7 Mon Sep 17 00:00:00 2001 From: Nathan Reiner Date: Mon, 28 Oct 2024 20:35:17 +0100 Subject: remove progress bar Since calculating the total count takes up time, we do not use the progress bar anymore, but just a total files copied. --- src/main.rs | 63 ++++++++++++++++++++++++++++++++++++------------------------- 1 file changed, 37 insertions(+), 26 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 5ade5c6..c6b3f1e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,18 @@ +use clap::Parser; use std::path::Path; use anyhow::Result; -use kdam::tqdm; use walkdir::WalkDir; +#[derive(Parser, Debug)] +#[command(version, about, author)] +struct Args { + source: String, + destination: String, + #[arg(short, long, default_value_t = 8)] + jobs: usize, +} + async fn copy(from: String, to: String) -> std::io::Result<()> { let from_path = std::path::Path::new(&from); let to_path = std::path::Path::new(&to); @@ -20,29 +29,14 @@ async fn copy(from: String, to: String) -> std::io::Result<()> { Ok(()) } -#[tokio::main] -async fn main() -> Result<()> { - let args: Vec = std::env::args().collect(); +async fn run(args: Args) -> Result<()> { + let source = Path::new(&args.source); + let destination = Path::new(&args.destination); - if args.len() != 3 { - println!("{} ", args.first().unwrap()); - return Ok(()); - } + let mut tasks: Vec>> = + Vec::new(); - let source = Path::new(&args[1]); - let destination = Path::new(&args[2]); - const MAX: usize = 100; - let mut tasks = Vec::new(); - let count = WalkDir::new(source) - .into_iter() - .filter_map(|e| e.ok()) - .filter(|e| e.file_type().is_file()).count(); - - for entry in tqdm!(WalkDir::new(source) - .into_iter() - .filter_map(|e| e.ok()) - .filter(|e| e.file_type().is_file()), total = count) - { + for (count, entry) in WalkDir::new(source).into_iter().filter_map(|e| e.ok()).enumerate() { let from = entry.path().to_str().unwrap().to_string(); let rel = entry.path().strip_prefix(source)?; @@ -54,15 +48,32 @@ async fn main() -> Result<()> { tasks.push(tokio::spawn(copy(from, to))); - if tasks.len() > MAX { - for task in tasks { - if let Err(e) = task.await.unwrap() { + print!("copied {count} files\r"); + } + + println!("\nsyncing filesystem"); + + for task in tasks { + match task.await { + Ok(result) => { + if let Err(e) = result { eprintln!("Error: {:?}", e); } } - tasks = Vec::new(); + Err(e) => eprintln!("Runtime Error: {:?}", e) } } Ok(()) } + +fn main() -> Result<()> { + let args = Args::parse(); + let runtime = tokio::runtime::Builder::new_multi_thread() + .worker_threads(args.jobs) + .enable_all() + .build() + .unwrap(); + + runtime.block_on(run(args)) +} -- cgit v1.2.3-70-g09d2