use ratatui::{
buffer::Buffer,
layout::Rect,
style::Style,
widgets::StatefulWidget,
};
use crate::spin::{Spin, SpinState};
#[derive(Clone, Copy)]
pub enum SpinVariant {
Dots,
Line,
Dots2,
Bounce,
Pulse,
Arrows,
Square,
Clock,
Custom(&'static [&'static str]),
}
impl SpinVariant {
pub fn frames(&self) -> &'static [&'static str] {
match self {
Self::Dots => crate::DEFAULT_FRAMES,
Self::Line => &["╶", "╴"],
Self::Dots2 => &["⠄", "⠆", "⠖", "⠶", "⠾", "⠷", "⠧", "⠇"],
Self::Bounce => &["⢀", "⡀", "⣀", "⡄", "⢄"],
Self::Pulse => &["▁", "▃", "▄", "▅", "▆", "▇", "█", "▇", "▆", "▅", "▄", "▃"],
Self::Arrows => &["←", "↖", "↑", "↗", "→", "↘", "↓", "↙"],
Self::Square => &["▰", "▱"],
Self::Clock => &[
"🕐", "🕑", "🕒", "🕓", "🕔", "🕕", "🕖", "🕗", "🕘", "🕙", "🕚", "🕛",
],
Self::Custom(f) => f,
}
}
}
impl Default for SpinVariant {
fn default() -> Self {
Self::Dots
}
}
#[derive(Clone)]
pub struct ThemedSpin {
spin: Spin,
}
impl ThemedSpin {
pub fn new() -> Self {
Self {
spin: Spin::new().frames(SpinVariant::Dots.frames()),
}
}
pub fn variant(mut self, variant: SpinVariant) -> Self {
self.spin = self.spin.frames(variant.frames());
self
}
pub fn spinner_style(mut self, style: Style) -> Self {
self.spin = self.spin.spinner_style(style);
self
}
}
impl StatefulWidget for ThemedSpin {
type State = SpinState;
fn render(self, area: Rect, buf: &mut Buffer, state: &mut Self::State) {
self.spin.render(area, buf, state);
}
}