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
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
//! Modal/Dialog component for displaying overlays and prompts.
//!
//! ## When to use Modal
//!
//! - Confirmation dialogs ("Are you sure?")
//! - Error/warning/success messages
//! - Information that needs acknowledgment before continuing
//!
//! ## See also
//!
//! - [`Confirm`](super::Confirm) — Inline yes/no without a modal box
//! - [`Box`](super::Box) — Build custom containers (Modal is built on Box)
//!
//! # Example
//!
//! ```ignore
//! use blaeck::prelude::*;
//!
//! let modal = Element::node::<Modal>(
//!     ModalProps::new("Confirm Action")
//!         .body("Are you sure you want to proceed?")
//!         .style(ModalStyle::Warning)
//!         .buttons(vec![
//!             ModalButton::cancel(),
//!             ModalButton::ok(),
//!         ]),
//!     vec![],
//! );
//! ```

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

/// Modal visual style.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ModalStyle {
    /// Default style (no special coloring).
    #[default]
    Default,
    /// Info style (blue/cyan).
    Info,
    /// Success style (green).
    Success,
    /// Warning style (yellow).
    Warning,
    /// Error/danger style (red).
    Error,
}

impl ModalStyle {
    /// Get the title color for this style.
    pub fn title_color(&self) -> Option<Color> {
        match self {
            ModalStyle::Default => None,
            ModalStyle::Info => Some(Color::Cyan),
            ModalStyle::Success => Some(Color::Green),
            ModalStyle::Warning => Some(Color::Yellow),
            ModalStyle::Error => Some(Color::Red),
        }
    }

    /// Get the border color for this style.
    pub fn border_color(&self) -> Option<Color> {
        match self {
            ModalStyle::Default => Some(Color::White),
            ModalStyle::Info => Some(Color::Cyan),
            ModalStyle::Success => Some(Color::Green),
            ModalStyle::Warning => Some(Color::Yellow),
            ModalStyle::Error => Some(Color::Red),
        }
    }

    /// Get the icon for this style.
    pub fn icon(&self) -> Option<&'static str> {
        match self {
            ModalStyle::Default => None,
            ModalStyle::Info => Some(""),
            ModalStyle::Success => Some(""),
            ModalStyle::Warning => Some(""),
            ModalStyle::Error => Some(""),
        }
    }
}

/// A button in the modal footer.
#[derive(Debug, Clone)]
pub struct ModalButton {
    /// Button label.
    pub label: String,
    /// Button color.
    pub color: Option<Color>,
    /// Whether this button is highlighted/primary.
    pub primary: bool,
}

impl ModalButton {
    /// Create a new button.
    pub fn new(label: impl Into<String>) -> Self {
        Self {
            label: label.into(),
            color: None,
            primary: false,
        }
    }

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

    /// Mark as primary button.
    #[must_use]
    pub fn primary(mut self) -> Self {
        self.primary = true;
        self
    }

    /// Create an "OK" button.
    pub fn ok() -> Self {
        Self::new("OK").color(Color::Green).primary()
    }

    /// Create a "Cancel" button.
    pub fn cancel() -> Self {
        Self::new("Cancel").color(Color::DarkGray)
    }

    /// Create a "Yes" button.
    pub fn yes() -> Self {
        Self::new("Yes").color(Color::Green).primary()
    }

    /// Create a "No" button.
    pub fn no() -> Self {
        Self::new("No").color(Color::Red)
    }

    /// Create a "Confirm" button.
    pub fn confirm() -> Self {
        Self::new("Confirm").color(Color::Green).primary()
    }

    /// Create a "Delete" button.
    pub fn delete() -> Self {
        Self::new("Delete").color(Color::Red).primary()
    }

    /// Create a "Close" button.
    pub fn close() -> Self {
        Self::new("Close").color(Color::DarkGray)
    }
}

/// Properties for the Modal component.
#[derive(Debug, Clone)]
pub struct ModalProps {
    /// Modal title.
    pub title: String,
    /// Modal body text.
    pub body: Option<String>,
    /// Modal style.
    pub style: ModalStyle,
    /// Border style.
    pub border_style: BorderStyle,
    /// Buttons in footer.
    pub buttons: Vec<ModalButton>,
    /// Minimum width.
    pub min_width: usize,
    /// Maximum width (0 = no limit).
    pub max_width: usize,
    /// Padding inside the modal.
    pub padding: usize,
    /// Show icon based on style.
    pub show_icon: bool,
    /// Center the title.
    pub center_title: bool,
    /// Dim the border.
    pub dim_border: bool,
}

impl Default for ModalProps {
    fn default() -> Self {
        Self {
            title: String::new(),
            body: None,
            style: ModalStyle::Default,
            border_style: BorderStyle::Round,
            buttons: Vec::new(),
            min_width: 30,
            max_width: 60,
            padding: 1,
            show_icon: true,
            center_title: false,
            dim_border: false,
        }
    }
}

impl ModalProps {
    /// Create new modal props with a title.
    pub fn new(title: impl Into<String>) -> Self {
        Self {
            title: title.into(),
            ..Default::default()
        }
    }

    /// Set the body text.
    #[must_use]
    pub fn body(mut self, body: impl Into<String>) -> Self {
        self.body = Some(body.into());
        self
    }

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

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

    /// Set the buttons.
    #[must_use]
    pub fn buttons(mut self, buttons: Vec<ModalButton>) -> Self {
        self.buttons = buttons;
        self
    }

    /// Add a single button.
    #[must_use]
    pub fn button(mut self, button: ModalButton) -> Self {
        self.buttons.push(button);
        self
    }

    /// Set minimum width.
    #[must_use]
    pub fn min_width(mut self, width: usize) -> Self {
        self.min_width = width;
        self
    }

    /// Set maximum width.
    #[must_use]
    pub fn max_width(mut self, width: usize) -> Self {
        self.max_width = width;
        self
    }

    /// Set padding.
    #[must_use]
    pub fn padding(mut self, padding: usize) -> Self {
        self.padding = padding;
        self
    }

    /// Show or hide the style icon.
    #[must_use]
    pub fn show_icon(mut self, show: bool) -> Self {
        self.show_icon = show;
        self
    }

    /// Center the title.
    #[must_use]
    pub fn center_title(mut self, center: bool) -> Self {
        self.center_title = center;
        self
    }

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

/// A modal/dialog component.
pub struct Modal;

impl Component for Modal {
    type Props = ModalProps;

    fn render(props: &Self::Props) -> Element {
        let chars = props.border_style.chars();
        let border_color = props.style.border_color();
        let title_color = props.style.title_color();

        // Build title with optional icon
        let title = if props.show_icon {
            if let Some(icon) = props.style.icon() {
                format!("{} {}", icon, props.title)
            } else {
                props.title.clone()
            }
        } else {
            props.title.clone()
        };

        // Calculate content width
        let title_width = unicode_width::UnicodeWidthStr::width(title.as_str());
        let body_width = props.body.as_ref().map_or(0, |b| {
            b.lines()
                .map(unicode_width::UnicodeWidthStr::width)
                .max()
                .unwrap_or(0)
        });
        let buttons_width: usize = props
            .buttons
            .iter()
            .map(|b| unicode_width::UnicodeWidthStr::width(b.label.as_str()) + 4) // [ label ]
            .sum::<usize>()
            + props.buttons.len().saturating_sub(1) * 2; // spacing between buttons

        let content_width = title_width.max(body_width).max(buttons_width);
        let inner_width = content_width + props.padding * 2;
        let inner_width = inner_width.max(props.min_width);
        let inner_width = if props.max_width > 0 {
            inner_width.min(props.max_width)
        } else {
            inner_width
        };
        let _total_width = inner_width + 2; // +2 for borders

        let mut lines: Vec<Element> = Vec::new();

        // Border style
        let border_style = if props.dim_border {
            border_color
                .map(|c| Style::new().fg(c).add_modifier(Modifier::DIM))
                .unwrap_or_else(|| Style::new().add_modifier(Modifier::DIM))
        } else {
            border_color.map(|c| Style::new().fg(c)).unwrap_or_default()
        };

        // Top border line
        let top_line = format!(
            "{}{}{}",
            chars.top_left,
            chars.horizontal.to_string().repeat(inner_width),
            chars.top_right
        );
        lines.push(Element::styled_text(&top_line, border_style));

        // Title line
        let mut title_segments = vec![Element::styled_text(
            chars.vertical.to_string(),
            border_style,
        )];

        let title_style = title_color
            .map(|c| Style::new().fg(c).add_modifier(Modifier::BOLD))
            .unwrap_or_else(|| Style::new().add_modifier(Modifier::BOLD));

        if props.center_title {
            let padding_total = inner_width.saturating_sub(title_width);
            let left_pad = padding_total / 2;
            let right_pad = padding_total - left_pad;
            title_segments.push(Element::text(" ".repeat(left_pad)));
            title_segments.push(Element::styled_text(&title, title_style));
            title_segments.push(Element::text(" ".repeat(right_pad)));
        } else {
            let left_pad = " ".repeat(props.padding);
            let right_pad = " ".repeat(inner_width.saturating_sub(title_width + props.padding));
            title_segments.push(Element::text(&left_pad));
            title_segments.push(Element::styled_text(&title, title_style));
            title_segments.push(Element::text(&right_pad));
        }
        title_segments.push(Element::styled_text(
            chars.vertical.to_string(),
            border_style,
        ));
        lines.push(Element::Fragment(title_segments));

        // Separator line under title
        let sep_line = format!(
            "{}{}{}",
            chars.vertical,
            chars.horizontal.to_string().repeat(inner_width),
            chars.vertical
        );
        lines.push(Element::styled_text(&sep_line, border_style));

        // Body lines
        if let Some(ref body) = props.body {
            // Empty line for padding
            if props.padding > 0 {
                let empty_line = format!(
                    "{}{}{}",
                    chars.vertical,
                    " ".repeat(inner_width),
                    chars.vertical
                );
                lines.push(Element::styled_text(&empty_line, border_style));
            }

            for line in body.lines() {
                let line_width = unicode_width::UnicodeWidthStr::width(line);
                let content = format!(
                    "{}{}{}",
                    " ".repeat(props.padding),
                    line,
                    " ".repeat(inner_width.saturating_sub(line_width + props.padding))
                );
                let line_segments = vec![
                    Element::styled_text(chars.vertical.to_string(), border_style),
                    Element::text(&content),
                    Element::styled_text(chars.vertical.to_string(), border_style),
                ];
                lines.push(Element::Fragment(line_segments));
            }

            // Empty line for padding
            if props.padding > 0 {
                let empty_line = format!(
                    "{}{}{}",
                    chars.vertical,
                    " ".repeat(inner_width),
                    chars.vertical
                );
                lines.push(Element::styled_text(&empty_line, border_style));
            }
        }

        // Buttons
        if !props.buttons.is_empty() {
            // Separator before buttons
            let sep_line = format!(
                "{}{}{}",
                chars.vertical,
                chars.horizontal.to_string().repeat(inner_width),
                chars.vertical
            );
            lines.push(Element::styled_text(&sep_line, border_style));

            // Button line
            let mut button_segments: Vec<Element> = vec![Element::styled_text(
                chars.vertical.to_string(),
                border_style,
            )];

            // Calculate button string
            let mut button_parts: Vec<Element> = Vec::new();
            for (i, btn) in props.buttons.iter().enumerate() {
                if i > 0 {
                    button_parts.push(Element::text("  "));
                }
                let btn_text = format!("[ {} ]", btn.label);
                let btn_style = if btn.primary {
                    btn.color
                        .map(|c| Style::new().fg(c).add_modifier(Modifier::BOLD))
                        .unwrap_or_else(|| Style::new().add_modifier(Modifier::BOLD))
                } else {
                    btn.color.map(|c| Style::new().fg(c)).unwrap_or_default()
                };
                button_parts.push(Element::styled_text(&btn_text, btn_style));
            }

            // Center buttons
            let buttons_total_width: usize = props
                .buttons
                .iter()
                .map(|b| unicode_width::UnicodeWidthStr::width(b.label.as_str()) + 4)
                .sum::<usize>()
                + props.buttons.len().saturating_sub(1) * 2;
            let padding_total = inner_width.saturating_sub(buttons_total_width);
            let left_pad = padding_total / 2;
            let right_pad = padding_total - left_pad;

            button_segments.push(Element::text(" ".repeat(left_pad)));
            button_segments.extend(button_parts);
            button_segments.push(Element::text(" ".repeat(right_pad)));
            button_segments.push(Element::styled_text(
                chars.vertical.to_string(),
                border_style,
            ));
            lines.push(Element::Fragment(button_segments));
        }

        // Bottom border
        let bottom_line = format!(
            "{}{}{}",
            chars.bottom_left,
            chars.horizontal.to_string().repeat(inner_width),
            chars.bottom_right
        );
        lines.push(Element::styled_text(&bottom_line, border_style));

        Element::Fragment(lines)
    }
}

/// Create a simple alert modal.
pub fn alert(title: &str, message: &str) -> Element {
    Element::node::<Modal>(
        ModalProps::new(title)
            .body(message)
            .style(ModalStyle::Info)
            .button(ModalButton::ok()),
        vec![],
    )
}

/// Create a confirm modal with Yes/No buttons.
pub fn confirm_modal(title: &str, message: &str) -> Element {
    Element::node::<Modal>(
        ModalProps::new(title)
            .body(message)
            .style(ModalStyle::Warning)
            .buttons(vec![ModalButton::no(), ModalButton::yes()]),
        vec![],
    )
}

/// Create an error modal.
pub fn error_modal(title: &str, message: &str) -> Element {
    Element::node::<Modal>(
        ModalProps::new(title)
            .body(message)
            .style(ModalStyle::Error)
            .button(ModalButton::close()),
        vec![],
    )
}

/// Create a success modal.
pub fn success_modal(title: &str, message: &str) -> Element {
    Element::node::<Modal>(
        ModalProps::new(title)
            .body(message)
            .style(ModalStyle::Success)
            .button(ModalButton::ok()),
        vec![],
    )
}

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

    #[test]
    fn test_modal_style_colors() {
        assert!(ModalStyle::Default.title_color().is_none());
        assert_eq!(ModalStyle::Info.title_color(), Some(Color::Cyan));
        assert_eq!(ModalStyle::Success.title_color(), Some(Color::Green));
        assert_eq!(ModalStyle::Warning.title_color(), Some(Color::Yellow));
        assert_eq!(ModalStyle::Error.title_color(), Some(Color::Red));
    }

    #[test]
    fn test_modal_style_icons() {
        assert!(ModalStyle::Default.icon().is_none());
        assert_eq!(ModalStyle::Info.icon(), Some(""));
        assert_eq!(ModalStyle::Success.icon(), Some(""));
        assert_eq!(ModalStyle::Warning.icon(), Some(""));
        assert_eq!(ModalStyle::Error.icon(), Some(""));
    }

    #[test]
    fn test_modal_button_new() {
        let btn = ModalButton::new("Test");
        assert_eq!(btn.label, "Test");
        assert!(!btn.primary);
        assert!(btn.color.is_none());
    }

    #[test]
    fn test_modal_button_presets() {
        let ok = ModalButton::ok();
        assert_eq!(ok.label, "OK");
        assert!(ok.primary);
        assert_eq!(ok.color, Some(Color::Green));

        let cancel = ModalButton::cancel();
        assert_eq!(cancel.label, "Cancel");
        assert!(!cancel.primary);
    }

    #[test]
    fn test_modal_props_new() {
        let props = ModalProps::new("Test Title");
        assert_eq!(props.title, "Test Title");
        assert!(props.body.is_none());
        assert_eq!(props.style, ModalStyle::Default);
    }

    #[test]
    fn test_modal_props_builder() {
        let props = ModalProps::new("Title")
            .body("Body text")
            .style(ModalStyle::Warning)
            .buttons(vec![ModalButton::cancel(), ModalButton::ok()])
            .min_width(40)
            .max_width(80);

        assert_eq!(props.title, "Title");
        assert_eq!(props.body, Some("Body text".to_string()));
        assert_eq!(props.style, ModalStyle::Warning);
        assert_eq!(props.buttons.len(), 2);
        assert_eq!(props.min_width, 40);
        assert_eq!(props.max_width, 80);
    }

    #[test]
    fn test_modal_render() {
        let props = ModalProps::new("Test").body("Test body");
        let elem = Modal::render(&props);
        assert!(elem.is_fragment());
    }

    #[test]
    fn test_alert_helper() {
        let elem = alert("Alert", "This is an alert");
        assert!(elem.is_node());
    }

    #[test]
    fn test_confirm_modal_helper() {
        let elem = confirm_modal("Confirm", "Are you sure?");
        assert!(elem.is_node());
    }

    #[test]
    fn test_error_modal_helper() {
        let elem = error_modal("Error", "Something went wrong");
        assert!(elem.is_node());
    }

    #[test]
    fn test_success_modal_helper() {
        let elem = success_modal("Success", "Operation completed");
        assert!(elem.is_node());
    }
}