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
mod stopwatch;
mod timer;
use crate::{notify::notify, utils::PrettyDuration};
use clock_core::{stopwatch::StopwatchData, timer::TimerData};
use cursive::{traits::*, views::Dialog, Cursive};
pub use stopwatch::StopwatchView;
pub use timer::TimerView;
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.set_fps(15);
siv.run();
}
fn summarize(stopwatch: &StopwatchData) -> 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()
)
}
fn timer_on_finish(data: TimerData) {
let expected_duration = data.duration_expected().pretty_s();
let actual_duration = data.duration_actual().pretty_s();
let msg = &format!(
"Expected: {}\nActual: {}",
&expected_duration, &actual_duration,
);
notify(msg).unwrap();
match notify(msg) {
Ok(_) => {}
Err(_) => {}
}
}
#[allow(dead_code)]
fn timer_on_finish_debug(s: &mut Cursive, data: TimerData) {
s.add_layer(Dialog::info(format!("{:?}", data)));
}
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(|_: &mut Cursive, timer| timer_on_finish(timer)));
siv.set_autorefresh(true);
siv.run();
}