Skip to content

Commit

Permalink
Added progress bar
Browse files Browse the repository at this point in the history
  • Loading branch information
LennyPhoenix committed Apr 18, 2024
1 parent eee5a0d commit bab4d41
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
34 changes: 34 additions & 0 deletions src/database/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
};
Expand Down Expand Up @@ -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::<Vec<_>>();
let total_entries = source_entries.len();

source_entries
.into_par_iter()
.try_for_each(|entry| -> Result<()> {
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -217,4 +225,30 @@ impl Database {
Ok(())
})
}

fn log_progress(counter: Arc<Mutex<usize>>, 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::<char>::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::<String>();

print!("\r[{bar}] {progress:.1}%");
}
}

0 comments on commit bab4d41

Please sign in to comment.