1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
use std::fmt::Display;

pub struct Progress<T> {
    calls: usize,
    max: T,
    start_time: std::time::Instant,
    last_print_time: std::time::Instant,
}

pub struct ProgressBar {
    pub progress: f32,
}

impl Display for ProgressBar {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let total_width = 20;
        let current = if self.progress >= 1.0 {
            total_width
        } else if self.progress <= 0.0 {
            0
        } else {
            (total_width as f32 * self.progress).round() as usize
        };
        let full = "#";
        let empty = " ";
        write!(
            f,
            "[{}{}]",
            full.repeat(current),
            empty.repeat(total_width - current)
        )
    }
}

impl ProgressBar {
    pub fn new(progress: f32) -> Self {
        Self { progress }
    }
}

pub struct Duration {
    pub seconds: f64,
}

impl Duration {
    pub fn new(seconds: f64) -> Self {
        Self { seconds }
    }
}

impl Display for Duration {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut s = self.seconds;
        if s > 60.0 {
            s /= 60.0;
            if s > 60.0 {
                s /= 60.0;
                if s > 24.0 {
                    s /= 24.0;
                    if s > 365.0 {
                        s /= 365.242196;
                        write!(f, "{} years", s)
                    } else if s > 50.0 {
                        s /= 30.4368496667;
                        write!(f, "{} months", s)
                    } else if s > 14.0 {
                        s /= 7.0;
                        write!(f, "{} weeks", s)
                    } else {
                        write!(f, "{} days", s)
                    }
                } else {
                    write!(f, "{} hours", s)
                }
            } else {
                write!(f, "{} minutes", s)
            }
        } else {
            write!(f, "{} seconds", s)
        }
    }
}

impl Progress<usize> {
    pub fn new(max: usize) -> Self {
        Progress {
            calls: 0,
            max,
            start_time: std::time::Instant::now(),
            last_print_time: std::time::Instant::now(),
        }
    }

    pub fn progress(&mut self, current: usize) {
        self.calls += 1;
        if self.last_print_time.elapsed() >= std::time::Duration::new(10, 0) {
            self.last_print_time = std::time::Instant::now();

            let elapsed = self.start_time.elapsed().as_secs_f64();
            let progress = current as f64 / self.max as f64;
            let total = elapsed / progress;
            let time_left = total - elapsed;

            println!(
                "{} Calls since last print: {}  Elapsed: {}  Progress: {}%  ETA: {}",
                ProgressBar::new(progress as f32),
                self.calls,
                Duration::new(elapsed),
                100.0 * progress,
                Duration::new(time_left)
            );

            self.calls = 0;
        }
    }
}