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
use crate::utils::PrettyDuration;
use clock_core::stopwatch::{Stopwatch, StopwatchData};
use cursive::{
event::{Callback, Event, EventResult, Key},
view::View,
Cursive, Printer, Vec2, With,
};
use std::rc::Rc;
pub struct StopwatchView {
stopwatch: Stopwatch,
on_stop: Option<Rc<dyn Fn(&mut Cursive, StopwatchData)>>,
show_laps: usize,
}
impl StopwatchView {
pub fn new() -> Self {
Self {
stopwatch: Stopwatch::new(),
on_stop: None,
show_laps: 0,
}
}
pub fn with_laps(mut self, n: usize) -> Self {
self.show_laps = n;
self
}
pub fn set_on_stop<F, R>(&mut self, cb: F)
where
F: 'static + Fn(&mut Cursive, StopwatchData) -> R,
{
self.on_stop = Some(Rc::new(move |s, t| {
cb(s, t);
}));
}
pub fn on_stop<F, R>(self, cb: F) -> Self
where
F: 'static + Fn(&mut Cursive, StopwatchData) -> R,
{
self.with(|s| s.set_on_stop(cb))
}
fn stop(&mut self) -> EventResult {
let data = self.stopwatch.stop();
if self.on_stop.is_some() {
let cb = self.on_stop.clone().unwrap();
EventResult::Consumed(Some(Callback::from_fn_once(move |s| cb(s, data))))
} else {
EventResult::Consumed(None)
}
}
}
impl View for StopwatchView {
fn draw(&self, printer: &Printer) {
printer.print((4, 0), &self.stopwatch.read().pretty());
let len = self.stopwatch.data.laps.len();
for i in 1..=std::cmp::min(len, self.show_laps) {
printer.print(
(0, i),
&[
format!("Lap {:02}: ", len - i + 1),
self.stopwatch.data.laps[len - i].pretty(),
]
.concat(),
);
}
}
fn required_size(&mut self, _constraint: Vec2) -> Vec2 {
Vec2::new(20, self.show_laps + 1)
}
fn on_event(&mut self, event: Event) -> EventResult {
match event {
Event::Char(' ') => {
self.stopwatch.pause_or_resume();
}
Event::Key(Key::Enter) => {
return self.stop();
}
Event::Char('l') => {
self.stopwatch.lap();
}
_ => return EventResult::Ignored,
}
EventResult::Consumed(None)
}
}