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
//! Component and Element types for building declarative terminal UIs.
//!
//! This module defines the core abstractions:
//!
//! - **`Element`** — A node in the UI tree. Can be `Text`, `Node` (component), `Fragment`, or `Empty`.
//! - **`Component`** — A trait for defining UI pieces. Has a `Props` type and `render()` method.
//!
//! Components are type-erased via `TypeId` + `Box<dyn Any>` so the element tree can hold
//! any component type. Props are downcast at render time.
//!
//! Pattern from Iocraft. See `ARCHITECTURE.md` for why type erasure is used.

use crate::layout::LayoutStyle;
use crate::style::Style;
use std::any::{Any, TypeId};

/// A component that can be rendered.
///
/// Components are the building blocks of a Blaeck UI. Each component
/// defines how to render itself given a set of properties.
pub trait Component: 'static {
    /// The type of properties this component accepts.
    type Props: Default + 'static;

    /// Render this component with the given props, returning an Element tree.
    fn render(props: &Self::Props) -> Element;
}

/// An element in the UI tree.
///
/// Elements are lightweight descriptions of what to render.
/// They can be text, styled text, component nodes, or fragments.
#[derive(Default)]
pub enum Element {
    /// An empty element (renders nothing)
    #[default]
    Empty,
    /// A text element with optional styling
    Text {
        /// The text content
        content: String,
        /// The style to apply
        style: Style,
    },
    /// A component node with props and children
    Node {
        /// The TypeId of the component
        type_id: TypeId,
        /// The props as a boxed Any
        props: Box<dyn Any>,
        /// The layout style for this node
        layout_style: Box<LayoutStyle>,
        /// Child elements
        children: Vec<Element>,
        /// Render function for this component
        render_fn: fn(&dyn Any) -> Element,
    },
    /// A fragment containing multiple elements (no wrapping container)
    Fragment(Vec<Element>),
}

impl Element {
    /// Create an empty element.
    pub fn empty() -> Self {
        Element::Empty
    }

    /// Create a text element with default style.
    pub fn text(s: impl Into<String>) -> Self {
        Element::Text {
            content: s.into(),
            style: Style::default(),
        }
    }

    /// Create a text element with a specific style.
    pub fn styled_text(s: impl Into<String>, style: Style) -> Self {
        Element::Text {
            content: s.into(),
            style,
        }
    }

    /// Create a component node.
    ///
    /// ## Why type erasure?
    ///
    /// The element tree needs to hold any component type in a single `Vec<Element>`.
    /// Rust doesn't have heterogeneous collections, so we have three options:
    ///
    /// 1. **Enum with all variants** — Doesn't scale, can't add custom components
    /// 2. **Trait objects (`dyn Component`)** — Loses the `Props` type information
    /// 3. **Type erasure** — Store `TypeId` + `Box<dyn Any>` + a render function
    ///
    /// We use option 3. The `render_fn` closure captures the concrete type `C`
    /// at construction time, so it can downcast `props` back to `C::Props` at
    /// render time. This gives us type safety (wrong props = panic) while
    /// allowing heterogeneous trees.
    ///
    /// Pattern copied from Iocraft.
    pub fn node<C: Component>(props: C::Props, children: Vec<Element>) -> Self {
        Element::Node {
            type_id: TypeId::of::<C>(),
            props: Box::new(props),
            layout_style: Box::new(LayoutStyle::default()),
            children,
            render_fn: |props_any| {
                let props = props_any.downcast_ref::<C::Props>().unwrap();
                C::render(props)
            },
        }
    }

    /// Create a component node with layout style.
    pub fn node_with_layout<C: Component>(
        props: C::Props,
        layout_style: LayoutStyle,
        children: Vec<Element>,
    ) -> Self {
        Element::Node {
            type_id: TypeId::of::<C>(),
            props: Box::new(props),
            layout_style: Box::new(layout_style),
            children,
            render_fn: |props_any| {
                let props = props_any.downcast_ref::<C::Props>().unwrap();
                C::render(props)
            },
        }
    }

    /// Get the type ID if this is a node element.
    pub fn type_id(&self) -> Option<TypeId> {
        match self {
            Element::Node { type_id, .. } => Some(*type_id),
            _ => None,
        }
    }

    /// Check if this element is empty.
    pub fn is_empty(&self) -> bool {
        matches!(self, Element::Empty)
    }

    /// Check if this element is text.
    pub fn is_text(&self) -> bool {
        matches!(self, Element::Text { .. })
    }

    /// Check if this element is a node.
    pub fn is_node(&self) -> bool {
        matches!(self, Element::Node { .. })
    }

    /// Check if this element is a fragment.
    pub fn is_fragment(&self) -> bool {
        matches!(self, Element::Fragment(_))
    }

    /// Create a fragment from multiple elements.
    ///
    /// **Note:** Fragments render children **horizontally** (inline).
    /// For vertical stacking, use [`Element::column`] instead.
    pub fn fragment(elements: Vec<Element>) -> Self {
        Element::Fragment(elements)
    }

    /// Create a vertical column of elements.
    ///
    /// This is the recommended way to render a dynamic list of items vertically.
    /// It creates a `Box` with `flex_direction: Column`.
    ///
    /// # Example
    ///
    /// ```ignore
    /// let items = vec!["Apple", "Banana", "Cherry"];
    /// Element::column(
    ///     items.iter().map(|item| {
    ///         element! { Text(content: item) }
    ///     }).collect()
    /// )
    /// ```
    pub fn column(children: Vec<Element>) -> Self {
        use crate::components::{Box, BoxProps};
        Element::node::<Box>(
            BoxProps {
                flex_direction: crate::layout::FlexDirection::Column,
                ..Default::default()
            },
            children,
        )
    }

    /// Create a horizontal row of elements.
    ///
    /// This creates a `Box` with `flex_direction: Row`.
    ///
    /// # Example
    ///
    /// ```ignore
    /// Element::row(vec![
    ///     element! { Text(content: "Left") },
    ///     element! { Spacer },
    ///     element! { Text(content: "Right") },
    /// ])
    /// ```
    pub fn row(children: Vec<Element>) -> Self {
        use crate::components::{Box, BoxProps};
        Element::node::<Box>(
            BoxProps {
                flex_direction: crate::layout::FlexDirection::Row,
                ..Default::default()
            },
            children,
        )
    }

    /// Get the children of this element (empty for non-nodes).
    pub fn children(&self) -> &[Element] {
        match self {
            Element::Node { children, .. } => children,
            _ => &[],
        }
    }

    /// Get the layout style of this element.
    pub fn layout_style(&self) -> &LayoutStyle {
        use std::sync::LazyLock;
        static DEFAULT_LAYOUT: LazyLock<LayoutStyle> = LazyLock::new(LayoutStyle::default);
        match self {
            Element::Node { layout_style, .. } => layout_style,
            _ => &DEFAULT_LAYOUT,
        }
    }

    /// Invoke the render function for a node element.
    pub fn render_component(&self) -> Option<Element> {
        match self {
            Element::Node {
                props, render_fn, ..
            } => Some(render_fn(props.as_ref())),
            _ => None,
        }
    }
}

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

    // Test component
    struct TestComponent;

    #[derive(Default)]
    struct TestProps {
        value: i32,
    }

    impl Component for TestComponent {
        type Props = TestProps;

        fn render(props: &Self::Props) -> Element {
            Element::text(format!("Value: {}", props.value))
        }
    }

    // Another test component for nested testing
    struct ContainerComponent;

    #[derive(Default)]
    struct ContainerProps {
        label: String,
    }

    impl Component for ContainerComponent {
        type Props = ContainerProps;

        fn render(props: &Self::Props) -> Element {
            Element::text(format!("Container: {}", props.label))
        }
    }

    #[test]
    fn test_element_empty() {
        let elem = Element::empty();
        assert!(elem.is_empty());
        assert!(!elem.is_text());
        assert!(!elem.is_node());
    }

    #[test]
    fn test_element_text() {
        let elem = Element::text("Hello");
        match &elem {
            Element::Text { content, style } => {
                assert_eq!(content, "Hello");
                assert_eq!(*style, Style::default());
            }
            _ => panic!("Expected Text"),
        }
        assert!(elem.is_text());
        assert!(!elem.is_empty());
        assert!(!elem.is_node());
    }

    #[test]
    fn test_element_styled_text() {
        let style = Style::new().fg(Color::Red).bold();
        let elem = Element::styled_text("Styled", style);
        match &elem {
            Element::Text {
                content, style: s, ..
            } => {
                assert_eq!(content, "Styled");
                assert_eq!(s.fg, Color::Red);
            }
            _ => panic!("Expected Text"),
        }
    }

    #[test]
    fn test_element_node() {
        let elem = Element::node::<TestComponent>(TestProps { value: 42 }, vec![]);
        match &elem {
            Element::Node { children, .. } => assert!(children.is_empty()),
            _ => panic!("Expected Node"),
        }
        assert!(elem.is_node());
        assert!(!elem.is_empty());
        assert!(!elem.is_text());
    }

    #[test]
    fn test_element_with_children() {
        let child = Element::text("Child");
        let elem = Element::node::<TestComponent>(TestProps::default(), vec![child]);
        match &elem {
            Element::Node { children, .. } => assert_eq!(children.len(), 1),
            _ => panic!("Expected Node"),
        }
    }

    #[test]
    fn test_element_type_id() {
        let elem = Element::node::<TestComponent>(TestProps::default(), vec![]);
        assert_eq!(elem.type_id(), Some(TypeId::of::<TestComponent>()));

        let text_elem = Element::text("Hello");
        assert_eq!(text_elem.type_id(), None);
    }

    #[test]
    fn test_element_children() {
        let child1 = Element::text("A");
        let child2 = Element::text("B");
        let elem = Element::node::<TestComponent>(TestProps::default(), vec![child1, child2]);

        assert_eq!(elem.children().len(), 2);

        let empty = Element::empty();
        assert!(empty.children().is_empty());
    }

    #[test]
    fn test_element_default() {
        let elem = Element::default();
        assert!(elem.is_empty());
    }

    #[test]
    fn test_component_render() {
        let elem = Element::node::<TestComponent>(TestProps { value: 99 }, vec![]);
        let rendered = elem.render_component().unwrap();

        match rendered {
            Element::Text { content, .. } => {
                assert_eq!(content, "Value: 99");
            }
            _ => panic!("Expected Text from render"),
        }
    }

    #[test]
    fn test_nested_components() {
        let inner = Element::node::<TestComponent>(TestProps { value: 10 }, vec![]);
        let outer = Element::node::<ContainerComponent>(
            ContainerProps {
                label: "Outer".to_string(),
            },
            vec![inner],
        );

        assert!(outer.is_node());
        assert_eq!(outer.children().len(), 1);
        assert!(outer.children()[0].is_node());
    }

    #[test]
    fn test_element_node_with_layout() {
        use crate::layout::FlexDirection;

        let layout = LayoutStyle {
            width: Some(100.0),
            height: Some(50.0),
            flex_direction: FlexDirection::Row,
            ..Default::default()
        };
        let elem = Element::node_with_layout::<TestComponent>(TestProps::default(), layout, vec![]);

        let style = elem.layout_style();
        assert_eq!(style.width, Some(100.0));
        assert_eq!(style.height, Some(50.0));
        assert_eq!(style.flex_direction, FlexDirection::Row);
    }

    #[test]
    fn test_render_component_on_non_node() {
        let text = Element::text("Hello");
        assert!(text.render_component().is_none());

        let empty = Element::empty();
        assert!(empty.render_component().is_none());
    }
}