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
pub mod zuu {
    use std::cell::Cell;
    use std::collections::HashMap;
    use std::thread::sleep;
    use std::time::{Duration, Instant};
    use progress_bar::{Color, finalize_progress_bar, inc_progress_bar, init_progress_bar, print_progress_bar_info, set_progress_bar_action, Style};
    use colored_truecolor::Colorize;

    pub struct Performances {
        start: Instant,
        all: Cell<usize>,
        callback: HashMap<String, u128>,
    }

    impl Performances {
        ///
        /// Constructor
        ///
        ///
        pub fn new() -> Performances
        {
            println!("\n     {}\n", "All units are in µs".magenta().bold());
            Self {
                start: Instant::now(),
                all: Cell::new(0),
                callback: HashMap::new(),
            }
        }

        ///
        /// Capture the execution time of the callback
        ///
        /// - `name` The function name
        /// - `f` The pointer to the callback
        ///
        ///
        pub fn capture<T>(&mut self, name: &str, f: &dyn Fn() -> T) -> &mut Performances
        {
            self.all.set(self.all.get() + 1);

            f();
            let time = self.start.elapsed();
            self.start = Instant::now();
            self.callback.insert(String::from(name), time.as_micros());

            return self;
        }

        ///
        ///
        /// Print the result
        ///
        ///```
        /// use std::thread::sleep;
        /// use std::time::Duration;
        /// use performances::zuu::Performances;
        ///
        /// fn kool() -> bool
        /// {
        ///    return false;
        /// }
        /// fn look() -> bool
        /// {
        ///    return true;
        /// }
        ///
        /// fn s() -> i32
        /// {
        ///    return 0;
        /// }
        ///
        /// fn get_fans() -> i32
        /// {
        ///    return 255;
        /// }
        /// fn f() -> i32
        /// {
        ///    return 1;
        /// }
        ///
        /// fn bad()
        /// {
        ///    sleep(Duration::from_nanos(50));
        /// }
        ///
        /// Performances::new().capture("bad", &bad)
        ///                    .capture("kool", &kool)
        ///                    .capture("look", &look)
        ///                    .capture("s", &s)
        ///                    .capture("get_fans", &get_fans)
        ///                    .print()
        ///                    .expect("panic message");
        ///```
        ///
        pub fn print(&mut self) -> Result<(), String>
        {
            if self.all.get() < 1 {
                return Err(String::from("No captured function founded"));
            }
            init_progress_bar(self.all.get());
            set_progress_bar_action("Loading", Color::Blue, Style::Bold);
            for test in self.callback.iter() {
                sleep(Duration::from_millis(50));
                print_progress_bar_info(test.0, format!("{}", String::from(test.1.to_string()).blue().bold()).as_str(), Color::Green, Style::Bold);
                inc_progress_bar();
            }
            set_progress_bar_action("Finnish", Color::Green, Style::Bold);
            finalize_progress_bar();
            Ok(())
        }
    }
}