use std::time::Duration;
use std::io::{Write, stdout};
#[derive(Debug)]
pub struct LoadingBar {
duration: Duration,
progress_char: char,
length: u32,
prefix: String,
}
impl LoadingBar {
pub fn new() -> Self {
Self {
duration: Duration::from_secs(5),
progress_char: '#',
length: 20,
prefix: String::new(),
}
}
pub fn new_with_config(duration: Duration, progress_char: char, length: u32, prefix: String) -> Self {
Self {
duration,
progress_char,
length,
prefix,
}
}
pub fn start(&self) {
let each_duration = self.duration / self.length;
(1..=self.length).for_each(|i| {
print!("{}", self.prefix);
print!("[");
(1..=i).for_each(|_| print!("{}", self.progress_char));
(i..self.length).for_each(|_| print!(" "));
print!("]");
stdout().flush().unwrap();
if i != self.length {
print!("\r");
} else {
println!();
}
stdout().flush().unwrap();
std::thread::sleep(each_duration);
});
}
}