move util.rs to util/countdown.rs

This commit is contained in:
2025-03-31 12:43:54 +01:00
parent 186fcde20e
commit 57db451958
3 changed files with 2 additions and 1 deletions
+40
View File
@@ -0,0 +1,40 @@
use std::time::{Duration, Instant};
pub struct Countdown {
pub i: usize,
pub i_original: usize,
pub i_string: String,
pub interval: Instant,
pub first_count: bool,
}
impl Countdown {
pub fn new(i: usize) -> Self {
Self {
i,
i_original: i,
i_string: String::new(),
interval: Instant::now(),
first_count: true,
}
}
pub fn reset(&mut self) {
*self = Self::new(self.i_original);
}
pub fn count(&mut self) {
if self.first_count {
self.interval = Instant::now();
self.first_count = false;
}
if self.interval.elapsed() >= Duration::from_secs(1) {
self.i -= 1;
self.interval = Instant::now();
}
self.i_string = self.i.to_string();
if self.i == 0 {
self.reset();
};
}
}