saorsa-tui 0.4.0

Retained-mode, CSS-styled terminal UI framework
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
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
//! Hierarchical tree widget with expandable/collapsible nodes.
//!
//! Displays a tree of items as a flat list with indentation.
//! Supports keyboard navigation, expand/collapse, and lazy loading.

use crate::buffer::ScreenBuffer;
use crate::cell::Cell;
use crate::event::{Event, KeyCode, KeyEvent};
use crate::geometry::Rect;
use crate::segment::Segment;
use crate::style::Style;
use crate::text::truncate_to_display_width;
use unicode_width::UnicodeWidthStr;

use super::{BorderStyle, EventResult, InteractiveWidget, Widget};

/// A node in the tree hierarchy.
#[derive(Clone, Debug)]
pub struct TreeNode<T> {
    /// The data for this node.
    pub data: T,
    /// Children of this node.
    pub children: Vec<TreeNode<T>>,
    /// Whether this node is expanded (children visible).
    pub expanded: bool,
    /// Whether this node is a leaf (cannot have children).
    pub is_leaf: bool,
}

impl<T> TreeNode<T> {
    /// Create a new tree node with the given data.
    pub fn new(data: T) -> Self {
        Self {
            data,
            children: Vec::new(),
            expanded: false,
            is_leaf: true,
        }
    }

    /// Create a new branch node (not a leaf).
    pub fn branch(data: T) -> Self {
        Self {
            data,
            children: Vec::new(),
            expanded: false,
            is_leaf: false,
        }
    }

    /// Add a child node.
    pub fn with_child(mut self, child: TreeNode<T>) -> Self {
        self.is_leaf = false;
        self.children.push(child);
        self
    }

    /// Add multiple children.
    pub fn with_children(mut self, children: Vec<TreeNode<T>>) -> Self {
        if !children.is_empty() {
            self.is_leaf = false;
        }
        self.children = children;
        self
    }
}

/// A flattened visible node entry.
struct VisibleNode {
    /// Depth in the tree (0 = root).
    depth: usize,
    /// Path of indices to reach this node from roots.
    path: Vec<usize>,
    /// Whether expanded.
    expanded: bool,
    /// Whether leaf.
    is_leaf: bool,
}

/// Type alias for the node render function.
type NodeRenderFn<T> = Box<dyn Fn(&T, usize, bool, bool) -> Vec<Segment>>;

/// Type alias for the lazy load function.
type LazyLoadFn<T> = Option<Box<dyn Fn(&T) -> Vec<TreeNode<T>>>>;

/// A hierarchical tree widget with expandable/collapsible nodes.
///
/// Displays a forest of [`TreeNode`]s as an indented list. Supports
/// keyboard navigation, expand/collapse, and optional lazy loading.
pub struct Tree<T> {
    /// Root nodes (a forest).
    roots: Vec<TreeNode<T>>,
    /// Selected visible node index.
    selected: usize,
    /// Scroll offset (first visible line).
    scroll_offset: usize,
    /// Function to render a node as Segments.
    render_fn: NodeRenderFn<T>,
    /// Style for unselected nodes.
    node_style: Style,
    /// Style for the selected node.
    selected_style: Style,
    /// Border style.
    border: BorderStyle,
    /// Lazy load callback.
    lazy_load_fn: LazyLoadFn<T>,
}

impl<T> Tree<T> {
    /// Create a new tree with the given root nodes.
    pub fn new(roots: Vec<TreeNode<T>>) -> Self {
        Self {
            roots,
            selected: 0,
            scroll_offset: 0,
            render_fn: Box::new(|_, _, _, _| vec![Segment::new("???")]),
            node_style: Style::default(),
            selected_style: Style::default().reverse(true),
            border: BorderStyle::None,
            lazy_load_fn: None,
        }
    }

    /// Set a custom render function for nodes.
    ///
    /// Parameters: (data, depth, expanded, is_leaf) -> Segments
    #[must_use]
    pub fn with_render_fn<F>(mut self, f: F) -> Self
    where
        F: Fn(&T, usize, bool, bool) -> Vec<Segment> + 'static,
    {
        self.render_fn = Box::new(f);
        self
    }

    /// Set the style for unselected nodes.
    #[must_use]
    pub fn with_node_style(mut self, style: Style) -> Self {
        self.node_style = style;
        self
    }

    /// Set the style for the selected node.
    #[must_use]
    pub fn with_selected_style(mut self, style: Style) -> Self {
        self.selected_style = style;
        self
    }

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

    /// Set a lazy load function for deferred child loading.
    #[must_use]
    pub fn with_lazy_load<F>(mut self, f: F) -> Self
    where
        F: Fn(&T) -> Vec<TreeNode<T>> + 'static,
    {
        self.lazy_load_fn = Some(Box::new(f));
        self
    }

    /// Get a reference to the root nodes.
    pub fn roots(&self) -> &[TreeNode<T>] {
        &self.roots
    }

    /// Get the selected visible node index.
    pub fn selected(&self) -> usize {
        self.selected
    }

    /// Get the scroll offset.
    pub fn scroll_offset(&self) -> usize {
        self.scroll_offset
    }

    /// Build the list of visible nodes by pre-order traversal.
    fn build_visible(&self) -> Vec<VisibleNode> {
        let mut result = Vec::new();
        for (idx, root) in self.roots.iter().enumerate() {
            self.collect_visible(root, 0, vec![idx], &mut result);
        }
        result
    }

    /// Recursively collect visible nodes.
    fn collect_visible(
        &self,
        node: &TreeNode<T>,
        depth: usize,
        path: Vec<usize>,
        result: &mut Vec<VisibleNode>,
    ) {
        result.push(VisibleNode {
            depth,
            path: path.clone(),
            expanded: node.expanded,
            is_leaf: node.is_leaf,
        });

        if node.expanded {
            for (child_idx, child) in node.children.iter().enumerate() {
                let mut child_path = path.clone();
                child_path.push(child_idx);
                self.collect_visible(child, depth + 1, child_path, result);
            }
        }
    }

    /// Get a mutable reference to a node by path.
    fn node_at_path_mut(&mut self, path: &[usize]) -> Option<&mut TreeNode<T>> {
        if path.is_empty() {
            return None;
        }
        let mut current = self.roots.get_mut(path[0])?;
        for &idx in &path[1..] {
            current = current.children.get_mut(idx)?;
        }
        Some(current)
    }

    /// Get an immutable reference to a node by path.
    fn node_at_path(&self, path: &[usize]) -> Option<&TreeNode<T>> {
        if path.is_empty() {
            return None;
        }
        let mut current = self.roots.get(path[0])?;
        for &idx in &path[1..] {
            current = current.children.get(idx)?;
        }
        Some(current)
    }

    /// Toggle expand/collapse at the selected node.
    pub fn toggle_selected(&mut self) {
        let visible = self.build_visible();
        if let Some(vnode) = visible.get(self.selected) {
            let path = vnode.path.clone();
            if let Some(node) = self.node_at_path_mut(&path)
                && !node.is_leaf
            {
                node.expanded = !node.expanded;
            }
        }
    }

    /// Expand the selected node (load children lazily if needed).
    pub fn expand_selected(&mut self) {
        let visible = self.build_visible();
        if let Some(vnode) = visible.get(self.selected) {
            let path = vnode.path.clone();
            let is_leaf = vnode.is_leaf;

            if is_leaf {
                return;
            }

            // Lazy load if needed
            if let Some(ref lazy_fn) = self.lazy_load_fn
                && let Some(node) = self.node_at_path(&path)
                && node.children.is_empty()
                && !node.is_leaf
            {
                let new_children = lazy_fn(&node.data);
                if let Some(node_mut) = self.node_at_path_mut(&path) {
                    node_mut.children = new_children;
                }
            }

            if let Some(node) = self.node_at_path_mut(&path) {
                node.expanded = true;
            }
        }
    }

    /// Collapse the selected node.
    pub fn collapse_selected(&mut self) {
        let visible = self.build_visible();
        if let Some(vnode) = visible.get(self.selected) {
            let path = vnode.path.clone();

            if let Some(node) = self.node_at_path_mut(&path)
                && node.expanded
            {
                node.expanded = false;
                return;
            }

            // If already collapsed, move to parent
            if path.len() > 1 {
                let parent_path = &path[..path.len() - 1];
                // Find parent in visible list
                for (idx, v) in visible.iter().enumerate() {
                    if v.path == parent_path {
                        self.selected = idx;
                        break;
                    }
                }
            }
        }
    }

    /// Get a reference to the data of the selected visible node.
    pub fn selected_node(&self) -> Option<&TreeNode<T>> {
        let visible = self.build_visible();
        visible
            .get(self.selected)
            .and_then(|v| self.node_at_path(&v.path))
    }

    /// Get the total number of visible nodes.
    pub fn visible_count(&self) -> usize {
        self.build_visible().len()
    }

    /// Ensure the selected item is visible by adjusting scroll_offset.
    fn ensure_selected_visible(&mut self, visible_height: usize) {
        if visible_height == 0 {
            return;
        }
        if self.selected < self.scroll_offset {
            self.scroll_offset = self.selected;
        }
        if self.selected >= self.scroll_offset + visible_height {
            self.scroll_offset = self
                .selected
                .saturating_sub(visible_height.saturating_sub(1));
        }
    }
}

impl<T> Widget for Tree<T> {
    fn render(&self, area: Rect, buf: &mut ScreenBuffer) {
        if area.size.width == 0 || area.size.height == 0 {
            return;
        }

        super::border::render_border(area, self.border, self.node_style.clone(), buf);

        let inner = super::border::inner_area(area, self.border);
        if inner.size.width == 0 || inner.size.height == 0 {
            return;
        }

        let height = inner.size.height as usize;
        let width = inner.size.width as usize;
        let visible = self.build_visible();
        let count = visible.len();

        let max_offset = count.saturating_sub(height.max(1));
        let scroll = self.scroll_offset.min(max_offset);
        let visible_end = (scroll + height).min(count);

        for (row, vis_idx) in (scroll..visible_end).enumerate() {
            let y = inner.position.y + row as u16;
            if let Some(vnode) = visible.get(vis_idx) {
                let is_selected = vis_idx == self.selected;
                let style = if is_selected {
                    &self.selected_style
                } else {
                    &self.node_style
                };

                // Fill row if selected
                if is_selected {
                    for col in 0..inner.size.width {
                        buf.set(inner.position.x + col, y, Cell::new(" ", style.clone()));
                    }
                }

                // Indentation (2 spaces per level)
                let indent = vnode.depth * 2;

                // Expand indicator
                let indicator = if vnode.is_leaf {
                    " "
                } else if vnode.expanded {
                    "\u{25bc}" //                } else {
                    "\u{25b6}" //                };

                // Render indent + indicator + content
                let mut col: u16 = 0;

                // Indent
                for _ in 0..indent {
                    if col as usize >= width {
                        break;
                    }
                    buf.set(inner.position.x + col, y, Cell::new(" ", style.clone()));
                    col += 1;
                }

                // Indicator
                if (col as usize) < width {
                    buf.set(
                        inner.position.x + col,
                        y,
                        Cell::new(indicator, style.clone()),
                    );
                    col += 1;
                }

                // Space after indicator
                if (col as usize) < width {
                    buf.set(inner.position.x + col, y, Cell::new(" ", style.clone()));
                    col += 1;
                }

                // Content from render_fn
                if let Some(node) = self.node_at_path(&vnode.path) {
                    let segments =
                        (self.render_fn)(&node.data, vnode.depth, vnode.expanded, vnode.is_leaf);
                    for segment in &segments {
                        if col as usize >= width {
                            break;
                        }
                        let remaining = width.saturating_sub(col as usize);
                        let truncated = truncate_to_display_width(&segment.text, remaining);
                        for ch in truncated.chars() {
                            let char_w =
                                UnicodeWidthStr::width(ch.encode_utf8(&mut [0; 4]) as &str);
                            if col as usize + char_w > width {
                                break;
                            }
                            buf.set(
                                inner.position.x + col,
                                y,
                                Cell::new(ch.to_string(), style.clone()),
                            );
                            col += char_w as u16;
                        }
                    }
                }
            }
        }
    }
}

impl<T> InteractiveWidget for Tree<T> {
    fn handle_event(&mut self, event: &Event) -> EventResult {
        let Event::Key(KeyEvent { code, .. }) = event else {
            return EventResult::Ignored;
        };

        let count = self.visible_count();

        match code {
            KeyCode::Up => {
                if self.selected > 0 {
                    self.selected -= 1;
                    self.ensure_selected_visible(20);
                }
                EventResult::Consumed
            }
            KeyCode::Down => {
                if count > 0 && self.selected < count.saturating_sub(1) {
                    self.selected += 1;
                    self.ensure_selected_visible(20);
                }
                EventResult::Consumed
            }
            KeyCode::Right => {
                self.expand_selected();
                EventResult::Consumed
            }
            KeyCode::Left => {
                self.collapse_selected();
                EventResult::Consumed
            }
            KeyCode::Enter => {
                self.toggle_selected();
                EventResult::Consumed
            }
            KeyCode::PageUp => {
                let page = 20;
                self.selected = self.selected.saturating_sub(page);
                self.ensure_selected_visible(20);
                EventResult::Consumed
            }
            KeyCode::PageDown => {
                let page = 20;
                if count > 0 {
                    self.selected = (self.selected + page).min(count.saturating_sub(1));
                    self.ensure_selected_visible(20);
                }
                EventResult::Consumed
            }
            KeyCode::Home => {
                self.selected = 0;
                self.scroll_offset = 0;
                EventResult::Consumed
            }
            KeyCode::End => {
                if count > 0 {
                    self.selected = count.saturating_sub(1);
                    self.ensure_selected_visible(20);
                }
                EventResult::Consumed
            }
            _ => EventResult::Ignored,
        }
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
    use super::*;
    use crate::geometry::Size;

    fn make_render_fn() -> impl Fn(&String, usize, bool, bool) -> Vec<Segment> {
        |data: &String, _depth, _expanded, _is_leaf| vec![Segment::new(data)]
    }

    fn make_test_tree() -> Tree<String> {
        let tree = TreeNode::branch("root".into())
            .with_child(TreeNode::new("leaf1".into()))
            .with_child(
                TreeNode::branch("branch1".into())
                    .with_child(TreeNode::new("child1".into()))
                    .with_child(TreeNode::new("child2".into())),
            )
            .with_child(TreeNode::new("leaf2".into()));

        Tree::new(vec![tree]).with_render_fn(make_render_fn())
    }

    #[test]
    fn create_tree_with_nodes() {
        let tree = make_test_tree();
        assert_eq!(tree.roots().len(), 1);
    }

    #[test]
    fn render_collapsed_tree_only_roots() {
        let tree = make_test_tree();
        // Only root is visible (collapsed)
        assert_eq!(tree.visible_count(), 1);

        let mut buf = ScreenBuffer::new(Size::new(30, 10));
        tree.render(Rect::new(0, 0, 30, 10), &mut buf);

        // Should show "▶ root" (▶ is the expand indicator for non-leaf)
        assert_eq!(buf.get(0, 0).map(|c| c.grapheme.as_str()), Some("\u{25b6}"));
    }

    #[test]
    fn expand_node_children_visible() {
        let mut tree = make_test_tree();
        // Expand root
        tree.toggle_selected();

        // Root + 3 children: leaf1, branch1, leaf2
        assert_eq!(tree.visible_count(), 4);
    }

    #[test]
    fn collapse_node_hides_children() {
        let mut tree = make_test_tree();
        tree.toggle_selected(); // expand
        assert_eq!(tree.visible_count(), 4);
        tree.toggle_selected(); // collapse
        assert_eq!(tree.visible_count(), 1);
    }

    #[test]
    fn navigate_visible_nodes() {
        let mut tree = make_test_tree();
        tree.toggle_selected(); // expand root

        let down = Event::Key(KeyEvent {
            code: KeyCode::Down,
            modifiers: crate::event::Modifiers::NONE,
        });
        let up = Event::Key(KeyEvent {
            code: KeyCode::Up,
            modifiers: crate::event::Modifiers::NONE,
        });

        assert_eq!(tree.selected(), 0);
        tree.handle_event(&down);
        assert_eq!(tree.selected(), 1); // leaf1
        tree.handle_event(&down);
        assert_eq!(tree.selected(), 2); // branch1
        tree.handle_event(&up);
        assert_eq!(tree.selected(), 1);
    }

    #[test]
    fn right_key_expands() {
        let mut tree = make_test_tree();
        let right = Event::Key(KeyEvent {
            code: KeyCode::Right,
            modifiers: crate::event::Modifiers::NONE,
        });

        tree.handle_event(&right); // expand root
        assert_eq!(tree.visible_count(), 4);
    }

    #[test]
    fn left_key_collapses() {
        let mut tree = make_test_tree();
        tree.toggle_selected(); // expand root
        let left = Event::Key(KeyEvent {
            code: KeyCode::Left,
            modifiers: crate::event::Modifiers::NONE,
        });

        tree.handle_event(&left); // collapse root
        assert_eq!(tree.visible_count(), 1);
    }

    #[test]
    fn enter_toggles() {
        let mut tree = make_test_tree();
        let enter = Event::Key(KeyEvent {
            code: KeyCode::Enter,
            modifiers: crate::event::Modifiers::NONE,
        });

        tree.handle_event(&enter); // expand
        assert_eq!(tree.visible_count(), 4);
        tree.handle_event(&enter); // collapse
        assert_eq!(tree.visible_count(), 1);
    }

    #[test]
    fn lazy_load_on_expand() {
        use std::cell::RefCell;
        use std::rc::Rc;

        let load_count = Rc::new(RefCell::new(0));
        let captured = Rc::clone(&load_count);

        let root = TreeNode::branch("root".into());
        let mut tree = Tree::new(vec![root])
            .with_render_fn(make_render_fn())
            .with_lazy_load(move |_data: &String| {
                *captured.borrow_mut() += 1;
                vec![
                    TreeNode::new("loaded1".into()),
                    TreeNode::new("loaded2".into()),
                ]
            });

        tree.expand_selected();
        assert_eq!(*load_count.borrow(), 1);
        // root (expanded) + 2 loaded children
        assert_eq!(tree.visible_count(), 3);
    }

    #[test]
    fn selected_node_retrieval() {
        let mut tree = make_test_tree();
        tree.toggle_selected(); // expand root

        // Select leaf1
        tree.selected = 1;
        match tree.selected_node() {
            Some(node) => assert_eq!(node.data, "leaf1"),
            None => unreachable!("should have selected node"),
        }
    }

    #[test]
    fn utf8_safe_node_labels() {
        let node = TreeNode::new("你好".into());
        let tree = Tree::new(vec![node]).with_render_fn(make_render_fn());

        let mut buf = ScreenBuffer::new(Size::new(10, 1));
        tree.render(Rect::new(0, 0, 10, 1), &mut buf);

        // " 你" — space indicator + space + CJK chars
        // Position 2 (after " " indicator + " " space) should be "你"
        assert_eq!(buf.get(2, 0).map(|c| c.grapheme.as_str()), Some(""));
    }

    #[test]
    fn empty_tree() {
        let tree: Tree<String> = Tree::new(vec![]).with_render_fn(make_render_fn());
        assert_eq!(tree.visible_count(), 0);
        assert!(tree.selected_node().is_none());

        // Should not crash on render
        let mut buf = ScreenBuffer::new(Size::new(20, 5));
        tree.render(Rect::new(0, 0, 20, 5), &mut buf);
    }

    #[test]
    fn deep_tree_multiple_levels() {
        let deep = TreeNode::branch("L0".into()).with_child(
            TreeNode::branch("L1".into())
                .with_child(TreeNode::branch("L2".into()).with_child(TreeNode::new("L3".into()))),
        );

        let mut tree = Tree::new(vec![deep]).with_render_fn(make_render_fn());

        // Expand all levels
        tree.expand_selected(); // L0
        let down = Event::Key(KeyEvent {
            code: KeyCode::Down,
            modifiers: crate::event::Modifiers::NONE,
        });
        tree.handle_event(&down); // select L1
        tree.expand_selected(); // L1
        tree.handle_event(&down); // select L2
        tree.handle_event(&down); // stay or move to L2
        tree.selected = 2; // L2
        tree.expand_selected(); // L2

        // L0, L1, L2, L3 all visible
        assert_eq!(tree.visible_count(), 4);
    }

    #[test]
    fn mixed_expanded_collapsed() {
        let mut tree = make_test_tree();
        tree.toggle_selected(); // expand root
        // Navigate to branch1 (idx 2) and expand it
        tree.selected = 2;
        tree.toggle_selected(); // expand branch1

        // root, leaf1, branch1, child1, child2, leaf2
        assert_eq!(tree.visible_count(), 6);

        // Collapse branch1
        tree.toggle_selected();
        // root, leaf1, branch1, leaf2
        assert_eq!(tree.visible_count(), 4);
    }

    #[test]
    fn render_with_border() {
        let tree = make_test_tree();
        let tree = Tree {
            border: BorderStyle::Single,
            ..tree
        };

        let mut buf = ScreenBuffer::new(Size::new(20, 10));
        tree.render(Rect::new(0, 0, 20, 10), &mut buf);

        assert_eq!(buf.get(0, 0).map(|c| c.grapheme.as_str()), Some("\u{250c}"));
    }
}