charmed-bubbles 0.2.0

Common TUI components for bubbletea applications
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
//! Progress bar component.
//!
//! This module provides a progress bar with optional gradient fill and
//! spring-based animations.
//!
//! # Example
//!
//! ```rust
//! use bubbles::progress::Progress;
//!
//! // Create a progress bar
//! let progress = Progress::new();
//!
//! // Render at a specific percentage
//! let view = progress.view_as(0.5);
//! ```

use std::sync::atomic::{AtomicU64, Ordering};
use std::time::Duration;

use bubbletea::{Cmd, Message, Model};
use harmonica::Spring;
use lipgloss::Style;

const FPS: u32 = 60;
const DEFAULT_WIDTH: usize = 40;
const DEFAULT_FREQUENCY: f64 = 18.0;
const DEFAULT_DAMPING: f64 = 1.0;

/// Global ID counter for progress instances.
static NEXT_ID: AtomicU64 = AtomicU64::new(1);

fn next_id() -> u64 {
    NEXT_ID.fetch_add(1, Ordering::Relaxed)
}

/// Message indicating that an animation frame should occur.
#[derive(Debug, Clone, Copy)]
pub struct FrameMsg {
    /// The progress bar ID.
    pub id: u64,
    /// Tag for message ordering.
    tag: u64,
}

/// Progress bar gradient configuration.
#[derive(Debug, Clone)]
pub struct Gradient {
    /// Start color (hex).
    pub color_a: String,
    /// End color (hex).
    pub color_b: String,
    /// Whether to scale the gradient to the filled portion.
    pub scaled: bool,
}

impl Default for Gradient {
    fn default() -> Self {
        Self {
            color_a: "#5A56E0".to_string(),
            color_b: "#EE6FF8".to_string(),
            scaled: false,
        }
    }
}

/// Progress bar model.
#[derive(Debug, Clone)]
pub struct Progress {
    /// Unique identifier.
    id: u64,
    /// Tag for frame message ordering.
    tag: u64,
    /// Total width of the progress bar.
    pub width: usize,
    /// Character for filled sections.
    pub full_char: char,
    /// Color for filled sections (when not using gradient).
    pub full_color: String,
    /// Character for empty sections.
    pub empty_char: char,
    /// Color for empty sections.
    pub empty_color: String,
    /// Whether to show percentage text.
    pub show_percentage: bool,
    /// Format string for percentage.
    pub percent_format: String,
    /// Style for percentage text.
    pub percentage_style: Style,
    /// Spring for animations.
    spring: Spring,
    /// Currently displayed percentage (for animation).
    percent_shown: f64,
    /// Target percentage (for animation).
    target_percent: f64,
    /// Animation velocity.
    velocity: f64,
    /// Gradient configuration (if using gradient).
    gradient: Option<Gradient>,
}

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

impl Progress {
    /// Creates a new progress bar with default settings.
    #[must_use]
    pub fn new() -> Self {
        Self {
            id: next_id(),
            tag: 0,
            width: DEFAULT_WIDTH,
            full_char: 'â–ˆ',
            full_color: "#7571F9".to_string(),
            empty_char: 'â–‘',
            empty_color: "#606060".to_string(),
            show_percentage: true,
            percent_format: " {:3.0}%".to_string(),
            percentage_style: Style::new(),
            spring: Spring::new(FPS as f64, DEFAULT_FREQUENCY, DEFAULT_DAMPING),
            percent_shown: 0.0,
            target_percent: 0.0,
            velocity: 0.0,
            gradient: None,
        }
    }

    /// Creates a progress bar with default gradient colors.
    #[must_use]
    pub fn with_gradient() -> Self {
        let mut p = Self::new();
        p.gradient = Some(Gradient::default());
        p
    }

    /// Creates a progress bar with custom gradient colors.
    #[must_use]
    pub fn with_gradient_colors(color_a: &str, color_b: &str) -> Self {
        let mut p = Self::new();
        p.gradient = Some(Gradient {
            color_a: color_a.to_string(),
            color_b: color_b.to_string(),
            scaled: false,
        });
        p
    }

    /// Creates a progress bar with a scaled gradient.
    #[must_use]
    pub fn with_scaled_gradient(color_a: &str, color_b: &str) -> Self {
        let mut p = Self::new();
        p.gradient = Some(Gradient {
            color_a: color_a.to_string(),
            color_b: color_b.to_string(),
            scaled: true,
        });
        p
    }

    /// Sets the width of the progress bar.
    #[must_use]
    pub fn width(mut self, width: usize) -> Self {
        self.width = width;
        self
    }

    /// Sets the fill characters.
    #[must_use]
    pub fn fill_chars(mut self, full: char, empty: char) -> Self {
        self.full_char = full;
        self.empty_char = empty;
        self
    }

    /// Sets the solid fill color (disables gradient).
    #[must_use]
    pub fn solid_fill(mut self, color: &str) -> Self {
        self.full_color = color.to_string();
        self.gradient = None;
        self
    }

    /// Disables percentage display.
    #[must_use]
    pub fn without_percentage(mut self) -> Self {
        self.show_percentage = false;
        self
    }

    /// Sets the spring animation parameters.
    pub fn set_spring_options(&mut self, frequency: f64, damping: f64) {
        self.spring = Spring::new(FPS as f64, frequency, damping);
    }

    /// Returns the progress bar's unique ID.
    #[must_use]
    pub fn id(&self) -> u64 {
        self.id
    }

    /// Returns the current target percentage.
    #[must_use]
    pub fn percent(&self) -> f64 {
        self.target_percent
    }

    /// Sets the percentage and returns a command to start animation.
    pub fn set_percent(&mut self, p: f64) -> Option<Cmd> {
        self.target_percent = if p.is_finite() {
            p.clamp(0.0, 1.0)
        } else {
            0.0
        };
        self.tag = self.tag.wrapping_add(1);
        self.next_frame()
    }

    /// Increments the percentage.
    pub fn incr_percent(&mut self, v: f64) -> Option<Cmd> {
        self.set_percent(self.percent() + v)
    }

    /// Decrements the percentage.
    pub fn decr_percent(&mut self, v: f64) -> Option<Cmd> {
        self.set_percent(self.percent() - v)
    }

    /// Returns whether the progress bar is still animating.
    #[must_use]
    pub fn is_animating(&self) -> bool {
        let dist = (self.percent_shown - self.target_percent).abs();
        !(dist < 0.001 && self.velocity.abs() < 0.01)
    }

    /// Creates a command for the next animation frame.
    fn next_frame(&self) -> Option<Cmd> {
        let id = self.id;
        let tag = self.tag;
        let delay = Duration::from_secs_f64(1.0 / f64::from(FPS));

        Some(Cmd::new(move || {
            std::thread::sleep(delay);
            Message::new(FrameMsg { id, tag })
        }))
    }

    /// Updates the progress bar state.
    pub fn update(&mut self, msg: Message) -> Option<Cmd> {
        if let Some(frame) = msg.downcast_ref::<FrameMsg>() {
            if frame.id != self.id || frame.tag != self.tag {
                return None;
            }

            if !self.is_animating() {
                return None;
            }

            let (new_pos, new_vel) =
                self.spring
                    .update(self.percent_shown, self.velocity, self.target_percent);
            self.percent_shown = new_pos;
            self.velocity = new_vel;

            return self.next_frame();
        }

        None
    }

    /// Renders the progress bar at the current animated position.
    #[must_use]
    pub fn view(&self) -> String {
        self.view_as(self.percent_shown)
    }

    /// Renders the progress bar at a specific percentage.
    #[must_use]
    pub fn view_as(&self, percent: f64) -> String {
        let mut result = String::new();
        let percent_view = self.percentage_view(percent);
        let percent_width = percent_view.chars().count();

        self.bar_view(&mut result, percent, percent_width);
        result.push_str(&percent_view);
        result
    }

    fn bar_view(&self, buf: &mut String, percent: f64, text_width: usize) {
        use unicode_width::UnicodeWidthChar;

        let full_width = self.full_char.width().unwrap_or(1).max(1);
        let empty_width = self.empty_char.width().unwrap_or(1).max(1);

        let available_width = self.width.saturating_sub(text_width);
        let filled_target_width =
            ((available_width as f64 * percent).round() as usize).min(available_width);

        let filled_count = filled_target_width / full_width;
        let filled_visual_width = filled_count * full_width;

        let empty_target_width = available_width.saturating_sub(filled_visual_width);
        let empty_count = empty_target_width / empty_width;

        if let Some(ref gradient) = self.gradient {
            // Gradient fill
            for i in 0..filled_count {
                let p = if filled_count <= 1 {
                    0.5
                } else if gradient.scaled {
                    i as f64 / (filled_count - 1) as f64
                } else {
                    (i * full_width) as f64 / (available_width.saturating_sub(1)).max(1) as f64
                };

                // Simple linear interpolation between colors
                let color = interpolate_color(&gradient.color_a, &gradient.color_b, p);
                buf.push_str(&format!("\x1b[38;2;{}m{}\x1b[0m", color, self.full_char));
            }
        } else {
            // Solid fill
            let colored_char =
                format_colored_char(self.full_char, &self.full_color).repeat(filled_count);
            buf.push_str(&colored_char);
        }

        // Empty fill
        let empty_colored = format_colored_char(self.empty_char, &self.empty_color);
        for _ in 0..empty_count {
            buf.push_str(&empty_colored);
        }

        // Pad remaining space if chars don't divide width evenly
        let used = (filled_count * full_width) + (empty_count * empty_width);
        let remaining = available_width.saturating_sub(used);
        if remaining > 0 {
            buf.push_str(&" ".repeat(remaining));
        }
    }

    fn percentage_view(&self, percent: f64) -> String {
        if !self.show_percentage {
            return String::new();
        }
        let percent = percent.clamp(0.0, 1.0) * 100.0;
        // Use the configurable percent_format field, replacing the placeholder
        // Supports {:3.0} (default) and {} (simple) format placeholders
        let formatted = format!("{:3.0}", percent);
        if self.percent_format.contains("{:3.0}") {
            self.percent_format.replace("{:3.0}", &formatted)
        } else {
            self.percent_format.replace("{}", &formatted)
        }
    }
}

/// Format a character with ANSI color.
fn format_colored_char(c: char, hex_color: &str) -> String {
    if let Some(rgb) = parse_hex_color(hex_color) {
        format!("\x1b[38;2;{};{};{}m{}\x1b[0m", rgb.0, rgb.1, rgb.2, c)
    } else {
        c.to_string()
    }
}

/// Parse a hex color string to RGB.
fn parse_hex_color(hex: &str) -> Option<(u8, u8, u8)> {
    let hex = hex.trim_start_matches('#');
    if hex.len() != 6 {
        return None;
    }
    let r = u8::from_str_radix(&hex[0..2], 16).ok()?;
    let g = u8::from_str_radix(&hex[2..4], 16).ok()?;
    let b = u8::from_str_radix(&hex[4..6], 16).ok()?;
    Some((r, g, b))
}

/// Interpolate between two hex colors.
fn interpolate_color(color_a: &str, color_b: &str, t: f64) -> String {
    let a = parse_hex_color(color_a).unwrap_or((0, 0, 0));
    let b = parse_hex_color(color_b).unwrap_or((0, 0, 0));

    let r = (a.0 as f64 + (b.0 as f64 - a.0 as f64) * t).round() as u8;
    let g = (a.1 as f64 + (b.1 as f64 - a.1 as f64) * t).round() as u8;
    let bl = (a.2 as f64 + (b.2 as f64 - a.2 as f64) * t).round() as u8;

    format!("{};{};{}", r, g, bl)
}

impl Model for Progress {
    /// Initialize the progress bar.
    ///
    /// Progress bars don't require initialization commands.
    fn init(&self) -> Option<Cmd> {
        None
    }

    /// Update the progress bar state based on incoming messages.
    fn update(&mut self, msg: Message) -> Option<Cmd> {
        Progress::update(self, msg)
    }

    /// Render the progress bar at the current animated position.
    fn view(&self) -> String {
        Progress::view(self)
    }
}

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

    #[test]
    fn test_progress_new() {
        let p = Progress::new();
        assert_eq!(p.width, DEFAULT_WIDTH);
        assert!(p.show_percentage);
        assert_eq!(p.percent(), 0.0);
    }

    #[test]
    fn test_progress_unique_ids() {
        let p1 = Progress::new();
        let p2 = Progress::new();
        assert_ne!(p1.id(), p2.id());
    }

    #[test]
    fn test_progress_set_percent() {
        let mut p = Progress::new();
        p.set_percent(0.5);
        assert!((p.percent() - 0.5).abs() < 0.001);
    }

    #[test]
    fn test_progress_percent_clamp() {
        let mut p = Progress::new();
        p.set_percent(1.5);
        assert!((p.percent() - 1.0).abs() < 0.001);

        p.set_percent(-0.5);
        assert!(p.percent().abs() < 0.001);
    }

    #[test]
    fn test_progress_view_as() {
        let p = Progress::new().width(20).without_percentage();
        let view = p.view_as(0.5);
        // Should have some filled and empty chars
        assert!(!view.is_empty());
    }

    #[test]
    fn test_progress_builder() {
        let p = Progress::new()
            .width(50)
            .fill_chars('#', '-')
            .without_percentage();

        assert_eq!(p.width, 50);
        assert_eq!(p.full_char, '#');
        assert_eq!(p.empty_char, '-');
        assert!(!p.show_percentage);
    }

    #[test]
    fn test_progress_with_gradient() {
        let p = Progress::with_gradient();
        assert!(p.gradient.is_some());
    }

    #[test]
    fn test_parse_hex_color() {
        assert_eq!(parse_hex_color("#FF0000"), Some((255, 0, 0)));
        assert_eq!(parse_hex_color("#00FF00"), Some((0, 255, 0)));
        assert_eq!(parse_hex_color("#0000FF"), Some((0, 0, 255)));
        assert_eq!(parse_hex_color("FFFFFF"), Some((255, 255, 255)));
        assert_eq!(parse_hex_color("invalid"), None);
    }

    #[test]
    fn test_interpolate_color() {
        let mid = interpolate_color("#000000", "#FFFFFF", 0.5);
        // Should be approximately middle gray
        assert!(mid.contains("127") || mid.contains("128"));
    }

    #[test]
    fn test_progress_animation_state() {
        let mut p = Progress::new();
        p.percent_shown = 0.5;
        p.target_percent = 0.5;
        p.velocity = 0.0;
        assert!(!p.is_animating());

        p.target_percent = 0.8;
        assert!(p.is_animating());
    }

    #[test]
    fn test_progress_animation_negative_velocity() {
        // Test that negative velocity is correctly detected as animating.
        // In an under-damped spring, velocity oscillates and can be negative
        // even when close to the target position.
        let mut p = Progress::new();
        p.percent_shown = 0.5;
        p.target_percent = 0.5;
        p.velocity = -0.5; // Significant negative velocity (moving backward)

        // Should still be animating because of momentum, even though at target
        assert!(
            p.is_animating(),
            "Should be animating with significant negative velocity"
        );

        // Small negative velocity should not be animating
        p.velocity = -0.001;
        assert!(
            !p.is_animating(),
            "Should not be animating with tiny negative velocity at target"
        );
    }

    // Model trait implementation tests
    #[test]
    fn test_model_init() {
        let p = Progress::new();
        // Progress bars don't require init commands
        let cmd = Model::init(&p);
        assert!(cmd.is_none());
    }

    #[test]
    fn test_model_view() {
        let p = Progress::new();
        // Model::view should return same result as Progress::view
        let model_view = Model::view(&p);
        let progress_view = Progress::view(&p);
        assert_eq!(model_view, progress_view);
    }

    #[test]
    fn test_model_update_handles_frame_msg() {
        let mut p = Progress::new();
        p.target_percent = 1.0;
        p.percent_shown = 0.0;
        p.velocity = 0.0;
        let id = p.id();
        let tag = p.tag;

        // Use Model::update explicitly
        let frame_msg = Message::new(FrameMsg { id, tag });
        let cmd = Model::update(&mut p, frame_msg);

        // Should return a command for the next frame (animating)
        assert!(
            cmd.is_some(),
            "Model::update should return next frame command when animating"
        );
        // Percent should have changed
        assert!(p.percent_shown > 0.0, "percent_shown should have advanced");
    }

    #[test]
    fn test_model_update_ignores_wrong_id() {
        let mut p = Progress::new();
        p.target_percent = 1.0;
        p.percent_shown = 0.0;
        let original_percent = p.percent_shown;

        // Send frame message with wrong ID
        let frame_msg = Message::new(FrameMsg { id: 99999, tag: 0 });
        let cmd = Model::update(&mut p, frame_msg);

        assert!(
            cmd.is_none(),
            "Should ignore messages for other progress bars"
        );
        assert!(
            (p.percent_shown - original_percent).abs() < 0.001,
            "percent_shown should not change"
        );
    }

    #[test]
    fn test_model_update_ignores_wrong_tag() {
        let mut p = Progress::new();
        p.target_percent = 1.0;
        p.percent_shown = 0.0;
        p.tag = 5;
        let id = p.id();
        let original_percent = p.percent_shown;

        // Send frame message with wrong tag
        let frame_msg = Message::new(FrameMsg { id, tag: 3 });
        let cmd = Model::update(&mut p, frame_msg);

        assert!(cmd.is_none(), "Should ignore messages with old tag");
        assert!(
            (p.percent_shown - original_percent).abs() < 0.001,
            "percent_shown should not change"
        );
    }

    #[test]
    fn test_progress_satisfies_model_bounds() {
        // Verify Progress can be used where Model + Send + 'static is required
        fn accepts_model<M: Model + Send + 'static>(_model: M) {}
        let p = Progress::new();
        accepts_model(p);
    }
}