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
//! Spinner controller for displaying animated loading indicators.
//!
//! Uses braille animation frames for a smooth terminal spinner.
/// Braille animation frames.
const SPINNER_FRAMES: &[&str] = &["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
/// Controller for a terminal spinner animation.
pub struct SpinnerController {
current_frame: usize,
message: String,
active: bool,
}
impl SpinnerController {
/// Create a new inactive spinner.
pub fn new() -> Self {
Self {
current_frame: 0,
message: String::new(),
active: false,
}
}
/// Whether the spinner is currently active.
pub fn active(&self) -> bool {
self.active
}
/// The 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.current_frame = 0;
self.active = true;
}
/// Stop the spinner.
pub fn stop(&mut self) {
self.active = false;
}
/// Advance to the next frame and return the current frame character.
///
/// Returns the braille character for the current frame.
pub fn tick(&mut self) -> &'static str {
let frame = SPINNER_FRAMES[self.current_frame];
self.current_frame = (self.current_frame + 1) % SPINNER_FRAMES.len();
frame
}
/// The available animation frames.
pub fn frames() -> &'static [&'static str] {
SPINNER_FRAMES
}
/// The current frame index.
pub fn current_frame(&self) -> usize {
self.current_frame
}
}
impl Default for SpinnerController {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
#[path = "spinner_tests.rs"]
mod tests;