ratatui-interact 0.5.1

Interactive TUI components for ratatui with focus management and mouse support
Documentation
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
//! AnimatedText widget for animated text labels with color effects
//!
//! Provides animated text display with multiple effect modes:
//! - Pulse: Entire text oscillates between two colors
//! - Wave: A highlighted portion travels back and forth across the text
//! - Rainbow: Colors cycle through a spectrum across the text
//!
//! # Example
//!
//! ```rust
//! use ratatui_interact::components::{AnimatedText, AnimatedTextState, AnimatedTextStyle, AnimatedTextEffect};
//! use ratatui::layout::Rect;
//! use ratatui::buffer::Buffer;
//! use ratatui::widgets::Widget;
//! use ratatui::style::Color;
//!
//! // Create state and advance each frame
//! let mut state = AnimatedTextState::new();
//!
//! // Pulse effect - entire text oscillates between colors
//! let style = AnimatedTextStyle::pulse(Color::Cyan, Color::Blue);
//! let text = AnimatedText::new("Loading...", &state).style(style);
//!
//! // Wave effect - highlight travels across the text
//! let style = AnimatedTextStyle::wave(Color::White, Color::Yellow);
//! let text = AnimatedText::new("Processing data", &state).style(style);
//!
//! // In your event loop, advance the animation
//! state.tick();
//! ```

use std::time::{Duration, Instant};

use ratatui::{
    buffer::Buffer,
    layout::Rect,
    style::{Color, Modifier, Style},
    widgets::Widget,
};
use unicode_width::UnicodeWidthStr;

/// Animation effect types for animated text
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum AnimatedTextEffect {
    /// Entire text pulses/oscillates between two colors
    #[default]
    Pulse,
    /// A highlighted portion travels back and forth (wave effect)
    Wave,
    /// Colors cycle through a rainbow spectrum across the text
    Rainbow,
    /// Gradient that shifts over time
    GradientShift,
    /// Text appears to sparkle with random highlights
    Sparkle,
}

/// Direction for wave animation
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum WaveDirection {
    /// Wave travels left to right
    #[default]
    Forward,
    /// Wave travels right to left
    Backward,
}

/// State for animated text
#[derive(Debug, Clone)]
pub struct AnimatedTextState {
    /// Current animation frame (0-255 for smooth transitions)
    pub frame: u8,
    /// Wave position (for wave effect)
    pub wave_position: usize,
    /// Wave direction (for bounce behavior)
    pub wave_direction: WaveDirection,
    /// Last tick time
    last_tick: Option<Instant>,
    /// Frame interval
    interval: Duration,
    /// Whether the animation is active
    pub active: bool,
    /// Random seed for sparkle effect
    sparkle_seed: u64,
}

impl Default for AnimatedTextState {
    fn default() -> Self {
        Self::new()
    }
}

impl AnimatedTextState {
    /// Create a new animated text state
    pub fn new() -> Self {
        Self {
            frame: 0,
            wave_position: 0,
            wave_direction: WaveDirection::Forward,
            last_tick: None,
            interval: Duration::from_millis(50),
            active: true,
            sparkle_seed: 0,
        }
    }

    /// Create state with a specific interval
    pub fn with_interval(interval_ms: u64) -> Self {
        Self {
            interval: Duration::from_millis(interval_ms),
            ..Self::new()
        }
    }

    /// Set the animation interval
    pub fn set_interval(&mut self, interval_ms: u64) {
        self.interval = Duration::from_millis(interval_ms);
    }

    /// Advance the animation by one tick
    ///
    /// Returns true if the frame changed
    pub fn tick(&mut self) -> bool {
        self.tick_with_text_width(20) // Default width assumption
    }

    /// Advance the animation with known text width (for wave calculations)
    ///
    /// Returns true if the frame changed
    pub fn tick_with_text_width(&mut self, text_width: usize) -> bool {
        if !self.active {
            return false;
        }

        let now = Instant::now();

        match self.last_tick {
            Some(last) if now.duration_since(last) >= self.interval => {
                // Advance frame (wraps at 256)
                self.frame = self.frame.wrapping_add(4);

                // Update wave position
                let max_pos = text_width.saturating_sub(1);
                match self.wave_direction {
                    WaveDirection::Forward => {
                        if self.wave_position >= max_pos {
                            self.wave_direction = WaveDirection::Backward;
                        } else {
                            self.wave_position += 1;
                        }
                    }
                    WaveDirection::Backward => {
                        if self.wave_position == 0 {
                            self.wave_direction = WaveDirection::Forward;
                        } else {
                            self.wave_position -= 1;
                        }
                    }
                }

                // Update sparkle seed
                self.sparkle_seed = self.sparkle_seed.wrapping_add(1);

                self.last_tick = Some(now);
                true
            }
            None => {
                self.last_tick = Some(now);
                false
            }
            _ => false,
        }
    }

    /// Reset the animation to initial state
    pub fn reset(&mut self) {
        self.frame = 0;
        self.wave_position = 0;
        self.wave_direction = WaveDirection::Forward;
        self.last_tick = None;
        self.sparkle_seed = 0;
    }

    /// Start the animation
    pub fn start(&mut self) {
        self.active = true;
    }

    /// Stop the animation
    pub fn stop(&mut self) {
        self.active = false;
    }

    /// Check if the animation is active
    pub fn is_active(&self) -> bool {
        self.active
    }

    /// Get the current interpolation factor (0.0 to 1.0)
    pub fn interpolation_factor(&self) -> f32 {
        // Use a sine wave for smooth oscillation
        let radians = (self.frame as f32 / 255.0) * std::f32::consts::PI * 2.0;
        (radians.sin() + 1.0) / 2.0
    }
}

/// Style configuration for animated text
#[derive(Debug, Clone)]
pub struct AnimatedTextStyle {
    /// Animation effect type
    pub effect: AnimatedTextEffect,
    /// Primary/base color
    pub primary_color: Color,
    /// Secondary color (for pulse/wave effects)
    pub secondary_color: Color,
    /// Text modifiers (bold, italic, etc.)
    pub modifiers: Modifier,
    /// Width of the wave highlight (in characters)
    pub wave_width: usize,
    /// Background color (optional)
    pub background: Option<Color>,
    /// Rainbow colors for rainbow effect
    pub rainbow_colors: Vec<Color>,
}

impl Default for AnimatedTextStyle {
    fn default() -> Self {
        Self {
            effect: AnimatedTextEffect::Pulse,
            primary_color: Color::White,
            secondary_color: Color::Cyan,
            modifiers: Modifier::empty(),
            wave_width: 3,
            background: None,
            rainbow_colors: vec![
                Color::Red,
                Color::Yellow,
                Color::Green,
                Color::Cyan,
                Color::Blue,
                Color::Magenta,
            ],
        }
    }
}

impl From<&crate::theme::Theme> for AnimatedTextStyle {
    fn from(theme: &crate::theme::Theme) -> Self {
        let p = &theme.palette;
        Self {
            effect: AnimatedTextEffect::Pulse,
            primary_color: p.text,
            secondary_color: p.secondary,
            modifiers: Modifier::empty(),
            wave_width: 3,
            background: None,
            rainbow_colors: vec![
                Color::Red,
                Color::Yellow,
                Color::Green,
                Color::Cyan,
                Color::Blue,
                Color::Magenta,
            ],
        }
    }
}

impl AnimatedTextStyle {
    /// Create a new style with default values
    pub fn new() -> Self {
        Self::default()
    }

    /// Create a pulse effect style
    pub fn pulse(primary: Color, secondary: Color) -> Self {
        Self {
            effect: AnimatedTextEffect::Pulse,
            primary_color: primary,
            secondary_color: secondary,
            ..Default::default()
        }
    }

    /// Create a wave effect style
    pub fn wave(base: Color, highlight: Color) -> Self {
        Self {
            effect: AnimatedTextEffect::Wave,
            primary_color: base,
            secondary_color: highlight,
            wave_width: 3,
            ..Default::default()
        }
    }

    /// Create a rainbow effect style
    pub fn rainbow() -> Self {
        Self {
            effect: AnimatedTextEffect::Rainbow,
            ..Default::default()
        }
    }

    /// Create a gradient shift effect style
    pub fn gradient_shift(start: Color, end: Color) -> Self {
        Self {
            effect: AnimatedTextEffect::GradientShift,
            primary_color: start,
            secondary_color: end,
            ..Default::default()
        }
    }

    /// Create a sparkle effect style
    pub fn sparkle(base: Color, sparkle: Color) -> Self {
        Self {
            effect: AnimatedTextEffect::Sparkle,
            primary_color: base,
            secondary_color: sparkle,
            ..Default::default()
        }
    }

    /// Set the animation effect
    pub fn effect(mut self, effect: AnimatedTextEffect) -> Self {
        self.effect = effect;
        self
    }

    /// Set the primary color
    pub fn primary_color(mut self, color: Color) -> Self {
        self.primary_color = color;
        self
    }

    /// Set the secondary color
    pub fn secondary_color(mut self, color: Color) -> Self {
        self.secondary_color = color;
        self
    }

    /// Set text modifiers
    pub fn modifiers(mut self, modifiers: Modifier) -> Self {
        self.modifiers = modifiers;
        self
    }

    /// Add bold modifier
    pub fn bold(mut self) -> Self {
        self.modifiers |= Modifier::BOLD;
        self
    }

    /// Add italic modifier
    pub fn italic(mut self) -> Self {
        self.modifiers |= Modifier::ITALIC;
        self
    }

    /// Set the wave width
    pub fn wave_width(mut self, width: usize) -> Self {
        self.wave_width = width.max(1);
        self
    }

    /// Set the background color
    pub fn background(mut self, color: Color) -> Self {
        self.background = Some(color);
        self
    }

    /// Set custom rainbow colors
    pub fn rainbow_colors(mut self, colors: Vec<Color>) -> Self {
        if !colors.is_empty() {
            self.rainbow_colors = colors;
        }
        self
    }

    // Preset styles

    /// Success style (green pulse)
    pub fn success() -> Self {
        Self::pulse(Color::Green, Color::LightGreen).bold()
    }

    /// Warning style (yellow pulse)
    pub fn warning() -> Self {
        Self::pulse(Color::Yellow, Color::LightYellow).bold()
    }

    /// Error style (red pulse)
    pub fn error() -> Self {
        Self::pulse(Color::Red, Color::LightRed).bold()
    }

    /// Info style (blue wave)
    pub fn info() -> Self {
        Self::wave(Color::Blue, Color::Cyan)
    }

    /// Loading style (cyan wave)
    pub fn loading() -> Self {
        Self::wave(Color::DarkGray, Color::Cyan).wave_width(5)
    }

    /// Highlight style (yellow sparkle)
    pub fn highlight() -> Self {
        Self::sparkle(Color::White, Color::Yellow)
    }
}

/// An animated text widget with color effects
///
/// Displays text with animated color transitions, including:
/// - Pulse: Color oscillates between two values
/// - Wave: A highlight travels back and forth
/// - Rainbow: Colors cycle across the text
#[derive(Debug, Clone)]
pub struct AnimatedText<'a> {
    /// The text to display
    text: &'a str,
    /// Reference to the animation state
    state: &'a AnimatedTextState,
    /// Style configuration
    style: AnimatedTextStyle,
}

impl<'a> AnimatedText<'a> {
    /// Create a new animated text widget
    pub fn new(text: &'a str, state: &'a AnimatedTextState) -> Self {
        Self {
            text,
            state,
            style: AnimatedTextStyle::default(),
        }
    }

    /// Set the style
    pub fn style(mut self, style: AnimatedTextStyle) -> Self {
        self.style = style;
        self
    }

    /// Apply a theme to derive the style
    pub fn theme(self, theme: &crate::theme::Theme) -> Self {
        self.style(AnimatedTextStyle::from(theme))
    }

    /// Set the effect directly
    pub fn effect(mut self, effect: AnimatedTextEffect) -> Self {
        self.style.effect = effect;
        self
    }

    /// Set colors directly (primary and secondary)
    pub fn colors(mut self, primary: Color, secondary: Color) -> Self {
        self.style.primary_color = primary;
        self.style.secondary_color = secondary;
        self
    }

    /// Get the display width of the text
    pub fn display_width(&self) -> usize {
        self.text.width()
    }

    /// Interpolate between two colors based on factor (0.0 to 1.0)
    fn interpolate_color(c1: Color, c2: Color, factor: f32) -> Color {
        match (c1, c2) {
            (Color::Rgb(r1, g1, b1), Color::Rgb(r2, g2, b2)) => {
                let r = (r1 as f32 + (r2 as f32 - r1 as f32) * factor) as u8;
                let g = (g1 as f32 + (g2 as f32 - g1 as f32) * factor) as u8;
                let b = (b1 as f32 + (b2 as f32 - b1 as f32) * factor) as u8;
                Color::Rgb(r, g, b)
            }
            _ => {
                // For non-RGB colors, just switch at midpoint
                if factor < 0.5 { c1 } else { c2 }
            }
        }
    }

    /// Get color for pulse effect
    fn pulse_color(&self) -> Color {
        let factor = self.state.interpolation_factor();
        Self::interpolate_color(self.style.primary_color, self.style.secondary_color, factor)
    }

    /// Get color for a specific character position in wave effect
    fn wave_color(&self, char_index: usize) -> Color {
        let wave_center = self.state.wave_position;
        let half_width = self.style.wave_width / 2;
        let start = wave_center.saturating_sub(half_width);
        let end = wave_center + half_width + 1;

        if char_index >= start && char_index < end {
            // Calculate intensity based on distance from center
            let distance = char_index.abs_diff(wave_center);
            let max_distance = half_width.max(1);
            let intensity = 1.0 - (distance as f32 / max_distance as f32);
            Self::interpolate_color(
                self.style.primary_color,
                self.style.secondary_color,
                intensity,
            )
        } else {
            self.style.primary_color
        }
    }

    /// Get color for a specific character position in rainbow effect
    fn rainbow_color(&self, char_index: usize) -> Color {
        let colors = &self.style.rainbow_colors;
        if colors.is_empty() {
            return self.style.primary_color;
        }

        // Offset the rainbow based on frame for animation
        let offset = (self.state.frame as usize) / 16;
        let color_index = (char_index + offset) % colors.len();
        colors[color_index]
    }

    /// Get color for gradient shift effect
    fn gradient_color(&self, char_index: usize, text_width: usize) -> Color {
        if text_width == 0 {
            return self.style.primary_color;
        }

        // Calculate position in gradient (0.0 to 1.0)
        let base_position = char_index as f32 / text_width.max(1) as f32;

        // Shift the gradient based on frame
        let shift = self.state.frame as f32 / 255.0;
        let position = (base_position + shift) % 1.0;

        Self::interpolate_color(
            self.style.primary_color,
            self.style.secondary_color,
            position,
        )
    }

    /// Check if a character should sparkle
    fn should_sparkle(&self, char_index: usize) -> bool {
        // Simple pseudo-random based on position and seed
        let hash = char_index
            .wrapping_mul(31)
            .wrapping_add(self.state.sparkle_seed as usize);
        hash % 8 == 0 // ~12.5% chance
    }

    /// Get color for sparkle effect
    fn sparkle_color(&self, char_index: usize) -> Color {
        if self.should_sparkle(char_index) {
            self.style.secondary_color
        } else {
            self.style.primary_color
        }
    }
}

impl Widget for AnimatedText<'_> {
    fn render(self, area: Rect, buf: &mut Buffer) {
        if area.width == 0 || area.height == 0 {
            return;
        }

        let text_width = self.text.width();
        let mut x = area.x;
        let y = area.y;

        // Build base style with modifiers
        let base_style = Style::default().add_modifier(self.style.modifiers);

        let base_style = if let Some(bg) = self.style.background {
            base_style.bg(bg)
        } else {
            base_style
        };

        // Render each character with its color
        for (char_index, ch) in self.text.chars().enumerate() {
            let ch_width = unicode_width::UnicodeWidthChar::width(ch).unwrap_or(0);

            if x >= area.x + area.width {
                break;
            }

            // Get color based on effect type
            let fg_color = match self.style.effect {
                AnimatedTextEffect::Pulse => self.pulse_color(),
                AnimatedTextEffect::Wave => self.wave_color(char_index),
                AnimatedTextEffect::Rainbow => self.rainbow_color(char_index),
                AnimatedTextEffect::GradientShift => self.gradient_color(char_index, text_width),
                AnimatedTextEffect::Sparkle => self.sparkle_color(char_index),
            };

            let style = base_style.fg(fg_color);

            // Only render if it fits
            if x as usize + ch_width <= (area.x + area.width) as usize {
                buf.set_string(x, y, ch.to_string(), style);
                x += ch_width as u16;
            }
        }

        // Clear rest of the area if text is shorter
        while x < area.x + area.width {
            buf.set_string(x, y, " ", base_style);
            x += 1;
        }
    }
}

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

    #[test]
    fn test_animated_text_state_new() {
        let state = AnimatedTextState::new();
        assert_eq!(state.frame, 0);
        assert_eq!(state.wave_position, 0);
        assert!(state.active);
    }

    #[test]
    fn test_animated_text_state_with_interval() {
        let state = AnimatedTextState::with_interval(100);
        assert_eq!(state.interval, Duration::from_millis(100));
    }

    #[test]
    fn test_animated_text_state_reset() {
        let mut state = AnimatedTextState::new();
        state.frame = 128;
        state.wave_position = 10;
        state.wave_direction = WaveDirection::Backward;

        state.reset();

        assert_eq!(state.frame, 0);
        assert_eq!(state.wave_position, 0);
        assert_eq!(state.wave_direction, WaveDirection::Forward);
    }

    #[test]
    fn test_animated_text_state_start_stop() {
        let mut state = AnimatedTextState::new();
        assert!(state.is_active());

        state.stop();
        assert!(!state.is_active());

        state.start();
        assert!(state.is_active());
    }

    #[test]
    fn test_animated_text_state_interpolation() {
        let mut state = AnimatedTextState::new();

        // At frame 0, should be near 0.5 (sin(0) = 0, (0+1)/2 = 0.5)
        let factor = state.interpolation_factor();
        assert!((factor - 0.5).abs() < 0.1);

        // At frame 64 (quarter turn), should be near 1.0
        state.frame = 64;
        let factor = state.interpolation_factor();
        assert!(factor > 0.8);

        // At frame 192 (three-quarter turn), should be near 0.0
        state.frame = 192;
        let factor = state.interpolation_factor();
        assert!(factor < 0.2);
    }

    #[test]
    fn test_animated_text_style_presets() {
        let pulse = AnimatedTextStyle::pulse(Color::Red, Color::Blue);
        assert_eq!(pulse.effect, AnimatedTextEffect::Pulse);
        assert_eq!(pulse.primary_color, Color::Red);
        assert_eq!(pulse.secondary_color, Color::Blue);

        let wave = AnimatedTextStyle::wave(Color::White, Color::Yellow);
        assert_eq!(wave.effect, AnimatedTextEffect::Wave);

        let rainbow = AnimatedTextStyle::rainbow();
        assert_eq!(rainbow.effect, AnimatedTextEffect::Rainbow);
    }

    #[test]
    fn test_animated_text_style_builder() {
        let style = AnimatedTextStyle::new()
            .effect(AnimatedTextEffect::Wave)
            .primary_color(Color::Green)
            .secondary_color(Color::Cyan)
            .wave_width(5)
            .bold();

        assert_eq!(style.effect, AnimatedTextEffect::Wave);
        assert_eq!(style.primary_color, Color::Green);
        assert_eq!(style.secondary_color, Color::Cyan);
        assert_eq!(style.wave_width, 5);
        assert!(style.modifiers.contains(Modifier::BOLD));
    }

    #[test]
    fn test_animated_text_style_presets_themed() {
        let success = AnimatedTextStyle::success();
        assert_eq!(success.primary_color, Color::Green);

        let warning = AnimatedTextStyle::warning();
        assert_eq!(warning.primary_color, Color::Yellow);

        let error = AnimatedTextStyle::error();
        assert_eq!(error.primary_color, Color::Red);

        let info = AnimatedTextStyle::info();
        assert_eq!(info.effect, AnimatedTextEffect::Wave);
    }

    #[test]
    fn test_animated_text_display_width() {
        let state = AnimatedTextState::new();
        let text = AnimatedText::new("Hello", &state);
        assert_eq!(text.display_width(), 5);

        let text = AnimatedText::new("Hello World", &state);
        assert_eq!(text.display_width(), 11);
    }

    #[test]
    fn test_animated_text_render() {
        let state = AnimatedTextState::new();
        let text = AnimatedText::new("Test", &state);

        let mut buf = Buffer::empty(Rect::new(0, 0, 10, 1));
        text.render(Rect::new(0, 0, 10, 1), &mut buf);
        // Just verify it doesn't panic
    }

    #[test]
    fn test_animated_text_render_wave() {
        let mut state = AnimatedTextState::new();
        state.wave_position = 2;

        let style = AnimatedTextStyle::wave(Color::White, Color::Yellow);
        let text = AnimatedText::new("Hello", &state).style(style);

        let mut buf = Buffer::empty(Rect::new(0, 0, 10, 1));
        text.render(Rect::new(0, 0, 10, 1), &mut buf);
        // Just verify it doesn't panic
    }

    #[test]
    fn test_animated_text_render_rainbow() {
        let state = AnimatedTextState::new();
        let style = AnimatedTextStyle::rainbow();
        let text = AnimatedText::new("Rainbow!", &state).style(style);

        let mut buf = Buffer::empty(Rect::new(0, 0, 10, 1));
        text.render(Rect::new(0, 0, 10, 1), &mut buf);
        // Just verify it doesn't panic
    }

    #[test]
    fn test_animated_text_render_empty_area() {
        let state = AnimatedTextState::new();
        let text = AnimatedText::new("Test", &state);

        let mut buf = Buffer::empty(Rect::new(0, 0, 0, 0));
        text.render(Rect::new(0, 0, 0, 0), &mut buf);
        // Should not panic on empty area
    }

    #[test]
    fn test_interpolate_color_rgb() {
        // Test RGB interpolation
        let c1 = Color::Rgb(0, 0, 0);
        let c2 = Color::Rgb(255, 255, 255);

        let result = AnimatedText::interpolate_color(c1, c2, 0.5);
        if let Color::Rgb(r, g, b) = result {
            assert!((r as i16 - 127).abs() <= 1);
            assert!((g as i16 - 127).abs() <= 1);
            assert!((b as i16 - 127).abs() <= 1);
        } else {
            panic!("Expected RGB color");
        }
    }

    #[test]
    fn test_interpolate_color_non_rgb() {
        // Non-RGB colors should switch at midpoint
        let c1 = Color::Red;
        let c2 = Color::Blue;

        assert_eq!(AnimatedText::interpolate_color(c1, c2, 0.3), Color::Red);
        assert_eq!(AnimatedText::interpolate_color(c1, c2, 0.7), Color::Blue);
    }

    #[test]
    fn test_wave_direction_changes() {
        let mut state = AnimatedTextState::new();
        state.interval = Duration::from_millis(0); // Immediate ticks
        state.last_tick = Some(Instant::now() - Duration::from_secs(1));

        // Move forward
        let text_width = 10;
        for _ in 0..15 {
            state.tick_with_text_width(text_width);
        }

        // Should have hit the end and reversed
        assert_eq!(state.wave_direction, WaveDirection::Backward);
    }
}