progress_bar/
progress-bar.rs

1use status_line::StatusLine;
2use std::fmt::{Display, Formatter};
3use std::sync::atomic::{AtomicUsize, Ordering};
4use std::thread;
5
6const PROGRESS_LEN: usize = 80;
7
8struct Progress {
9    pos: AtomicUsize,
10    max: usize,
11}
12
13impl Display for Progress {
14    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
15        let pos = self.pos.load(Ordering::Relaxed);
16        let pos = PROGRESS_LEN * pos / self.max;
17        write!(f, "[{}{}]", "*".repeat(pos), " ".repeat(PROGRESS_LEN - pos))
18    }
19}
20
21fn main() {
22    let progress = Progress {
23        pos: AtomicUsize::new(0),
24        max: 1000000000,
25    };
26
27    let progress_bar = StatusLine::new(progress);
28
29    // StatusLine can be moved to another thread:
30    thread::spawn(move || {
31        for _ in 0..progress_bar.max {
32            progress_bar.pos.fetch_add(1, Ordering::Relaxed);
33        }
34    })
35    .join()
36    .unwrap();
37}