blaeck 0.4.0

A component-based terminal UI framework for Rust
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
//! Progress bar component - visual progress indicator.
//!
//! The Progress component displays a progress bar showing
//! completion percentage with customizable styling. Includes 6 built-in styles.
//!
//! ## When to use Progress
//!
//! - Determinate operations (known percentage complete)
//! - File downloads, uploads, processing tasks
//! - Multi-step wizards showing completion
//!
//! ## See also
//!
//! - [`Spinner`](super::Spinner) — Use for indeterminate loading (unknown duration)
//! - [`Timer`](super::Timer) — Show elapsed/remaining time alongside progress

use crate::element::{Component, Element};
use crate::style::{Color, Modifier, Style};

/// Built-in progress bar styles.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ProgressStyle {
    /// Block style: ████████░░░░
    #[default]
    Block,
    /// ASCII style: ========----
    Ascii,
    /// Thin style: ───────○────
    Thin,
    /// Thick style: ▓▓▓▓▓▓▓░░░░░
    Thick,
    /// Dots style: ●●●●●○○○○○
    Dots,
    /// Braille style: ⣿⣿⣿⣿⣀⣀⣀⣀
    Braille,
}

impl ProgressStyle {
    /// Get the characters for filled and empty portions.
    pub fn chars(&self) -> ProgressChars {
        match self {
            ProgressStyle::Block => ProgressChars {
                filled: '',
                empty: '',
                head: None,
            },
            ProgressStyle::Ascii => ProgressChars {
                filled: '=',
                empty: '-',
                head: Some('>'),
            },
            ProgressStyle::Thin => ProgressChars {
                filled: '',
                empty: '',
                head: Some(''),
            },
            ProgressStyle::Thick => ProgressChars {
                filled: '',
                empty: '',
                head: None,
            },
            ProgressStyle::Dots => ProgressChars {
                filled: '',
                empty: '',
                head: None,
            },
            ProgressStyle::Braille => ProgressChars {
                filled: '',
                empty: '',
                head: None,
            },
        }
    }
}

/// Characters used to render the progress bar.
#[derive(Debug, Clone, Copy)]
pub struct ProgressChars {
    /// Character for filled portion.
    pub filled: char,
    /// Character for empty portion.
    pub empty: char,
    /// Optional character for the head/cursor position.
    pub head: Option<char>,
}

impl Default for ProgressChars {
    fn default() -> Self {
        ProgressStyle::Block.chars()
    }
}

/// Properties for the Progress component.
#[derive(Debug, Clone)]
pub struct ProgressProps {
    /// Progress value from 0.0 to 1.0.
    pub progress: f32,
    /// Width of the progress bar in characters.
    pub width: usize,
    /// Progress bar style.
    pub style: ProgressStyle,
    /// Custom characters (overrides style if set).
    pub custom_chars: Option<ProgressChars>,
    /// Whether to show percentage text.
    pub show_percentage: bool,
    /// Optional label to show before the bar.
    pub label: Option<String>,
    /// Color for filled portion.
    pub filled_color: Option<Color>,
    /// Color for empty portion.
    pub empty_color: Option<Color>,
    /// Color for percentage text.
    pub text_color: Option<Color>,
    /// Background color.
    pub bg_color: Option<Color>,
    /// Whether to show brackets around the bar.
    pub brackets: bool,
    /// Whether the filled portion should be bold.
    pub bold: bool,
    /// Whether the empty portion should be dimmed.
    pub dim_empty: bool,
}

impl Default for ProgressProps {
    fn default() -> Self {
        Self {
            progress: 0.0,
            width: 20,
            style: ProgressStyle::Block,
            custom_chars: None,
            show_percentage: false,
            label: None,
            filled_color: None,
            empty_color: None,
            text_color: None,
            bg_color: None,
            brackets: false,
            bold: false,
            dim_empty: true,
        }
    }
}

impl ProgressProps {
    /// Create new ProgressProps with the given progress value.
    pub fn new(progress: f32) -> Self {
        Self {
            progress: progress.clamp(0.0, 1.0),
            ..Default::default()
        }
    }

    /// Set the progress value (0.0 to 1.0).
    #[must_use]
    pub fn progress(mut self, progress: f32) -> Self {
        self.progress = progress.clamp(0.0, 1.0);
        self
    }

    /// Set the progress from a percentage (0 to 100).
    #[must_use]
    pub fn percent(mut self, percent: u32) -> Self {
        self.progress = (percent.min(100) as f32) / 100.0;
        self
    }

    /// Set the width in characters.
    #[must_use]
    pub fn width(mut self, width: usize) -> Self {
        self.width = width.max(1);
        self
    }

    /// Set the progress bar style.
    #[must_use]
    pub fn style(mut self, style: ProgressStyle) -> Self {
        self.style = style;
        self
    }

    /// Set custom characters.
    #[must_use]
    pub fn custom_chars(mut self, chars: ProgressChars) -> Self {
        self.custom_chars = Some(chars);
        self
    }

    /// Show percentage text after the bar.
    #[must_use]
    pub fn show_percentage(mut self) -> Self {
        self.show_percentage = true;
        self
    }

    /// Set a label to show before the bar.
    #[must_use]
    pub fn label(mut self, label: impl Into<String>) -> Self {
        self.label = Some(label.into());
        self
    }

    /// Set the color for the filled portion.
    #[must_use]
    pub fn filled_color(mut self, color: Color) -> Self {
        self.filled_color = Some(color);
        self
    }

    /// Set the color for the empty portion.
    #[must_use]
    pub fn empty_color(mut self, color: Color) -> Self {
        self.empty_color = Some(color);
        self
    }

    /// Set the color for percentage/label text.
    #[must_use]
    pub fn text_color(mut self, color: Color) -> Self {
        self.text_color = Some(color);
        self
    }

    /// Set a single color for the entire bar.
    #[must_use]
    pub fn color(mut self, color: Color) -> Self {
        self.filled_color = Some(color);
        self.empty_color = Some(color);
        self.text_color = Some(color);
        self
    }

    /// Show brackets around the bar.
    #[must_use]
    pub fn brackets(mut self) -> Self {
        self.brackets = true;
        self
    }

    /// Make the filled portion bold.
    #[must_use]
    pub fn bold(mut self) -> Self {
        self.bold = true;
        self
    }

    /// Dim the empty portion.
    #[must_use]
    pub fn dim_empty(mut self, dim: bool) -> Self {
        self.dim_empty = dim;
        self
    }

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

    /// Get the characters to use for rendering.
    fn chars(&self) -> ProgressChars {
        self.custom_chars.unwrap_or_else(|| self.style.chars())
    }

    /// Get the percentage as an integer (0-100).
    pub fn percentage(&self) -> u32 {
        (self.progress * 100.0).round() as u32
    }

    /// Build the progress bar string.
    pub fn render_string(&self) -> String {
        let chars = self.chars();
        let filled_count = ((self.progress * self.width as f32).round() as usize).min(self.width);
        let empty_count = self.width - filled_count;

        let mut result = String::new();

        // Label
        if let Some(ref label) = self.label {
            result.push_str(label);
            result.push(' ');
        }

        // Opening bracket
        if self.brackets {
            result.push('[');
        }

        // Filled portion
        if let Some(head) = chars.head {
            // With head character
            if filled_count > 0 {
                result.extend(std::iter::repeat_n(chars.filled, filled_count - 1));
                if filled_count < self.width {
                    result.push(head);
                } else {
                    result.push(chars.filled);
                }
            }
        } else {
            // Without head character
            result.extend(std::iter::repeat_n(chars.filled, filled_count));
        }

        // Empty portion
        result.extend(std::iter::repeat_n(chars.empty, empty_count));

        // Closing bracket
        if self.brackets {
            result.push(']');
        }

        // Percentage
        if self.show_percentage {
            result.push_str(&format!(" {:>3}%", self.percentage()));
        }

        result
    }
}

/// A component that displays a progress bar.
///
/// # Examples
///
/// ```ignore
/// // Simple progress bar
/// let props = ProgressProps::new(0.75)
///     .width(30)
///     .color(Color::Green)
///     .show_percentage();
///
/// let elem = Progress::render(&props);
/// // Renders: ██████████████████████░░░░░░░░ 75%
/// ```
pub struct Progress;

impl Component for Progress {
    type Props = ProgressProps;

    fn render(props: &Self::Props) -> Element {
        let content = props.render_string();

        // Build style
        let mut style = Style::new();
        if let Some(color) = props.filled_color {
            style = style.fg(color);
        }
        if let Some(bg) = props.bg_color {
            style = style.bg(bg);
        }
        if props.bold {
            style = style.add_modifier(Modifier::BOLD);
        }

        Element::styled_text(&content, style)
    }
}

/// Helper to create a simple progress bar string.
///
/// # Example
///
/// ```ignore
/// let bar = progress_bar(75, 20); // 75%, 20 chars wide
/// // Returns: "███████████████░░░░░"
/// ```
pub fn progress_bar(percent: u32, width: usize) -> String {
    ProgressProps::new(percent as f32 / 100.0)
        .width(width)
        .render_string()
}

/// Helper to create a bracketed progress bar string.
///
/// # Example
///
/// ```ignore
/// let bar = progress_bar_bracketed(50, 15); // 50%, 15 chars wide
/// // Returns: "[███████░░░░░░░░]"
/// ```
pub fn progress_bar_bracketed(percent: u32, width: usize) -> String {
    ProgressProps::new(percent as f32 / 100.0)
        .width(width)
        .brackets()
        .render_string()
}

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

    #[test]
    fn test_progress_props_default() {
        let props = ProgressProps::default();
        assert_eq!(props.progress, 0.0);
        assert_eq!(props.width, 20);
        assert_eq!(props.style, ProgressStyle::Block);
    }

    #[test]
    fn test_progress_props_new() {
        let props = ProgressProps::new(0.5);
        assert_eq!(props.progress, 0.5);
    }

    #[test]
    fn test_progress_props_clamp() {
        let props = ProgressProps::new(1.5);
        assert_eq!(props.progress, 1.0);

        let props = ProgressProps::new(-0.5);
        assert_eq!(props.progress, 0.0);
    }

    #[test]
    fn test_progress_props_percent() {
        let props = ProgressProps::default().percent(75);
        assert_eq!(props.progress, 0.75);
    }

    #[test]
    fn test_progress_props_builder() {
        let props = ProgressProps::new(0.5)
            .width(30)
            .style(ProgressStyle::Ascii)
            .filled_color(Color::Green)
            .show_percentage()
            .brackets();

        assert_eq!(props.progress, 0.5);
        assert_eq!(props.width, 30);
        assert_eq!(props.style, ProgressStyle::Ascii);
        assert_eq!(props.filled_color, Some(Color::Green));
        assert!(props.show_percentage);
        assert!(props.brackets);
    }

    #[test]
    fn test_progress_percentage() {
        let props = ProgressProps::new(0.756);
        assert_eq!(props.percentage(), 76);
    }

    #[test]
    fn test_progress_render_string_empty() {
        let props = ProgressProps::new(0.0).width(10);
        let bar = props.render_string();
        assert_eq!(bar, "░░░░░░░░░░");
    }

    #[test]
    fn test_progress_render_string_full() {
        let props = ProgressProps::new(1.0).width(10);
        let bar = props.render_string();
        assert_eq!(bar, "██████████");
    }

    #[test]
    fn test_progress_render_string_half() {
        let props = ProgressProps::new(0.5).width(10);
        let bar = props.render_string();
        assert_eq!(bar, "█████░░░░░");
    }

    #[test]
    fn test_progress_render_string_with_brackets() {
        let props = ProgressProps::new(0.5).width(10).brackets();
        let bar = props.render_string();
        assert_eq!(bar, "[█████░░░░░]");
    }

    #[test]
    fn test_progress_render_string_with_percentage() {
        let props = ProgressProps::new(0.75).width(10).show_percentage();
        let bar = props.render_string();
        assert!(bar.ends_with(" 75%"));
    }

    #[test]
    fn test_progress_render_string_with_label() {
        let props = ProgressProps::new(0.5).width(10).label("Loading");
        let bar = props.render_string();
        assert!(bar.starts_with("Loading "));
    }

    #[test]
    fn test_progress_render_string_ascii_style() {
        let props = ProgressProps::new(0.5)
            .width(10)
            .style(ProgressStyle::Ascii);
        let bar = props.render_string();
        // ASCII style has a head character '>'
        assert!(bar.contains('='));
        assert!(bar.contains('>'));
        assert!(bar.contains('-'));
    }

    #[test]
    fn test_progress_style_block_chars() {
        let chars = ProgressStyle::Block.chars();
        assert_eq!(chars.filled, '');
        assert_eq!(chars.empty, '');
        assert!(chars.head.is_none());
    }

    #[test]
    fn test_progress_style_ascii_chars() {
        let chars = ProgressStyle::Ascii.chars();
        assert_eq!(chars.filled, '=');
        assert_eq!(chars.empty, '-');
        assert_eq!(chars.head, Some('>'));
    }

    #[test]
    fn test_progress_helper_function() {
        let bar = progress_bar(50, 10);
        assert_eq!(bar, "█████░░░░░");
    }

    #[test]
    fn test_progress_helper_bracketed() {
        let bar = progress_bar_bracketed(50, 10);
        assert_eq!(bar, "[█████░░░░░]");
    }

    #[test]
    fn test_progress_render_component() {
        let props = ProgressProps::new(0.5).width(10);
        let elem = Progress::render(&props);
        match elem {
            Element::Text { content, .. } => {
                assert_eq!(content, "█████░░░░░");
            }
            _ => panic!("Expected Text element"),
        }
    }

    #[test]
    fn test_all_progress_styles() {
        let styles = [
            ProgressStyle::Block,
            ProgressStyle::Ascii,
            ProgressStyle::Thin,
            ProgressStyle::Thick,
            ProgressStyle::Dots,
            ProgressStyle::Braille,
        ];

        for style in &styles {
            let props = ProgressProps::new(0.5).width(10).style(*style);
            let bar = props.render_string();
            assert_eq!(
                bar.chars().count(),
                10,
                "Style {:?} has wrong length",
                style
            );
        }
    }
}