From bab4d41569cab1a3c976d7103431e3bc5e3a9b9e Mon Sep 17 00:00:00 2001 From: Lenny Critchley Date: Thu, 18 Apr 2024 14:24:47 +0100 Subject: [PATCH] Added progress bar --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/database/mod.rs | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3eaee2d..53089ed 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -270,7 +270,7 @@ checksum = "6c8640c5d730cb13ebd907d8d04b52f55ac9a2eec55b440c8892f40d56c76c1d" [[package]] name = "mirrorman" -version = "0.2.4" +version = "0.2.5" dependencies = [ "anyhow", "base32", diff --git a/Cargo.toml b/Cargo.toml index be8bd9d..6633588 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mirrorman" -version = "0.2.4" +version = "0.2.5" edition = "2021" description = "Media conversion and mirror tool" readme = "README.md" diff --git a/src/database/mod.rs b/src/database/mod.rs index 50efe49..a84207e 100644 --- a/src/database/mod.rs +++ b/src/database/mod.rs @@ -12,6 +12,7 @@ use std::{ collections::{BTreeMap, BTreeSet}, fs::{copy, create_dir_all, File}, io::Read, + iter::repeat, path::{Path, PathBuf}, sync::{Arc, Mutex}, }; @@ -49,11 +50,14 @@ impl Database { pub fn sync(&mut self) -> Result<()> { let new_hashes = Arc::new(Mutex::new(BTreeMap::new())); let mirror_list = Arc::new(Mutex::new(BTreeSet::new())); + let counter = Arc::new(Mutex::new(0_usize)); // Walk source directory let source_entries = WalkDir::new(&self.source_path) .into_iter() .collect::>(); + let total_entries = source_entries.len(); + source_entries .into_par_iter() .try_for_each(|entry| -> Result<()> { @@ -86,9 +90,13 @@ impl Database { )?; } + Self::log_progress(counter.clone(), total_entries); + Ok(()) })?; + println!(); + self.hashes = match new_hashes.lock() { Ok(new_hashes) => new_hashes, Err(poisoned) => { @@ -217,4 +225,30 @@ impl Database { Ok(()) }) } + + fn log_progress(counter: Arc>, max_count: usize) { + let mut counter = match counter.lock() { + Ok(counter) => counter, + Err(poisoned) => poisoned.into_inner(), + }; + *counter += 1; + + let progress = 100.0 * (*counter as f64 / max_count as f64); + + const BLOCK_COUNT: usize = 20; + let num_blocks = 20 * *counter / max_count; + + let mut bar = Vec::::new(); + bar.extend(repeat('=').take(num_blocks)); + let count = if num_blocks < BLOCK_COUNT { + bar.push('>'); + BLOCK_COUNT - num_blocks - 1 + } else { + 0 + }; + bar.extend(repeat(' ').take(count)); + let bar = bar.into_iter().collect::(); + + print!("\r[{bar}] {progress:.1}%"); + } }