use super::next_id;
use crate::style::Style;
use crate::{Cmd, Msg as TeaMsg};
use std::time::{Duration, SystemTime, UNIX_EPOCH};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Spinner {
pub frames: &'static [&'static str],
pub fps: Duration,
}
const fn per_second(n: u64) -> Duration {
Duration::from_nanos(1_000_000_000 / n)
}
impl Spinner {
pub const LINE: Self = Self {
frames: &["|", "/", "-", "\\"],
fps: per_second(10),
};
pub const DOT: Self = Self {
frames: &["⣾ ", "⣽ ", "⣻ ", "⢿ ", "⡿ ", "⣟ ", "⣯ ", "⣷ "],
fps: per_second(10),
};
pub const MINI_DOT: Self = Self {
frames: &["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"],
fps: per_second(12),
};
pub const JUMP: Self = Self {
frames: &["⢄", "⢂", "⢁", "⡁", "⡈", "⡐", "⡠"],
fps: per_second(10),
};
pub const PULSE: Self = Self {
frames: &["█", "▓", "▒", "░"],
fps: per_second(8),
};
pub const POINTS: Self = Self {
frames: &["∙∙∙", "●∙∙", "∙●∙", "∙∙●"],
fps: per_second(7),
};
pub const GLOBE: Self = Self {
frames: &["🌍", "🌎", "🌏"],
fps: per_second(4),
};
pub const MOON: Self = Self {
frames: &["🌑", "🌒", "🌓", "🌔", "🌕", "🌖", "🌗", "🌘"],
fps: per_second(8),
};
pub const MONKEY: Self = Self {
frames: &["🙈", "🙉", "🙊"],
fps: per_second(3),
};
pub const METER: Self = Self {
frames: &["▱▱▱", "▰▱▱", "▰▰▱", "▰▰▰", "▰▰▱", "▰▱▱", "▱▱▱"],
fps: per_second(7),
};
pub const HAMBURGER: Self = Self {
frames: &["☱", "☲", "☴", "☲"],
fps: per_second(3),
};
pub const ELLIPSIS: Self = Self {
frames: &["", ".", "..", "..."],
fps: per_second(3),
};
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TickMsg {
pub time: SystemTime,
pub id: u64,
tag: u64,
}
pub type Msg = TickMsg;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Model {
pub spinner: Spinner,
pub style: Style,
frame: usize,
id: u64,
tag: u64,
}
impl Default for Model {
fn default() -> Self {
Self::new()
}
}
impl Model {
#[must_use]
pub fn new() -> Self {
Self {
spinner: Spinner::LINE,
style: Style::new(),
frame: 0,
id: next_id(),
tag: 0,
}
}
#[must_use]
pub const fn with_spinner(mut self, spinner: Spinner) -> Self {
self.spinner = spinner;
self
}
#[must_use]
pub const fn with_style(mut self, style: Style) -> Self {
self.style = style;
self
}
#[must_use]
pub const fn id(&self) -> u64 {
self.id
}
pub fn update(&mut self, msg: &TeaMsg<Msg>) -> Cmd<Msg> {
match msg {
TeaMsg::App(tick) if self.is_mine(tick) => {
self.frame = (self.frame + 1) % self.spinner.frames.len().max(1);
self.tag += 1;
let (id, tag) = (self.id, self.tag);
crate::tick(self.spinner.fps, move |time| TickMsg { time, id, tag })
}
_ => Cmd::none(),
}
}
const fn is_mine(&self, tick: &TickMsg) -> bool {
(tick.id == 0 || tick.id == self.id) && (tick.tag == 0 || tick.tag == self.tag)
}
#[must_use]
pub fn view(&self) -> String {
self.spinner
.frames
.get(self.frame)
.map_or_else(|| "(error)".to_string(), |f| self.style.paint(f))
}
pub fn tick(&self) -> Cmd<Msg> {
let (id, tag) = (self.id, self.tag);
crate::tick(Duration::ZERO, move |time| TickMsg { time, id, tag })
}
#[must_use]
pub const fn tick_msg(&self, time: SystemTime) -> TickMsg {
TickMsg {
time,
id: self.id,
tag: self.tag,
}
}
}
impl Default for TickMsg {
fn default() -> Self {
Self {
time: UNIX_EPOCH,
id: 0,
tag: 0,
}
}
}
#[cfg(test)]
mod tests;