index : nmrs-tui
A fast, Vim-friendly NetworkManager TUI built in Rust.
about summary refs log tree commit diff
nmrs-tui / src / timer.rs
-rw-r--r-- blob 73be6c30cd9f53872ae78ec7a58ce9d51c273b23 858 bytes (plain)
1
use std::time::Duration;
2
3
use crate::action::Action;
4
5
pub struct Timer {
6
    pub duration: Duration,
7
    pub remaining: Duration,
8
    pub on_finish: Action,
9
    pub enabled: bool,
10
}
11
12
#[allow(dead_code)]
13
impl Timer {
14
    pub fn new(duration: Duration, on_finish: Action, enabled: bool) -> Self {
15
        Self {
16
            duration,
17
            remaining: duration,
18
            on_finish,
19
            enabled,
20
        }
21
    }
22
23
    pub fn tick(&mut self, delta: Duration) {
24
        self.remaining = self.remaining.saturating_sub(delta);
25
    }
26
27
    pub fn is_finished(&self) -> bool {
28
        self.remaining.is_zero()
29
    }
30
31
    pub fn reset(&mut self) {
32
        self.remaining = self.duration;
33
    }
34
35
    pub fn enable(&mut self) {
36
        self.enabled = true;
37
        self.reset();
38
    }
39
40
    pub fn disable(&mut self) {
41
        self.enabled = false;
42
    }
43
}