use std::time::{Duration, Instant};
pub struct Spinner {
frame_index: usize,
frames: Vec<&'static str>,
last_update: Instant,
speed_ms: u64,
}
impl Spinner {
pub fn new() -> Self {
Self {
frame_index: 0,
frames: vec!["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"],
last_update: Instant::now(),
speed_ms: 80,
}
}
#[allow(dead_code)]
pub fn dots() -> Self {
Self {
frame_index: 0,
frames: vec!["⣾", "⣽", "⣻", "⢿", "⡿", "⣟", "⣯", "⣷"],
last_update: Instant::now(),
speed_ms: 80,
}
}
#[allow(dead_code)]
pub fn simple() -> Self {
Self {
frame_index: 0,
frames: vec!["|", "/", "-", "\\"],
last_update: Instant::now(),
speed_ms: 100,
}
}
pub fn tick(&mut self) -> &'static str {
let now = Instant::now();
if now.duration_since(self.last_update) >= Duration::from_millis(self.speed_ms) {
self.frame_index = (self.frame_index + 1) % self.frames.len();
self.last_update = now;
}
self.frames[self.frame_index]
}
#[allow(dead_code)]
pub fn reset(&mut self) {
self.frame_index = 0;
self.last_update = Instant::now();
}
}
impl Default for Spinner {
fn default() -> Self {
Self::new()
}
}