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
//! Spinner service for managing loading indicator state.
//!
//! Tracks whether a spinner is active, its message, and elapsed time
//! since activation.
use std::time::{Duration, Instant};
/// Service for managing spinner animation state and timing.
pub struct SpinnerService {
active: bool,
message: String,
start_time: Option<Instant>,
}
impl SpinnerService {
/// Create a new inactive spinner service.
pub fn new() -> Self {
Self {
active: false,
message: String::new(),
start_time: None,
}
}
/// Whether the spinner is currently active.
pub fn active(&self) -> bool {
self.active
}
/// The current spinner message.
pub fn message(&self) -> &str {
&self.message
}
/// Start the spinner with the given message.
pub fn start(&mut self, message: String) {
self.message = message;
self.start_time = Some(Instant::now());
self.active = true;
}
/// Stop the spinner.
pub fn stop(&mut self) {
self.active = false;
}
/// Get the elapsed duration since the spinner was started.
///
/// Returns `Duration::ZERO` if the spinner was never started.
pub fn elapsed(&self) -> Duration {
self.start_time
.map(|t| t.elapsed())
.unwrap_or(Duration::ZERO)
}
}
impl Default for SpinnerService {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
#[path = "spinner_tests.rs"]
mod tests;