aboutsummaryrefslogtreecommitdiffstats
path: root/crates/atuin-ai/src/tui/spinner.rs
blob: 138e0269415c568bd9e68fa0d912e5bcec61f940 (plain) (blame)
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//! Spinner styles and configuration for TUI animations
//!
//! To experiment with different spinners, change `ACTIVE_SPINNER` below.

use std::time::Duration;

/// Active spinner style - change this to experiment with different styles
pub const ACTIVE_SPINNER: SpinnerStyle = SpinnerStyle::Dots;

/// Spinner style definitions
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SpinnerStyle {
    /// Classic ASCII line spinner: / - \ |
    Line,
    /// Braille dots pattern
    Dots,
    /// Growing/shrinking dots
    Pulse,
    /// Simple arrow rotation
    Arrow,
    /// Block building
    Block,
}

impl SpinnerStyle {
    /// Get the frames for this spinner style
    pub const fn frames(&self) -> &'static [&'static str] {
        match self {
            SpinnerStyle::Line => &["/", "-", "\\", "|"],
            SpinnerStyle::Dots => &["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"],
            SpinnerStyle::Pulse => &["·", "•", "●", "•"],
            SpinnerStyle::Arrow => &["←", "↖", "↑", "↗", "→", "↘", "↓", "↙"],
            SpinnerStyle::Block => &[
                "▏", "▎", "▍", "▌", "▋", "▊", "▉", "█", "▉", "▊", "▋", "▌", "▍", "▎", "▏",
            ],
        }
    }

    /// Get the recommended tick interval for this spinner style
    /// Faster spinners need shorter intervals to look smooth
    pub const fn tick_interval(&self) -> Duration {
        match self {
            SpinnerStyle::Line => Duration::from_millis(150),
            SpinnerStyle::Dots => Duration::from_millis(80),
            SpinnerStyle::Pulse => Duration::from_millis(200),
            SpinnerStyle::Arrow => Duration::from_millis(100),
            SpinnerStyle::Block => Duration::from_millis(80),
        }
    }

    /// Get the frame at the given index (wraps around)
    pub fn frame_at(&self, index: usize) -> &'static str {
        let frames = self.frames();
        frames[index % frames.len()]
    }

    /// Get the number of frames in this spinner
    pub fn frame_count(&self) -> usize {
        self.frames().len()
    }
}

/// Get the active spinner's frame at the given index
pub fn active_frame(index: usize) -> &'static str {
    ACTIVE_SPINNER.frame_at(index)
}

/// Get the active spinner's tick interval
pub fn active_tick_interval() -> Duration {
    ACTIVE_SPINNER.tick_interval()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_frame_wrapping() {
        let style = SpinnerStyle::Line;
        assert_eq!(style.frame_at(0), "/");
        assert_eq!(style.frame_at(4), "/"); // wraps
        assert_eq!(style.frame_at(5), "-");
    }

    #[test]
    fn test_all_styles_have_frames() {
        let styles = [
            SpinnerStyle::Line,
            SpinnerStyle::Dots,
            SpinnerStyle::Pulse,
            SpinnerStyle::Arrow,
            SpinnerStyle::Block,
        ];
        for style in styles {
            assert!(!style.frames().is_empty());
            assert!(style.tick_interval().as_millis() > 0);
        }
    }
}