limit-tui 0.0.30

Terminal UI components with Virtual DOM rendering for Rust applications. Built with Ratatui.
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
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
// Flexbox layout system for terminal UI

use crate::vdom::VNode;
use ratatui::layout::Rect;

/// Main axis direction for flex layout
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub enum FlexDirection {
    #[default]
    Row,
    Column,
}

/// Main axis alignment
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub enum JustifyContent {
    #[default]
    Start,
    Center,
    End,
    SpaceBetween,
    SpaceAround,
}

/// Cross axis alignment
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub enum AlignItems {
    #[default]
    Start,
    Center,
    End,
    Stretch,
}

/// Flex layout style properties
#[derive(Debug, Clone, Copy, Default)]
pub struct FlexStyle {
    pub direction: FlexDirection,
    pub justify_content: JustifyContent,
    pub align_items: AlignItems,
    pub gap: u16,
    pub flex_grow: f32,
    pub flex_shrink: f32,
}

impl FlexStyle {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn row(mut self) -> Self {
        self.direction = FlexDirection::Row;
        self
    }

    pub fn column(mut self) -> Self {
        self.direction = FlexDirection::Column;
        self
    }

    pub fn justify(mut self, justify: JustifyContent) -> Self {
        self.justify_content = justify;
        self
    }

    pub fn align(mut self, align: AlignItems) -> Self {
        self.align_items = align;
        self
    }

    pub fn gap(mut self, gap: u16) -> Self {
        self.gap = gap;
        self
    }

    pub fn flex_grow(mut self, grow: f32) -> Self {
        self.flex_grow = grow;
        self
    }

    pub fn flex_shrink(mut self, shrink: f32) -> Self {
        self.flex_shrink = shrink;
        self
    }
}

/// Flexbox layout engine
#[derive(Debug, Clone, Copy, Default)]
pub struct FlexboxLayout;

impl FlexboxLayout {
    pub fn new() -> Self {
        Self
    }

    /// Calculate child positions for a flex container
    pub fn calculate(node: &VNode, constraints: Rect) -> Vec<Rect> {
        let children = match node.children() {
            Some(children) if !children.is_empty() => children,
            _ => return vec![],
        };

        // Extract flex style from node attributes
        let style = Self::extract_style(node);

        // Get base sizes for children
        let base_sizes: Vec<Rect> = children
            .iter()
            .map(|_| Rect {
                x: 0,
                y: 0,
                width: 1,  // Default minimum width
                height: 1, // Default minimum height
            })
            .collect();

        Self::layout_children(&base_sizes, constraints, &style, children.len())
    }

    /// Extract flex style from node attributes
    fn extract_style(node: &VNode) -> FlexStyle {
        let attrs = node.attrs().unwrap();

        FlexStyle {
            direction: attrs
                .get("direction")
                .and_then(|v| match v.as_str() {
                    "row" => Some(FlexDirection::Row),
                    "column" => Some(FlexDirection::Column),
                    _ => None,
                })
                .unwrap_or(FlexDirection::Row),

            justify_content: attrs
                .get("justify")
                .and_then(|v| match v.as_str() {
                    "start" => Some(JustifyContent::Start),
                    "center" => Some(JustifyContent::Center),
                    "end" => Some(JustifyContent::End),
                    "space-between" => Some(JustifyContent::SpaceBetween),
                    "space-around" => Some(JustifyContent::SpaceAround),
                    _ => None,
                })
                .unwrap_or(JustifyContent::Start),

            align_items: attrs
                .get("align")
                .and_then(|v| match v.as_str() {
                    "start" => Some(AlignItems::Start),
                    "center" => Some(AlignItems::Center),
                    "end" => Some(AlignItems::End),
                    "stretch" => Some(AlignItems::Stretch),
                    _ => None,
                })
                .unwrap_or(AlignItems::Start),

            gap: attrs.get("gap").and_then(|v| v.parse().ok()).unwrap_or(0),

            flex_grow: attrs
                .get("flex-grow")
                .and_then(|v| v.parse().ok())
                .unwrap_or(0.0),

            flex_shrink: attrs
                .get("flex-shrink")
                .and_then(|v| v.parse().ok())
                .unwrap_or(1.0),
        }
    }

    /// Layout children based on flex properties
    fn layout_children(
        _base_sizes: &[Rect],
        constraints: Rect,
        style: &FlexStyle,
        child_count: usize,
    ) -> Vec<Rect> {
        if child_count == 0 {
            return vec![];
        }

        let main_axis_size = match style.direction {
            FlexDirection::Row => constraints.width,
            FlexDirection::Column => constraints.height,
        };

        let cross_axis_size = match style.direction {
            FlexDirection::Row => constraints.height,
            FlexDirection::Column => constraints.width,
        };

        // Calculate total gap space
        let total_gap = style
            .gap
            .saturating_mul(child_count.saturating_sub(1) as u16);
        let available_space = main_axis_size.saturating_sub(total_gap);

        // Distribute main axis space
        let main_positions =
            Self::distribute_main_axis(available_space, style, child_count, main_axis_size);

        // Calculate cross axis positions
        let cross_positions =
            Self::distribute_cross_axis(cross_axis_size, &style.align_items, child_count);

        // Build final rects
        let mut results = Vec::with_capacity(child_count);

        for i in 0..child_count {
            let child_width = match style.direction {
                FlexDirection::Row => main_positions[i].size,
                FlexDirection::Column => cross_axis_size,
            };

            let child_height = match style.direction {
                FlexDirection::Row => cross_axis_size,
                FlexDirection::Column => main_positions[i].size,
            };

            let x = match style.direction {
                FlexDirection::Row => constraints.x + main_positions[i].pos,
                FlexDirection::Column => constraints.x + cross_positions[i].pos,
            };

            let y = match style.direction {
                FlexDirection::Row => constraints.y + cross_positions[i].pos,
                FlexDirection::Column => constraints.y + main_positions[i].pos,
            };

            results.push(Rect {
                x,
                y,
                width: child_width,
                height: child_height,
            });
        }

        results
    }

    /// Distribute space along main axis
    fn distribute_main_axis(
        available_space: u16,
        style: &FlexStyle,
        child_count: usize,
        _container_size: u16,
    ) -> Vec<PositionInfo> {
        let mut positions = Vec::with_capacity(child_count);

        if child_count == 0 {
            return positions;
        }

        // Calculate gap space
        let gap_space = style
            .gap
            .saturating_mul(child_count.saturating_sub(1) as u16);
        let usable_space = available_space.saturating_sub(gap_space);

        // Distribute space based on flex_grow
        let total_flex_grow = if style.flex_grow > 0.0 {
            style.flex_grow * child_count as f32
        } else {
            child_count as f32 // Default to equal distribution
        };

        // Calculate base size for each child
        let base_size = if total_flex_grow > 0.0 {
            (usable_space as f32 / total_flex_grow) as u16
        } else {
            0
        };

        // Calculate positions based on justify_content
        let total_used = base_size
            .saturating_mul(child_count as u16)
            .saturating_add(gap_space);

        let mut current_pos = match style.justify_content {
            JustifyContent::Start => 0,
            JustifyContent::Center => available_space.saturating_sub(total_used) / 2,
            JustifyContent::End => available_space.saturating_sub(total_used),
            JustifyContent::SpaceBetween => {
                if child_count > 1 {
                    let _extra_gap = (usable_space
                        .saturating_sub(base_size.saturating_mul(child_count as u16)))
                    .saturating_div(child_count.saturating_sub(1) as u16);
                    0
                } else {
                    0
                }
            }
            JustifyContent::SpaceAround => {
                let extra_gap = (usable_space
                    .saturating_sub(base_size.saturating_mul(child_count as u16)))
                .saturating_div(child_count as u16);
                extra_gap / 2
            }
        };

        let mut gap = style.gap;

        if style.justify_content == JustifyContent::SpaceBetween && child_count > 1 {
            let extra = usable_space.saturating_sub(base_size.saturating_mul(child_count as u16));
            gap = gap.saturating_add(extra / (child_count.saturating_sub(1) as u16));
        }

        if style.justify_content == JustifyContent::SpaceAround {
            let extra = usable_space.saturating_sub(base_size.saturating_mul(child_count as u16));
            let extra_gap = extra / (child_count as u16);
            gap = extra_gap;
        }

        for _i in 0..child_count {
            positions.push(PositionInfo {
                pos: current_pos,
                size: base_size,
            });

            current_pos = current_pos.saturating_add(base_size).saturating_add(gap);
        }

        positions
    }

    /// Distribute positions along cross axis
    fn distribute_cross_axis(
        cross_axis_size: u16,
        style: &AlignItems,
        child_count: usize,
    ) -> Vec<PositionInfo> {
        let mut positions = Vec::with_capacity(child_count);

        if child_count == 0 {
            return positions;
        }

        // Default to full cross axis size for each child
        let child_size = cross_axis_size;

        for _i in 0..child_count {
            let pos = match style {
                AlignItems::Start => 0,
                AlignItems::Center => 0, // Centered by taking full size
                AlignItems::End => 0,    // Aligned by taking full size
                AlignItems::Stretch => 0,
            };

            positions.push(PositionInfo {
                pos,
                size: child_size,
            });
        }

        positions
    }
}

/// Helper struct for position calculations
#[derive(Debug, Clone, Copy)]
struct PositionInfo {
    pos: u16,
    size: u16,
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::vdom::VNode;
    use std::collections::HashMap;

    fn create_flex_container(attrs: HashMap<String, String>, children: Vec<VNode>) -> VNode {
        VNode::Element {
            tag: "flex".to_string(),
            attrs,
            children,
        }
    }

    fn create_text_node(text: &str) -> VNode {
        VNode::Text(text.to_string())
    }

    #[test]
    fn test_layout_simple_row() {
        let children = vec![
            create_text_node("A"),
            create_text_node("B"),
            create_text_node("C"),
        ];

        let container = create_flex_container(HashMap::new(), children);

        let constraints = Rect {
            x: 0,
            y: 0,
            width: 20,
            height: 10,
        };

        let results = FlexboxLayout::calculate(&container, constraints);

        assert_eq!(results.len(), 3);

        // Verify horizontal layout (x positions increase)
        assert!(results[0].x < results[1].x);
        assert!(results[1].x < results[2].x);

        // All children have same height (full container height)
        assert_eq!(results[0].height, 10);
        assert_eq!(results[1].height, 10);
        assert_eq!(results[2].height, 10);

        // All have same y position
        assert_eq!(results[0].y, 0);
        assert_eq!(results[1].y, 0);
        assert_eq!(results[2].y, 0);
    }

    #[test]
    fn test_layout_simple_column() {
        let children = vec![
            create_text_node("A"),
            create_text_node("B"),
            create_text_node("C"),
        ];

        let mut attrs = HashMap::new();
        attrs.insert("direction".to_string(), "column".to_string());

        let container = create_flex_container(attrs, children);

        let constraints = Rect {
            x: 0,
            y: 0,
            width: 10,
            height: 20,
        };

        let results = FlexboxLayout::calculate(&container, constraints);

        assert_eq!(results.len(), 3);

        // Verify vertical layout (y positions increase)
        assert!(results[0].y < results[1].y);
        assert!(results[1].y < results[2].y);

        // All children have same width (full container width)
        assert_eq!(results[0].width, 10);
        assert_eq!(results[1].width, 10);
        assert_eq!(results[2].width, 10);

        // All have same x position
        assert_eq!(results[0].x, 0);
        assert_eq!(results[1].x, 0);
        assert_eq!(results[2].x, 0);
    }

    #[test]
    fn test_layout_justify_center() {
        let children = vec![
            create_text_node("A"),
            create_text_node("B"),
            create_text_node("C"),
        ];

        let mut attrs = HashMap::new();
        attrs.insert("justify".to_string(), "center".to_string());

        let container = create_flex_container(attrs, children);

        let constraints = Rect {
            x: 0,
            y: 0,
            width: 20,
            height: 10,
        };

        let results = FlexboxLayout::calculate(&container, constraints);

        assert_eq!(results.len(), 3);

        // First child should start at center position (offset > 0)
        let total_width = results[2].x + results[2].width;
        assert!(total_width <= 20);
        // With centering, first child should have some positive offset
        assert!(results[0].x > 0 || total_width == 20);
    }

    #[test]
    fn test_layout_flex_grow() {
        let children = vec![
            create_text_node("A"),
            create_text_node("B"),
            create_text_node("C"),
        ];

        let mut attrs = HashMap::new();
        attrs.insert("flex-grow".to_string(), "1.0".to_string());

        let container = create_flex_container(attrs, children);

        let constraints = Rect {
            x: 0,
            y: 0,
            width: 20,
            height: 10,
        };
        let results = FlexboxLayout::calculate(&container, constraints);

        assert_eq!(results.len(), 3);

        // With flex-grow=1.0, children should have non-zero width
        assert!(results[0].width > 0);
        assert!(results[1].width > 0);
        assert!(results[2].width > 0);
    }

    #[test]
    fn test_layout_gap() {
        let children = vec![
            create_text_node("A"),
            create_text_node("B"),
            create_text_node("C"),
        ];

        let mut attrs = HashMap::new();
        attrs.insert("gap".to_string(), "2".to_string());

        let container = create_flex_container(attrs, children);

        let constraints = Rect {
            x: 0,
            y: 0,
            width: 20,
            height: 10,
        };

        let results = FlexboxLayout::calculate(&container, constraints);

        assert_eq!(results.len(), 3);

        // Gap should affect positioning
        // Distance between children should account for gap
        let gap_1 = results[1].x - (results[0].x + results[0].width);
        let gap_2 = results[2].x - (results[1].x + results[1].width);

        // Gap should be present (though exact value depends on calculation)
        assert!(gap_1 >= 2 || gap_2 >= 2);
    }

    #[test]
    fn test_layout_nested() {
        // Create inner flex container
        let inner_children = vec![create_text_node("A"), create_text_node("B")];

        let mut inner_attrs = HashMap::new();
        inner_attrs.insert("direction".to_string(), "row".to_string());

        let inner_container = create_flex_container(inner_attrs, inner_children);

        // Create outer flex container
        let outer_children = vec![
            create_text_node("X"),
            inner_container,
            create_text_node("Y"),
        ];

        let mut outer_attrs = HashMap::new();
        outer_attrs.insert("direction".to_string(), "column".to_string());

        let outer_container = create_flex_container(outer_attrs, outer_children);

        let constraints = Rect {
            x: 0,
            y: 0,
            width: 20,
            height: 30,
        };

        let results = FlexboxLayout::calculate(&outer_container, constraints);

        // Outer container has 3 children
        assert_eq!(results.len(), 3);

        // Verify vertical layout (y positions increase)
        assert!(results[0].y < results[1].y);
        assert!(results[1].y < results[2].y);

        // All children have full width
        assert_eq!(results[0].width, 20);
        assert_eq!(results[1].width, 20);
        assert_eq!(results[2].width, 20);
    }

    #[test]
    fn test_flex_style_builder() {
        let style = FlexStyle::new()
            .row()
            .justify(JustifyContent::Center)
            .align(AlignItems::Stretch)
            .gap(2)
            .flex_grow(1.0)
            .flex_shrink(0.5);

        assert_eq!(style.direction, FlexDirection::Row);
        assert_eq!(style.justify_content, JustifyContent::Center);
        assert_eq!(style.align_items, AlignItems::Stretch);
        assert_eq!(style.gap, 2);
        assert_eq!(style.flex_grow, 1.0);
        assert_eq!(style.flex_shrink, 0.5);
    }

    #[test]
    fn test_empty_children() {
        let container = create_flex_container(HashMap::new(), vec![]);

        let constraints = Rect {
            x: 0,
            y: 0,
            width: 20,
            height: 10,
        };

        let results = FlexboxLayout::calculate(&container, constraints);

        assert_eq!(results.len(), 0);
    }

    #[test]
    fn test_single_child() {
        let children = vec![create_text_node("A")];

        let container = create_flex_container(HashMap::new(), children);

        let constraints = Rect {
            x: 0,
            y: 0,
            width: 20,
            height: 10,
        };

        let results = FlexboxLayout::calculate(&container, constraints);

        assert_eq!(results.len(), 1);
        assert_eq!(results[0].x, 0);
        assert_eq!(results[0].y, 0);
        assert_eq!(results[0].height, 10);
    }

    #[test]
    fn test_justify_end() {
        let children = vec![create_text_node("A"), create_text_node("B")];

        let mut attrs = HashMap::new();
        attrs.insert("justify".to_string(), "end".to_string());

        let container = create_flex_container(attrs, children);

        let constraints = Rect {
            x: 0,
            y: 0,
            width: 20,
            height: 10,
        };

        let results = FlexboxLayout::calculate(&container, constraints);

        assert_eq!(results.len(), 2);

        // Last child should be at or near the end
        let total_width = results[1].x + results[1].width;
        assert!(total_width <= 20);
    }

    #[test]
    fn test_space_between() {
        let children = vec![
            create_text_node("A"),
            create_text_node("B"),
            create_text_node("C"),
        ];

        let mut attrs = HashMap::new();
        attrs.insert("justify".to_string(), "space-between".to_string());

        let container = create_flex_container(attrs, children);

        let constraints = Rect {
            x: 0,
            y: 0,
            width: 20,
            height: 10,
        };

        let results = FlexboxLayout::calculate(&container, constraints);

        assert_eq!(results.len(), 3);

        // First child at start
        assert_eq!(results[0].x, 0);

        // Last child at end
        let last_end = results[2].x + results[2].width;
        assert!(last_end <= 20);
    }

    #[test]
    fn test_space_around() {
        let children = vec![
            create_text_node("A"),
            create_text_node("B"),
            create_text_node("C"),
        ];

        let mut attrs = HashMap::new();
        attrs.insert("justify".to_string(), "space-around".to_string());

        let container = create_flex_container(attrs, children);

        let constraints = Rect {
            x: 0,
            y: 0,
            width: 20,
            height: 10,
        };

        let results = FlexboxLayout::calculate(&container, constraints);

        assert_eq!(results.len(), 3);

        // First child should exist and have valid position
        assert!(!results.is_empty());
        assert!(results[0].width > 0);
    }
}