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
//! Breadcrumbs component - Navigation path display.
//!
//! The Breadcrumbs component displays a hierarchical navigation path,
//! commonly used to show the current location in a file system or menu.
//!
//! ## When to use Breadcrumbs
//!
//! - File path display (Home > Documents > File.txt)
//! - Nested menu location
//! - Multi-step wizard showing current step
//!
//! ## See also
//!
//! - [`Tabs`](super::Tabs) — Horizontal navigation (not hierarchical)
//! - [`TreeView`](super::TreeView) — Full tree with expand/collapse

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

/// A single item in the breadcrumb path.
#[derive(Debug, Clone)]
pub struct Crumb {
    /// The display label for this crumb.
    pub label: String,
    /// Whether this crumb is active/current.
    pub active: bool,
}

impl Crumb {
    /// Create a new breadcrumb item.
    pub fn new(label: impl Into<String>) -> Self {
        Self {
            label: label.into(),
            active: false,
        }
    }

    /// Create an active (current) breadcrumb item.
    pub fn active(label: impl Into<String>) -> Self {
        Self {
            label: label.into(),
            active: true,
        }
    }

    /// Set whether this crumb is active.
    #[must_use]
    pub fn set_active(mut self, active: bool) -> Self {
        self.active = active;
        self
    }
}

impl<S: Into<String>> From<S> for Crumb {
    fn from(s: S) -> Self {
        Crumb::new(s)
    }
}

/// Separator styles for breadcrumbs.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum BreadcrumbSeparator {
    /// Slash: /
    #[default]
    Slash,
    /// Backslash: \
    Backslash,
    /// Arrow: >
    Arrow,
    /// Double arrow: >>
    DoubleArrow,
    /// Chevron: ›
    Chevron,
    /// Double chevron: »
    DoubleChevron,
    /// Bullet: •
    Bullet,
    /// Pipe: |
    Pipe,
    /// Custom separator string
    Custom(&'static str),
}

impl BreadcrumbSeparator {
    /// Get the separator string.
    pub fn as_str(&self) -> &str {
        match self {
            BreadcrumbSeparator::Slash => " / ",
            BreadcrumbSeparator::Backslash => " \\ ",
            BreadcrumbSeparator::Arrow => " > ",
            BreadcrumbSeparator::DoubleArrow => " >> ",
            BreadcrumbSeparator::Chevron => "",
            BreadcrumbSeparator::DoubleChevron => " » ",
            BreadcrumbSeparator::Bullet => "",
            BreadcrumbSeparator::Pipe => " | ",
            BreadcrumbSeparator::Custom(s) => s,
        }
    }
}

/// Properties for the Breadcrumbs component.
#[derive(Debug, Clone)]
pub struct BreadcrumbsProps {
    /// The breadcrumb items.
    pub crumbs: Vec<Crumb>,
    /// Separator between items.
    pub separator: BreadcrumbSeparator,
    /// Color for inactive items.
    pub inactive_color: Option<Color>,
    /// Color for the active/current item.
    pub active_color: Option<Color>,
    /// Color for the separator.
    pub separator_color: Option<Color>,
    /// Whether to bold the active item.
    pub bold_active: bool,
    /// Whether to dim inactive items.
    pub dim_inactive: bool,
    /// Maximum number of crumbs to show (0 = all).
    pub max_items: usize,
    /// Text shown when items are collapsed.
    pub ellipsis: String,
    /// Whether to show a root indicator at the start.
    pub show_root: bool,
    /// Root indicator text.
    pub root_text: String,
}

impl Default for BreadcrumbsProps {
    fn default() -> Self {
        Self {
            crumbs: Vec::new(),
            separator: BreadcrumbSeparator::Slash,
            inactive_color: Some(Color::DarkGray),
            active_color: None,
            separator_color: Some(Color::DarkGray),
            bold_active: true,
            dim_inactive: false,
            max_items: 0,
            ellipsis: "...".into(),
            show_root: false,
            root_text: "~".into(),
        }
    }
}

impl BreadcrumbsProps {
    /// Create new BreadcrumbsProps with items.
    pub fn new<I, T>(crumbs: I) -> Self
    where
        I: IntoIterator<Item = T>,
        T: Into<Crumb>,
    {
        let mut crumbs: Vec<Crumb> = crumbs.into_iter().map(Into::into).collect();
        // Mark the last item as active by default
        if let Some(last) = crumbs.last_mut() {
            last.active = true;
        }
        Self {
            crumbs,
            ..Default::default()
        }
    }

    /// Create from a path string (splits on /).
    pub fn from_path(path: &str) -> Self {
        let parts: Vec<&str> = path.split('/').filter(|s| !s.is_empty()).collect();
        Self::new(parts)
    }

    /// Add a crumb.
    #[must_use]
    pub fn crumb(mut self, label: impl Into<String>) -> Self {
        // Clear active from previous last item
        if let Some(last) = self.crumbs.last_mut() {
            last.active = false;
        }
        // Add new item as active
        self.crumbs.push(Crumb::active(label));
        self
    }

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

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

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

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

    /// Enable/disable bold for active item.
    #[must_use]
    pub fn bold_active(mut self, bold: bool) -> Self {
        self.bold_active = bold;
        self
    }

    /// Enable/disable dim for inactive items.
    #[must_use]
    pub fn dim_inactive(mut self, dim: bool) -> Self {
        self.dim_inactive = dim;
        self
    }

    /// Set maximum items to display (0 = all).
    #[must_use]
    pub fn max_items(mut self, max: usize) -> Self {
        self.max_items = max;
        self
    }

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

    /// Enable/disable root indicator.
    #[must_use]
    pub fn show_root(mut self, show: bool) -> Self {
        self.show_root = show;
        self
    }

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

    /// Get the items to display (handles truncation).
    fn display_items(&self) -> Vec<&Crumb> {
        if self.max_items == 0 || self.crumbs.len() <= self.max_items {
            self.crumbs.iter().collect()
        } else {
            // Keep first item and last (max_items - 1) items
            let mut items = Vec::with_capacity(self.max_items + 1);
            if !self.crumbs.is_empty() {
                items.push(&self.crumbs[0]);
            }
            // Skip items in the middle, take last (max_items - 1)
            let skip = self.crumbs.len() - (self.max_items - 1);
            for crumb in self.crumbs.iter().skip(skip) {
                items.push(crumb);
            }
            items
        }
    }

    /// Check if truncation is needed.
    fn is_truncated(&self) -> bool {
        self.max_items > 0 && self.crumbs.len() > self.max_items
    }

    /// Render the breadcrumbs as a string.
    pub fn render_string(&self) -> String {
        if self.crumbs.is_empty() {
            if self.show_root {
                return self.root_text.clone();
            }
            return String::new();
        }

        let sep = self.separator.as_str();
        let items = self.display_items();
        let truncated = self.is_truncated();

        let mut parts = Vec::new();

        if self.show_root {
            parts.push(self.root_text.clone());
        }

        for (i, crumb) in items.iter().enumerate() {
            // Add ellipsis after first item if truncated
            if truncated && i == 1 {
                parts.push(self.ellipsis.clone());
            }
            parts.push(crumb.label.clone());
        }

        parts.join(sep)
    }
}

/// A component that displays a breadcrumb navigation path.
///
/// # Examples
///
/// ```ignore
/// // From path string
/// Element::node::<Breadcrumbs>(
///     BreadcrumbsProps::from_path("/home/user/documents/file.txt"),
///     vec![]
/// )
///
/// // With builder
/// Element::node::<Breadcrumbs>(
///     BreadcrumbsProps::new(["Home", "Projects", "Blaeck"])
///         .separator(BreadcrumbSeparator::Chevron)
///         .active_color(Color::Cyan),
///     vec![]
/// )
/// ```
pub struct Breadcrumbs;

impl Component for Breadcrumbs {
    type Props = BreadcrumbsProps;

    fn render(props: &Self::Props) -> Element {
        if props.crumbs.is_empty() {
            if props.show_root {
                let mut style = Style::new();
                if props.dim_inactive {
                    style = style.add_modifier(Modifier::DIM);
                }
                if let Some(color) = props.inactive_color {
                    style = style.fg(color);
                }
                return Element::styled_text(&props.root_text, style);
            }
            return Element::text("");
        }

        let sep = props.separator.as_str();
        let items = props.display_items();
        let truncated = props.is_truncated();

        let mut children = Vec::new();

        // Root indicator
        if props.show_root {
            let mut style = Style::new();
            if props.dim_inactive {
                style = style.add_modifier(Modifier::DIM);
            }
            if let Some(color) = props.inactive_color {
                style = style.fg(color);
            }
            children.push(Element::styled_text(&props.root_text, style));

            // Separator after root
            let mut sep_style = Style::new();
            if let Some(color) = props.separator_color {
                sep_style = sep_style.fg(color);
            }
            children.push(Element::styled_text(sep, sep_style));
        }

        for (i, crumb) in items.iter().enumerate() {
            // Add separator before item (except first)
            if i > 0 {
                let mut sep_style = Style::new();
                if let Some(color) = props.separator_color {
                    sep_style = sep_style.fg(color);
                }
                children.push(Element::styled_text(sep, sep_style));

                // Add ellipsis after first separator if truncated
                if truncated && i == 1 {
                    children.push(Element::styled_text(&props.ellipsis, sep_style));
                    children.push(Element::styled_text(sep, sep_style));
                }
            }

            // Add crumb
            let mut style = Style::new();
            if crumb.active {
                if props.bold_active {
                    style = style.add_modifier(Modifier::BOLD);
                }
                if let Some(color) = props.active_color {
                    style = style.fg(color);
                }
            } else {
                if props.dim_inactive {
                    style = style.add_modifier(Modifier::DIM);
                }
                if let Some(color) = props.inactive_color {
                    style = style.fg(color);
                }
            }
            children.push(Element::styled_text(&crumb.label, style));
        }

        // Return as Fragment for proper inline rendering
        Element::Fragment(children)
    }
}

/// Helper function to create breadcrumbs from a path.
pub fn breadcrumbs_path(path: &str) -> String {
    BreadcrumbsProps::from_path(path).render_string()
}

/// Helper function to create breadcrumbs from items.
pub fn breadcrumbs<I, T>(items: I) -> String
where
    I: IntoIterator<Item = T>,
    T: Into<Crumb>,
{
    BreadcrumbsProps::new(items).render_string()
}

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

    #[test]
    fn test_crumb_new() {
        let crumb = Crumb::new("Home");
        assert_eq!(crumb.label, "Home");
        assert!(!crumb.active);
    }

    #[test]
    fn test_crumb_active() {
        let crumb = Crumb::active("Current");
        assert_eq!(crumb.label, "Current");
        assert!(crumb.active);
    }

    #[test]
    fn test_crumb_from_string() {
        let crumb: Crumb = "Test".into();
        assert_eq!(crumb.label, "Test");
    }

    #[test]
    fn test_separator_slash() {
        assert_eq!(BreadcrumbSeparator::Slash.as_str(), " / ");
    }

    #[test]
    fn test_separator_chevron() {
        assert_eq!(BreadcrumbSeparator::Chevron.as_str(), "");
    }

    #[test]
    fn test_separator_custom() {
        let sep = BreadcrumbSeparator::Custom(" -> ");
        assert_eq!(sep.as_str(), " -> ");
    }

    #[test]
    fn test_breadcrumbs_props_new() {
        let props = BreadcrumbsProps::new(["Home", "Projects", "Blaeck"]);
        assert_eq!(props.crumbs.len(), 3);
        // Last item should be active
        assert!(props.crumbs[2].active);
        assert!(!props.crumbs[0].active);
    }

    #[test]
    fn test_breadcrumbs_props_from_path() {
        let props = BreadcrumbsProps::from_path("/home/user/docs");
        assert_eq!(props.crumbs.len(), 3);
        assert_eq!(props.crumbs[0].label, "home");
        assert_eq!(props.crumbs[1].label, "user");
        assert_eq!(props.crumbs[2].label, "docs");
    }

    #[test]
    fn test_breadcrumbs_props_builder() {
        let props = BreadcrumbsProps::new(Vec::<&str>::new())
            .crumb("Home")
            .crumb("Projects")
            .separator(BreadcrumbSeparator::Chevron)
            .active_color(Color::Cyan);

        assert_eq!(props.crumbs.len(), 2);
        assert_eq!(props.separator, BreadcrumbSeparator::Chevron);
        assert_eq!(props.active_color, Some(Color::Cyan));
    }

    #[test]
    fn test_breadcrumbs_render_string() {
        let props = BreadcrumbsProps::new(["Home", "Projects", "Blaeck"]);
        let result = props.render_string();
        assert_eq!(result, "Home / Projects / Blaeck");
    }

    #[test]
    fn test_breadcrumbs_render_string_chevron() {
        let props = BreadcrumbsProps::new(["A", "B", "C"]).separator(BreadcrumbSeparator::Chevron);
        let result = props.render_string();
        assert_eq!(result, "A › B › C");
    }

    #[test]
    fn test_breadcrumbs_render_empty() {
        let props = BreadcrumbsProps::new(Vec::<&str>::new());
        assert_eq!(props.render_string(), "");
    }

    #[test]
    fn test_breadcrumbs_render_with_root() {
        let props = BreadcrumbsProps::new(["home", "user"]).show_root(true);
        let result = props.render_string();
        assert_eq!(result, "~ / home / user");
    }

    #[test]
    fn test_breadcrumbs_truncation() {
        let props = BreadcrumbsProps::new(["a", "b", "c", "d", "e", "f"]).max_items(3);
        let result = props.render_string();
        // Should show: a / ... / e / f
        assert!(result.contains("a"));
        assert!(result.contains("..."));
        assert!(result.contains("f"));
    }

    #[test]
    fn test_breadcrumbs_helper_path() {
        let result = breadcrumbs_path("/home/user/docs");
        assert!(result.contains("home"));
        assert!(result.contains("docs"));
    }

    #[test]
    fn test_breadcrumbs_helper() {
        let result = breadcrumbs(["Home", "Docs"]);
        assert_eq!(result, "Home / Docs");
    }

    #[test]
    fn test_breadcrumbs_component_render() {
        let props = BreadcrumbsProps::new(["A", "B"]);
        let elem = Breadcrumbs::render(&props);
        assert!(elem.is_fragment());
    }

    #[test]
    fn test_breadcrumbs_component_render_empty() {
        let props = BreadcrumbsProps::new(Vec::<&str>::new());
        let elem = Breadcrumbs::render(&props);
        assert!(elem.is_text());
    }
}