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
// use chrono::Duration;
// use cursive::{traits::*, views::Dialog, Cursive};

// fn summarize(stopwatch: &StopWatch::StopWatch) -> String {
//     let elapsed = stopwatch.elapsed;
//     let average = stopwatch.elapsed / stopwatch.laps.len() as i32;
//     let max = stopwatch.laps.iter().max().unwrap();
//     let min = stopwatch.laps.iter().min().unwrap();
//     format!(
//         "Elapsed time: {}\nAverage: {}\nMax: {}\nMin: {}",
//         elapsed.pretty(),
//         average.pretty(),
//         max.pretty(),
//         min.pretty()
//     )
// }

//pub mod countdown;
mod countdown;
mod stopwatch;
use crate::core::{countdown::Timer, stopwatch::StopWatch};
use crate::utils::PrettyDuration;
use chrono::Duration;
use countdown::TimerView;
use cursive::{traits::*, views::Dialog, Cursive};
use stopwatch::StopWatchView;

pub fn stopwatch() {
    let mut siv = cursive::default();
    let stopwatch = StopWatchView::new();
    siv.add_layer(
        stopwatch
            .with_laps(8)
            .on_stop(|s: &mut Cursive, stopwatch| s.add_layer(Dialog::info(summarize(&stopwatch))))
            .with_name("stopwatch"),
    );
    siv.add_layer(Dialog::info(
        "Press 'Space' to start/pause/resume the stopwatch\nPress 'l' to record lap time\nPress 'Enter' to stop",
    ));
    siv.set_fps(15);
    siv.run();
}

fn summarize(stopwatch: &StopWatch) -> String {
    let elapsed = stopwatch.elapsed;
    let average = stopwatch.elapsed / stopwatch.laps.len() as i32;
    let max = stopwatch.laps.iter().max().unwrap();
    let min = stopwatch.laps.iter().min().unwrap();
    format!(
        "Elapsed time: {}\nAverage: {}\nMax: {}\nMin: {}",
        elapsed.pretty(),
        average.pretty(),
        max.pretty(),
        min.pretty()
    )
}

pub fn timer(h: u8, m: u8, s: u8) {
    let mut siv = cursive::default();
    let timer = TimerView::new(h, m, s);
    siv.add_layer(
        timer, // .on_finish(|s: &mut Cursive, timer| s.add_layer(Dialog::info(format!("{:?}", &timer)))),
    );
    //siv.set_fps(15);
    siv.set_autorefresh(true);
    siv.run();
}