lipgloss-tree 0.1.1

A tree component for terminal user interfaces, styled with Lip Gloss.
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
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
//! Tree rendering engine for lipgloss-tree.
//!
//! This module provides the core rendering functionality for tree structures,
//! handling complex styling, alignment, and multiline content rendering.
//! It supports both built-in tree glyphs (├──, └──, etc.) and custom
//! enumerators with proper alignment and styling inheritance.

use crate::children::Children;
use crate::{default_enumerator, default_indenter, Enumerator, Indenter, Node, StyleFunc};
use lipgloss::{height, join_horizontal, join_vertical, Style, LEFT, TOP};
use unicode_width::UnicodeWidthStr;

/// Minimal Children implementation used to synthesize enumerator glyphs with controlled length/index.
///
/// This is used internally by the renderer to generate the correct branch characters
/// (├── vs └──) based on whether a node is the last in its sequence.
struct DummyChildren {
    /// The virtual length of this children collection
    len: usize,
}
impl Children for DummyChildren {
    fn at(&self, _index: usize) -> Option<&dyn Node> {
        None
    }
    fn length(&self) -> usize {
        self.len
    }
}

/// A filtered view of children that exposes only visible, non-empty nodes via an index map.
///
/// This wrapper allows the renderer to work with only the nodes that will actually
/// produce visible output, while maintaining the correct indices for style functions.
struct VisibleChildren<'a> {
    /// Reference to the underlying children collection
    base: &'a dyn Children,
    /// Mapping from visible index to actual index in the base collection
    map: Vec<usize>,
}
impl<'a> Children for VisibleChildren<'a> {
    fn at(&self, index: usize) -> Option<&dyn Node> {
        self.map.get(index).and_then(|&i| self.base.at(i))
    }
    fn length(&self) -> usize {
        self.map.len()
    }
}

/// Styling configuration for tree rendering.
///
/// `TreeStyle` controls how different parts of the tree are styled, including
/// enumerators (branch characters like ├──), items (node content), and the root.
/// It supports both base styles (applied unconditionally) and function styles
/// (applied per-child based on index and context).
///
/// # Examples
///
/// ```rust
/// use lipgloss::Style;
/// use lipgloss_tree::renderer::TreeStyle;
///
/// let style = TreeStyle {
///     enumerator_func: |_, _| Style::new().foreground("blue"),
///     item_func: |_, _| Style::new().bold(true),
///     enumerator_base: Some(Style::new().padding_right(1)),
///     item_base: None,
///     root: Style::new().bold(true),
/// };
/// ```
#[derive(Debug, Clone)]
pub struct TreeStyle {
    /// Function style applied to enumerators (branch characters) based on child index
    pub enumerator_func: StyleFunc,
    /// Function style applied to item content based on child index  
    pub item_func: StyleFunc,
    /// Base style applied unconditionally to all enumerators before function styles
    pub enumerator_base: Option<Style>,
    /// Base style applied unconditionally to all items before function styles
    pub item_base: Option<Style>,
    /// Style applied to the root node
    pub root: Style,
}

impl Default for TreeStyle {
    /// Creates a default `TreeStyle` with Go lipgloss-compatible behavior.
    ///
    /// The default includes `padding_right(1)` on enumerators to match Go's
    /// behavior of printing a space after tree branch prefixes. Tests that
    /// need different behavior can override with no-op functions.
    fn default() -> Self {
        Self {
            // By default, Go prints a space after the prefix. Model this
            // with padding_right(1). Tests that set nil funcs expect no space
            // and will override with a no-op function.
            enumerator_func: |_children, _i| Style::new().padding_right(1),
            item_func: |_children, _i| Style::new(),
            enumerator_base: None,
            item_base: None,
            root: Style::new(),
        }
    }
}

/// The main tree rendering engine.
///
/// `Renderer` is responsible for converting tree node structures into styled
/// text output. It handles complex features like:
/// - Multi-line content with proper indentation
/// - Custom enumerators and indenters  
/// - Style inheritance and override resolution
/// - Alignment of custom enumerators
/// - Branch continuation across container nodes
///
/// # Examples
///
/// ```rust
/// use lipgloss_tree::{Renderer, Children};
/// use lipgloss_tree::renderer::TreeStyle;
/// use lipgloss::Style;
///
/// let renderer = Renderer::new()
///     .style(TreeStyle::default())
///     .enumerator(|children, i| {
///         if i == children.length() - 1 { "└──".to_string() }
///         else { "├──".to_string() }
///     });
/// ```
#[derive(Clone)]
pub struct Renderer {
    /// Styling configuration for this renderer
    style: TreeStyle,
    /// Function to generate enumerator strings (branch characters)
    enumerator: Enumerator,
    /// Function to generate indentation strings for nested content
    indenter: Indenter,
}

impl Renderer {
    /// Creates a new renderer with default configuration.
    ///
    /// The default renderer uses:
    /// - Standard box-drawing characters for branches (├──, └──)
    /// - Standard indentation (│   for continuing, spaces for last)
    /// - Default styling with padding after enumerators
    ///
    /// # Examples
    ///
    /// ```rust
    /// use lipgloss_tree::Renderer;
    ///
    /// let renderer = Renderer::new();
    /// ```
    pub fn new() -> Self {
        Self {
            style: TreeStyle::default(),
            enumerator: default_enumerator,
            indenter: default_indenter,
        }
    }

    /// Sets the styling configuration for this renderer.
    ///
    /// This replaces the entire `TreeStyle`, affecting how enumerators,
    /// items, and the root are styled.
    ///
    /// # Arguments
    ///
    /// * `style` - The new `TreeStyle` configuration to use
    ///
    /// # Examples
    ///
    /// ```rust
    /// use lipgloss_tree::Renderer;
    /// use lipgloss_tree::renderer::TreeStyle;
    /// use lipgloss::Style;
    ///
    /// let custom_style = TreeStyle {
    ///     root: Style::new().bold(true),
    ///     ..TreeStyle::default()
    /// };
    ///
    /// let renderer = Renderer::new().style(custom_style);
    /// ```
    pub fn style(mut self, style: TreeStyle) -> Self {
        self.style = style;
        self
    }

    /// Sets the enumerator function for this renderer.
    ///
    /// The enumerator function generates the branch characters (like ├──, └──)
    /// based on the child's position in its parent's children collection.
    ///
    /// # Arguments
    ///
    /// * `enumerator` - Function that takes `(children, index)` and returns a string
    ///
    /// # Examples
    ///
    /// ```rust
    /// use lipgloss_tree::{Renderer, Children};
    ///
    /// let renderer = Renderer::new()
    ///     .enumerator(|children, i| {
    ///         if i == children.length() - 1 {
    ///             "╰──".to_string()  // Rounded last branch
    ///         } else {
    ///             "├──".to_string()  // Standard continuing branch
    ///         }
    ///     });
    /// ```
    pub fn enumerator(mut self, enumerator: Enumerator) -> Self {
        self.enumerator = enumerator;
        self
    }

    /// Sets the indenter function for this renderer.
    ///
    /// The indenter function generates indentation strings for child content
    /// that appears below parent nodes, providing the visual connection
    /// between parent and nested children.
    ///
    /// # Arguments
    ///
    /// * `indenter` - Function that takes `(children, index)` and returns indentation string
    ///
    /// # Examples
    ///
    /// ```rust
    /// use lipgloss_tree::{Renderer, Children};
    ///
    /// let renderer = Renderer::new()
    ///     .indenter(|children, i| {
    ///         if i == children.length() - 1 {
    ///             "    ".to_string()  // Spaces for last child
    ///         } else {
    ///             "│   ".to_string()  // Vertical line for continuing
    ///         }
    ///     });
    /// ```
    pub fn indenter(mut self, indenter: Indenter) -> Self {
        self.indenter = indenter;
        self
    }

    /// Renders a tree node and its children to a formatted string.
    ///
    /// This is the main rendering method that converts a tree structure into
    /// styled text output. It handles complex scenarios like multi-line content,
    /// style inheritance, custom enumerators, and proper indentation.
    ///
    /// # Arguments
    ///
    /// * `node` - The tree node to render
    /// * `root` - Whether this is the root node (affects root style application)
    /// * `prefix` - String prefix to prepend to each line (for nested rendering)
    ///
    /// # Returns
    ///
    /// A formatted string representation of the tree with proper styling and indentation
    ///
    /// # Examples
    ///
    /// ```rust
    /// use lipgloss_tree::{Renderer, Tree};
    ///
    /// let tree = Tree::new()
    ///     .root("My Tree")
    ///     .child(vec!["Item 1".into(), "Item 2".into()]);
    ///
    /// let renderer = Renderer::new();
    /// let output = renderer.render(&tree, true, "");
    /// println!("{}", output);
    /// ```
    pub fn render(&self, node: &dyn Node, root: bool, prefix: &str) -> String {
        if node.hidden() {
            return String::new();
        }

        // Debug: uncomment for debugging
        // eprintln!("RENDER_START: root={}, prefix='{}', prefix_len={}", root, prefix.replace('\n', "\\n"), prefix.len());

        let mut strs = Vec::new();
        let children = node.children();
        // Prefer per-node overrides for enumerator/indenter when present, otherwise use renderer config
        let enumerator = node.get_enumerator().copied().unwrap_or(self.enumerator);
        let indenter = node.get_indenter().copied().unwrap_or(self.indenter);

        // Print the root node name if it's not empty
        if !node.value().is_empty() && root {
            strs.push(self.style.root.render(&node.value()));
        }

        // Build a filtered view of direct children that will render a line (non-hidden, non-empty)
        let mut visible_nodes: Vec<Box<dyn Node>> = Vec::new();
        for i in 0..children.length() {
            if let Some(child) = children.at(i) {
                if child.hidden() || child.value().is_empty() {
                    continue;
                }
                visible_nodes.push(child.clone_node());
            }
        }
        let filtered_children = crate::children::NodeChildren::from_nodes(visible_nodes);

        // Helper: does this node or any of its descendants render a visible line?
        fn has_visible_line(node: &dyn Node) -> bool {
            if node.hidden() {
                return false;
            }
            if !node.value().is_empty() {
                return true;
            }
            let ch = node.children();
            for i in 0..ch.length() {
                if let Some(n) = ch.at(i) {
                    if has_visible_line(n) {
                        return true;
                    }
                }
            }
            false
        }

        // Precompute which visible direct child is last in the overall visual sequence.
        // A visible child is last if there is no later sibling (direct) whose subtree would
        // render any visible line.
        let mut is_last_vec: Vec<bool> = vec![true; filtered_children.length()];
        #[allow(clippy::needless_range_loop)]
        for vi in 0..filtered_children.length() {
            let mut last = true;
            // locate this visible child among all direct children
            let mut seen = 0usize;
            for i in 0..children.length() {
                if let Some(ch) = children.at(i) {
                    if ch.hidden() {
                        continue;
                    }
                    if !ch.value().is_empty() {
                        if seen == vi {
                            // check later siblings
                            for j in (i + 1)..children.length() {
                                if let Some(next) = children.at(j) {
                                    if has_visible_line(next) {
                                        last = false;
                                        break;
                                    }
                                }
                            }
                            break;
                        }
                        seen += 1;
                    }
                }
            }
            is_last_vec[vi] = last;
        }

        // Prepare a visible-children view for style functions
        let mut vis_map: Vec<usize> = Vec::new();
        for i in 0..children.length() {
            if let Some(ch) = children.at(i) {
                if !ch.hidden() && !ch.value().is_empty() {
                    vis_map.push(i);
                }
            }
        }
        let vis_children = VisibleChildren {
            base: &*children,
            map: vis_map,
        };

        // Helper to detect built-in branch glyphs
        let is_branch = |s: &str| s == "├──" || s == "└──" || s == "╰──";

        // Calculate alignment padding for custom enumerators (not built-in branch glyphs)
        let mut max_enum_width = 0;
        for i in 0..filtered_children.length() {
            let user_pref = enumerator(&vis_children, i);
            if !is_branch(&user_pref) {
                // Only consider custom enumerators for alignment
                let width = user_pref.width();
                if width > max_enum_width {
                    max_enum_width = width;
                }
            }
        }

        // Render children
        let mut last_display_indent = String::new();
        for i in 0..children.length() {
            if let Some(child) = children.at(i) {
                if child.hidden() {
                    continue;
                }

                // Determine display index for visible children by counting prior visible siblings
                let mut display_idx_opt: Option<usize> = None;
                if !child.value().is_empty() {
                    let mut count = 0usize;
                    for j in 0..i {
                        if let Some(prev) = children.at(j) {
                            if !prev.hidden() && !prev.value().is_empty() {
                                count += 1;
                            }
                        }
                    }
                    display_idx_opt = Some(count);
                }
                let idx = display_idx_opt.unwrap_or(0);
                // Build effective styles for this node, respecting node overrides first
                let enum_style_func = node
                    .get_enumerator_style_func()
                    .copied()
                    .unwrap_or(self.style.enumerator_func);
                let item_style_func = node
                    .get_item_style_func()
                    .copied()
                    .unwrap_or(self.style.item_func);

                let enum_base = node
                    .get_enumerator_style()
                    .cloned()
                    .or_else(|| self.style.enumerator_base.clone());
                let item_base = node
                    .get_item_style()
                    .cloned()
                    .or_else(|| self.style.item_base.clone());

                // Compute indent: for visible children use indenter(filtered, display_idx);
                // for container (empty value) nodes, reuse the last visible indent so nested
                // content attaches under the previous item.
                let raw_indent = if let Some(di) = display_idx_opt {
                    indenter(&filtered_children, di)
                } else {
                    last_display_indent.clone()
                };
                // Apply styling to indent based on the type of indenter
                let indent = if raw_indent.trim().is_empty() {
                    // Standard whitespace indenter - apply item_base style if present (for padding)
                    if let Some(base) = &item_base {
                        base.render(&raw_indent)
                    } else {
                        raw_indent.clone()
                    }
                } else {
                    // Custom indenter (like "->") - apply enum_base style if present (for colors/styling)
                    if let Some(base) = &enum_base {
                        base.render(&raw_indent)
                    } else {
                        raw_indent.clone()
                    }
                };

                // Compute enumerator only for visible children
                // Base branch according to position in overall visual sequence
                let user_pref = enumerator(&vis_children, idx);
                let is_custom_enum = !is_branch(&user_pref);
                let mut node_prefix = if !is_custom_enum {
                    let dc = DummyChildren { len: 2 };
                    if is_last_vec[idx] {
                        enumerator(&dc, 1)
                    } else {
                        enumerator(&dc, 0)
                    }
                } else {
                    user_pref.clone()
                };

                // Apply alignment padding for custom enumerators
                if !is_custom_enum {
                    // Built-in branch glyph - no alignment needed
                } else if max_enum_width > 0 {
                    // Custom enumerator - apply alignment padding
                    let current_width = node_prefix.width();
                    let padding_needed = max_enum_width.saturating_sub(current_width);
                    if padding_needed > 0 {
                        node_prefix = format!("{}{}", " ".repeat(padding_needed), node_prefix);
                    }
                }

                // CRITICAL: Apply either base style OR function style, NEVER both
                // This prevents double-padding issues where both base and function styles add spacing
                //
                // PADDING_RIGHT BEHAVIOR:
                // - TreeStyle::default() sets enumerator_func to add padding_right(1)
                // - If enumerator_style (base) is set, it REPLACES the function entirely
                // - The base style can include its own padding_right(1)
                // - Go behavior: EnumeratorStyle() method replaces the default function
                //
                // SPACING CALCULATION:
                // - Tree symbols: "├──", "└──" are 3 chars wide
                // - padding_right(1) adds 1 space → "├── " (4 chars total)
                // - This aligns with default_indenter: "│   " (4 chars) and "    " (4 spaces)
                if let Some(base) = &enum_base {
                    // Base style set via .enumerator_style() - use ONLY this style
                    // Example: Style::new().foreground(color).padding_right(1)
                    node_prefix = base.render(&node_prefix);
                } else {
                    // No base style - use the function style (default or custom)
                    let enum_style_result = enum_style_func(&vis_children, idx);
                    let enum_lead = enum_style_result.render("");

                    // Check if this is a set_string style vs padding-only style
                    if !enum_lead.is_empty() && !enum_lead.trim().is_empty() {
                        // Set_string style with actual content (e.g., "+" prefix)
                        // Apply default padding to tree structure, then prepend the content
                        let default_styled = Style::new().padding_right(1).render(&node_prefix);
                        if !enum_lead.ends_with(' ') {
                            node_prefix = format!("{} {}", enum_lead, default_styled);
                        } else {
                            node_prefix = format!("{}{}", enum_lead, default_styled);
                        }
                    } else {
                        // Padding-only style or no additional content
                        // Apply style function directly to tree structure
                        node_prefix = enum_style_result.render(&node_prefix);
                    }
                }
                // Note: Alignment padding disabled - Go doesn't align prefixes to same width
                // Debug: uncomment for debugging
                // eprintln!("RENDER: idx={}, prefix='{}', width={}, max_len={}, final='{}'", idx, node_prefix.replace('\n', "\\n"), prefix_width, max_len, node_prefix.replace('\n', "\\n"));
                // Apply item styling: base style OR function style, not both
                let mut item = child.value();
                if let Some(base) = &item_base {
                    item = base.render(&item);
                } else {
                    // Only apply function style if no base style is set
                    let item_style_result = item_style_func(&vis_children, idx);
                    let item_lead = item_style_result.render("");

                    // Check if this is a true set_string style vs padding-only style
                    // Padding-only styles render to whitespace-only strings (spaces, tabs, newlines)
                    // Set_string styles have non-whitespace content
                    let is_padding_only = item_lead.chars().all(|c| c.is_whitespace());

                    if is_padding_only {
                        // Apply the style function to the item directly (padding-only or no style)
                        item = item_style_result.render(&item);
                    } else {
                        // This is a style with a string set via set_string (like Go's SetString)
                        if !item_lead.ends_with(' ') {
                            item = format!("{} {}", item_lead, item);
                        } else {
                            item = format!("{}{}", item_lead, item);
                        }
                    }
                }
                let mut multiline_prefix = prefix.to_string();

                // Handle multiline prefixes and items
                let item_height = height(&item);
                let mut node_prefix_height = height(&node_prefix);

                // Extend node prefix if item is taller
                while item_height > node_prefix_height {
                    // Use raw indent for multiline extension - no styling needed
                    let extension_indent = indent.clone();
                    node_prefix = join_vertical(LEFT, &[&node_prefix, &extension_indent]);
                    node_prefix_height = height(&node_prefix);
                }

                // Extend multiline prefix if node prefix is taller
                let mut multiline_prefix_height = height(&multiline_prefix);
                while node_prefix_height > multiline_prefix_height {
                    multiline_prefix = join_vertical(LEFT, &[&multiline_prefix, prefix]);
                    multiline_prefix_height = height(&multiline_prefix);
                }

                // Only emit a line if the child has a non-empty value; empty-value nodes are containers for sublists
                if !child.value().is_empty() {
                    // FINAL LINE ASSEMBLY:
                    // multiline_prefix: parent indentation (e.g., 10 spaces from nested list level)
                    // node_prefix: tree symbol with styling (e.g., "[color]├──[reset] " with padding_right)
                    // item: content with any item styling applied
                    //
                    // BACKGROUND COLOR CONSISTENCY:
                    // When tree components have background colors, we need to ensure the multiline_prefix
                    // also gets the same background color to avoid black rectangular gaps.

                    let styled_multiline_prefix = if !multiline_prefix.trim().is_empty() {
                        // If there is indentation content, check if we should apply background styling
                        // Priority: enum_base > item_base (prefer the enumerator background)
                        if let Some(base) = &enum_base {
                            // If enumerator has background, apply it to prefix indentation
                            if base.get_background().is_some() {
                                base.render(&multiline_prefix)
                            } else {
                                multiline_prefix.clone()
                            }
                        } else if let Some(base) = &item_base {
                            // If item has background, apply it to prefix indentation
                            if base.get_background().is_some() {
                                base.render(&multiline_prefix)
                            } else {
                                multiline_prefix.clone()
                            }
                        } else {
                            multiline_prefix.clone()
                        }
                    } else {
                        multiline_prefix.clone()
                    };

                    // DEBUG: Uncomment to debug spacing issues
                    // eprintln!("DEBUG: multiline_prefix='{}', node_prefix='{}', item='{}'",
                    //           styled_multiline_prefix.replace(' ', "·"),
                    //           node_prefix.replace(' ', "·"),
                    //           item.replace(' ', "·"));

                    let line =
                        join_horizontal(TOP, &[&styled_multiline_prefix, &node_prefix, &item]);
                    strs.push(line);
                    // Remember raw indent for subsequent container nodes (before styling)
                    last_display_indent = raw_indent.clone();
                }

                // Recursively render children
                if child.children().length() > 0 {
                    // Even if the child has an empty value (container), we still need to
                    // indent its children so they appear nested under the current item.
                    // Use styled indent with enum styling applied to indenter characters
                    // This ensures custom indenters (like "->") get the same styling as enumerators
                    let styled_indent = indent.clone();

                    // SMART INDENTATION LOGIC:
                    // Trees nested in lists should not inherit the list's indenter to avoid double indentation.
                    let dummy_children = crate::children::NodeChildren::new();
                    let parent_indent_sample = indenter(&dummy_children, 0);
                    #[allow(unused_variables)]
                    let is_parent_list_indenter =
                        parent_indent_sample.trim() == "" && parent_indent_sample.len() == 2;

                    let child_prefix = if let Some(child_indenter) = child.get_indenter() {
                        let child_indent_sample = child_indenter(&dummy_children, 0);
                        #[allow(unused_variables)]
                        let is_child_tree_indenter =
                            child_indent_sample.contains('') || child_indent_sample.len() == 4;

                        // DEBUG: uncomment for debugging tree indentation
                        // if child.get_enumerator_style().is_some() {
                        //     eprintln!("DEBUG TREE: parent_indent='{}', child_indent='{}', is_parent_list={}, is_child_tree={}, prefix_len={}",
                        //         parent_indent_sample.replace(' ', "·"), child_indent_sample.replace(' ', "·"), is_parent_list_indenter, is_child_tree_indenter, prefix.len());
                        // }

                        // Always provide proper nesting prefix - the issue is tree's internal indentation, not prefix
                        format!("{}{}", prefix, styled_indent)
                    } else {
                        // Child has no specific indenter - inherit parent's indentation
                        format!("{}{}", prefix, styled_indent)
                    };

                    // Detect if child has style overrides (indicating it's a styled tree vs plain container)
                    let has_style_overrides = child.get_enumerator_style().is_some()
                        || child.get_item_style().is_some()
                        || child.get_enumerator_style_func().is_some()
                        || child.get_item_style_func().is_some();

                    // Special case: if this child is a tree with its own indenter, use fresh renderer
                    // to prevent list indenter from affecting tree's internal rendering
                    let child_uses_tree_indenter =
                        if let Some(child_indenter) = child.get_indenter() {
                            let dummy = crate::children::NodeChildren::new();
                            let sample = child_indenter(&dummy, 0);
                            sample.contains('') || sample.len() == 4
                        } else {
                            false
                        };

                    let mut child_renderer = if child_uses_tree_indenter {
                        // Tree child: use fresh renderer to avoid inheriting list behavior
                        Renderer::new()
                    } else if has_style_overrides {
                        // Child has style overrides: use tree defaults for behavior but child styles
                        Renderer::new()
                    } else {
                        // Child has no overrides: inherit parent's behavior
                        Renderer::new()
                            .enumerator(self.enumerator)
                            .indenter(self.indenter)
                    };

                    // Apply any explicit functional overrides from the child node itself
                    if let Some(e) = child.get_enumerator() {
                        child_renderer = child_renderer.enumerator(*e);
                    }
                    if let Some(i) = child.get_indenter() {
                        child_renderer = child_renderer.indenter(*i);
                    }

                    // For style functions, use tree defaults unless child has specific overrides
                    // Children should inherit parent styles when they don't have their own
                    let style = TreeStyle {
                        enumerator_func: child
                            .get_enumerator_style_func()
                            .copied()
                            .unwrap_or(|_, _| Style::new().padding_right(1)),
                        item_func: child
                            .get_item_style_func()
                            .copied()
                            .unwrap_or(|_, _| Style::new()),
                        root: Style::default(),
                        // Inherit parent's base styles if child doesn't have overrides
                        enumerator_base: child
                            .get_enumerator_style()
                            .cloned()
                            .or_else(|| self.style.enumerator_base.clone()),
                        item_base: child
                            .get_item_style()
                            .cloned()
                            .or_else(|| self.style.item_base.clone()),
                    };
                    child_renderer = child_renderer.style(style);

                    let mut child_output = child_renderer.render(child, false, &child_prefix);
                    // If this child is an unnamed container and there are later siblings that
                    // will render visible lines, ensure the container's last visible branch uses
                    // the mid-branch glyph to visually continue the vertical line across
                    // containers (e.g., the 5th Qux before subsequent Quux entries).
                    if child.value().is_empty() {
                        let mut future_exists = false;
                        for j in (i + 1)..children.length() {
                            if let Some(next) = children.at(j) {
                                // Only consider later unnamed containers, which will visually
                                // continue under the same prior item indent.
                                if next.value().is_empty() && has_visible_line(next) {
                                    future_exists = true;
                                    break;
                                }
                            }
                        }
                        if future_exists {
                            // Replace the deepest last-branch at this level with a mid-branch.
                            let dc = DummyChildren { len: 2 };
                            let last_branch = enumerator(&dc, 1);
                            let mid_branch = enumerator(&dc, 0);
                            let look_for = format!("{}{}", child_prefix, last_branch);
                            if let Some(pos) = child_output.rfind(&look_for) {
                                // Ensure we are at line start; find preceding newline or start
                                let line_start =
                                    child_output[..pos].rfind('\n').map(|p| p + 1).unwrap_or(0);
                                if line_start == pos {
                                    child_output.replace_range(
                                        pos..pos + look_for.len(),
                                        &format!("{}{}", child_prefix, mid_branch),
                                    );
                                }
                            }
                        }
                    }
                    if !child_output.is_empty() {
                        strs.push(child_output);
                    }
                }
            }
        }

        strs.join("\n")
    }
}

impl Default for Renderer {
    fn default() -> Self {
        Self::new()
    }
}

/// Creates a new renderer with default configuration.
///
/// This is a convenience function equivalent to `Renderer::new()`.
/// Provided for API compatibility with other lipgloss libraries.
///
/// # Returns
///
/// A new `Renderer` instance with default settings
///
/// # Examples
///
/// ```rust
/// use lipgloss_tree::renderer::new_renderer;
///
/// let renderer = new_renderer();
/// ```
pub fn new_renderer() -> Renderer {
    Renderer::new()
}