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
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
//! Tree widget for rendering hierarchical structures with guide characters.
//!
use crate::cells::cell_len;
use crate::console::{Console, ConsoleOptions, Renderable, RenderableArc};
use crate::measure::Measurement;
use crate::segment::Segment;
use crate::style::{Style, StyleStack};
// Text is only used in the #[cfg(test)] module below.
#[cfg(test)]
use crate::text::Text;
// ---------------------------------------------------------------------------
// Guide character constants
// ---------------------------------------------------------------------------
/// Indices into guide character arrays.
const SPACE: usize = 0;
const CONTINUE: usize = 1;
const FORK: usize = 2;
const END: usize = 3;
/// ASCII guide characters: (space, continue, fork, end).
const ASCII_GUIDES: [&str; 4] = [" ", "| ", "+-- ", "`-- "];
/// Unicode guide sets: thin, bold, double.
const TREE_GUIDES: [[&str; 4]; 3] = [
[
" ",
"\u{2502} ",
"\u{251c}\u{2500}\u{2500} ",
"\u{2514}\u{2500}\u{2500} ",
], // thin
[
" ",
"\u{2503} ",
"\u{2523}\u{2501}\u{2501} ",
"\u{2517}\u{2501}\u{2501} ",
], // bold
[
" ",
"\u{2551} ",
"\u{2560}\u{2550}\u{2550} ",
"\u{255a}\u{2550}\u{2550} ",
], // double
];
// ---------------------------------------------------------------------------
// Helper: create a guide segment
// ---------------------------------------------------------------------------
/// Create a guide segment.
///
/// `legacy_windows` forces ASCII guides when true (P2 parity, finding #3).
fn make_guide(index: usize, style: &Style, ascii_only: bool, legacy_windows: bool) -> Segment {
if ascii_only || legacy_windows {
Segment::styled(ASCII_GUIDES[index], style.clone())
} else {
let guide_set = if style.bold() == Some(true) {
1
} else if style.underline2() == Some(true) {
2
} else {
0
};
Segment::styled(TREE_GUIDES[guide_set][index], style.clone())
}
}
// ---------------------------------------------------------------------------
// Tree
// ---------------------------------------------------------------------------
/// A tree widget that renders a hierarchical structure with guide characters.
#[derive(Clone)]
pub struct Tree {
/// The node's label — any renderable widget (Text, Panel, Rule, …).
pub label: RenderableArc,
/// Node style.
pub style: Style,
/// Guide line style.
pub guide_style: Style,
/// Child nodes.
pub children: Vec<Tree>,
/// Whether to show children.
pub expanded: bool,
/// Whether to hide the root node.
pub hide_root: bool,
/// Whether to highlight labels (P2 parity, finding #5). Default false.
pub highlight: bool,
}
// Manual Debug — RenderableArc (Arc<dyn Renderable + Send + Sync>) doesn't
// implement Debug, so we print a placeholder for the label field.
impl std::fmt::Debug for Tree {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Tree")
.field("label", &"<renderable>")
.field("style", &self.style)
.field("guide_style", &self.guide_style)
.field("children", &self.children)
.field("expanded", &self.expanded)
.field("hide_root", &self.hide_root)
.field("highlight", &self.highlight)
.finish()
}
}
impl Tree {
/// Create a new tree node with the given label.
///
/// Accepts any type that implements [`Renderable`] — [`Text`], [`Panel`],
/// [`Rule`], another [`Tree`], etc. The value is stored as a
/// [`RenderableArc`] (reference-counted, cheaply cloned).
pub fn new(label: impl Renderable + Send + Sync + 'static) -> Self {
Tree {
label: std::sync::Arc::new(label),
style: Style::null(),
guide_style: Style::null(),
children: Vec::new(),
expanded: true,
hide_root: false,
highlight: false,
}
}
/// Add a child node and return a mutable reference to it.
///
/// Accepts any type that implements [`Renderable`] as the label.
/// The child inherits `style`, `guide_style`, and `highlight` from the parent,
/// and defaults to `expanded: true`.
///
/// To override any of these per-node, use [`add_with`](Self::add_with).
pub fn add(&mut self, label: impl Renderable + Send + Sync + 'static) -> &mut Tree {
self.children.push(Tree {
label: std::sync::Arc::new(label),
style: self.style.clone(),
guide_style: self.guide_style.clone(),
children: Vec::new(),
expanded: true,
hide_root: false,
highlight: self.highlight,
});
self.children
.last_mut()
.expect("children is non-empty after push")
}
/// Add a child node with per-node style overrides.
///
/// Rich parity: Python's `Tree.add(label, *, style, guide_style, expand, highlight)`.
///
/// Each `Option` parameter:
/// - `Some(value)` — use the given value for this node.
/// - `None` — inherit from the parent (same behaviour as [`add`](Self::add)).
///
/// For `expanded`: `None` inherits from parent (default true unless parent is
/// collapsed).
///
/// # Examples
///
/// ```
/// use gilt::tree::Tree;
/// use gilt::style::Style;
/// use gilt::text::Text;
///
/// let mut tree = Tree::new(Text::new("root", Style::null()))
/// .with_style(Style::parse("bold"))
/// .with_guide_style(Style::parse("red"));
///
/// // Child uses its own style, inherits guide_style and highlight from parent.
/// tree.add_with(
/// Text::new("child", Style::null()),
/// Some(Style::parse("italic")), // override style
/// None, // inherit guide_style ("red")
/// Some(false), // collapsed
/// None, // inherit highlight
/// );
/// ```
pub fn add_with(
&mut self,
label: impl Renderable + Send + Sync + 'static,
style: Option<Style>,
guide_style: Option<Style>,
expanded: Option<bool>,
highlight: Option<bool>,
) -> &mut Tree {
self.children.push(Tree {
label: std::sync::Arc::new(label),
style: style.unwrap_or_else(|| self.style.clone()),
guide_style: guide_style.unwrap_or_else(|| self.guide_style.clone()),
children: Vec::new(),
expanded: expanded.unwrap_or(self.expanded),
hide_root: false,
highlight: highlight.unwrap_or(self.highlight),
});
self.children
.last_mut()
.expect("children is non-empty after push")
}
/// Set the node style (builder pattern).
#[must_use]
pub fn with_style(mut self, style: Style) -> Self {
self.style = style;
self
}
/// Set the guide style (builder pattern).
#[must_use]
pub fn with_guide_style(mut self, style: Style) -> Self {
self.guide_style = style;
self
}
/// Set whether the tree is expanded (builder pattern).
#[must_use]
pub fn with_expanded(mut self, expanded: bool) -> Self {
self.expanded = expanded;
self
}
/// Set whether the root node is hidden (builder pattern).
#[must_use]
pub fn with_hide_root(mut self, hide_root: bool) -> Self {
self.hide_root = hide_root;
self
}
/// Set whether to highlight labels (builder pattern, P2 parity finding #5).
#[must_use]
pub fn with_highlight(mut self, highlight: bool) -> Self {
self.highlight = highlight;
self
}
/// Measure this tree: compute minimum and maximum widths.
///
/// Uses `Renderable::gilt_measure` on each node's label so that any widget
/// type (Text, Panel, Rule, …) is measured correctly (P1 parity, finding #1).
pub fn measure(&self, console: &Console, options: &ConsoleOptions) -> Measurement {
let mut minimum: usize = 0;
let mut maximum: usize = 0;
fn measure_recursive(
tree: &Tree,
level: usize,
min: &mut usize,
max: &mut usize,
hide_root: bool,
console: &Console,
options: &ConsoleOptions,
) {
let effective_level = if hide_root {
level.saturating_sub(1)
} else {
level
};
let indent = effective_level * 4;
// Use gilt_measure on the RenderableArc so any widget type is measured.
let label_m = tree.label.gilt_measure(console, options);
if !(level == 0 && hide_root) {
*min = (*min).max(label_m.minimum + indent);
*max = (*max).max(label_m.maximum + indent);
}
if tree.expanded {
for child in &tree.children {
measure_recursive(child, level + 1, min, max, hide_root, console, options);
}
}
}
measure_recursive(
self,
0,
&mut minimum,
&mut maximum,
self.hide_root,
console,
options,
);
Measurement::new(minimum, maximum)
}
}
// ---------------------------------------------------------------------------
// Renderable
// ---------------------------------------------------------------------------
/// Stack frame for iterative DFS traversal.
struct StackFrame<'a> {
/// Iterator position within the children list.
index: usize,
/// The children being iterated.
children: &'a [Tree],
}
impl Renderable for Tree {
fn gilt_measure(&self, console: &Console, options: &ConsoleOptions) -> Measurement {
self.measure(console, options)
}
fn gilt_console(&self, console: &Console, options: &ConsoleOptions) -> Vec<Segment> {
let mut segments: Vec<Segment> = Vec::new();
let ascii_only = options.ascii_only();
// P2 parity (finding #3): legacy_windows forces ASCII guide characters.
let legacy_windows = options.legacy_windows;
let newline = Segment::line();
// P3 parity (Item 4): resolve default theme styles for the root node.
// If the root has a null style, fall back to the console's "tree" and
// "tree.line" theme entries. Use local variables — do NOT mutate self.
let root_style = if self.style.is_null() {
console.get_style("tree").unwrap_or_else(|_| Style::null())
} else {
self.style.clone()
};
let root_guide_style = if self.guide_style.is_null() {
console
.get_style("tree.line")
.unwrap_or_else(|_| Style::null())
} else {
self.guide_style.clone()
};
// Stack-based DFS (porting Python's stack/iterator approach).
//
// `levels` holds the guide segment for each depth level.
// The stack holds iterators over children at each level.
let mut levels: Vec<Segment> = vec![make_guide(
CONTINUE,
&root_guide_style,
ascii_only,
legacy_windows,
)];
let mut stack: Vec<StackFrame> = Vec::new();
// --- Style stacks (rich parity: ancestor style tints subtree labels) ---
// `style_stack` accumulates combined node styles top-down so that each
// label is rendered with the fully-resolved ancestor style.
// `guide_style_stack` accumulates guide styles the same way.
let mut style_stack = StyleStack::new(root_style);
let mut guide_style_stack = StyleStack::new(root_guide_style);
// Push the root as a single-element "children" iterator.
let root_slice = std::slice::from_ref(self);
stack.push(StackFrame {
index: 0,
children: root_slice,
});
let mut depth: usize = 0;
while let Some(frame) = stack.last_mut() {
if frame.index >= frame.children.len() {
// This level is exhausted — ascend.
stack.pop();
levels.pop();
if !levels.is_empty() {
let last_idx = levels.len() - 1;
let guide_style = levels[last_idx].style.clone().unwrap_or_else(Style::null);
levels[last_idx] = make_guide(FORK, &guide_style, ascii_only, legacy_windows);
// Pop the style stacks to return to the parent's context.
let _ = guide_style_stack.pop();
let _ = style_stack.pop();
}
depth = depth.saturating_sub(1);
continue;
}
let child_idx = frame.index;
let total = frame.children.len();
let last = child_idx == total - 1;
let node = &frame.children[child_idx];
frame.index += 1;
if last {
let last_level = levels.len() - 1;
let guide_style = levels[last_level].style.clone().unwrap_or_else(Style::null);
levels[last_level] = make_guide(END, &guide_style, ascii_only, legacy_windows);
}
// Build the prefix from levels, skipping levels for hidden root.
// P3 perf (finding #7): compute prefix_width from the slice without
// allocating a Vec first; build current_prefix only once for actual
// segment emission so there is a single allocation per node.
let skip = if self.hide_root { 2 } else { 1 };
let prefix_slice: &[Segment] = if levels.len() > skip {
&levels[skip..]
} else {
&[]
};
// Accumulated styles from the stacks (rich parity):
// - `node_style`: the fully-resolved style for this node's label
// (ancestor stack + node's own style).
// - `node_guide_style`: resolved guide style for this node.
let node_style = style_stack.current().clone() + node.style.clone();
let node_guide_style = guide_style_stack.current().clone() + node.guide_style.clone();
// Compute available width for the label directly from the slice.
let prefix_width: usize = prefix_slice.iter().map(|s| cell_len(&s.text)).sum();
let child_width = options.max_width.saturating_sub(prefix_width);
// P3 parity (finding #6): pad=true when justify is set.
let pad = options.justify.is_some();
// P2 parity (finding #5): forward highlight flag into options so
// the label renderer can apply syntax highlighting.
let mut child_opts = options.update_width(child_width);
if self.highlight {
child_opts.highlight = Some(true);
}
// Render the label into lines. To apply the accumulated ancestor
// style (rich parity: "Styled(node.label, style)"), we first render
// the label normally, then apply `node_style` over every segment via
// `Segment::apply_style`. We do this line-by-line after the render.
let raw_lines =
console.render_lines(node.label.as_ref(), Some(&child_opts), None, pad, false);
let rendered_lines: Vec<Vec<Segment>> = if node_style.is_null() {
raw_lines
} else {
raw_lines
.into_iter()
.map(|line| Segment::apply_style(&line, Some(node_style.clone()), None))
.collect()
};
// Emit segments (skip if this is the root and hide_root is set).
let skip_node = depth == 0 && self.hide_root;
if !skip_node {
// Guide-prefix styling (rich parity): apply the accumulated
// label style's background to the prefix so the background tints
// the guide characters, and strip the guide-line style as a
// post_style so it does not bleed into label rendering.
let prefix_bg = node_style.background_style();
// `prefix_post` is the `remove_guide_styles` negating style
// (rich parity): it has the same attribute bits SET as the guide
// style, but all values set to false, so any guide-specific
// decorations (bold, italic, underline, …) are turned off in the
// post_style pass rather than being additively layered on.
// fg color, bgcolor, and link are left as None — we negate only
// boolean attribute bits, matching rich's `remove_guide_styles`.
let prefix_post = {
let mut neg = Style::null();
if node_guide_style.bold().is_some() {
neg.set_bold(Some(false));
}
if node_guide_style.dim().is_some() {
neg.set_dim(Some(false));
}
if node_guide_style.italic().is_some() {
neg.set_italic(Some(false));
}
if node_guide_style.underline().is_some() {
neg.set_underline(Some(false));
}
if node_guide_style.blink().is_some() {
neg.set_blink(Some(false));
}
if node_guide_style.reverse().is_some() {
neg.set_reverse(Some(false));
}
if node_guide_style.conceal().is_some() {
neg.set_conceal(Some(false));
}
if node_guide_style.strike().is_some() {
neg.set_strike(Some(false));
}
neg
};
let has_prefix_style = !prefix_bg.is_null() || !prefix_post.is_null();
// Build current_prefix once; mutated after the first line only.
let raw_prefix: Vec<Segment> = prefix_slice.to_vec();
let mut current_prefix: Vec<Segment> = if has_prefix_style {
Segment::apply_style(
&raw_prefix,
Some(prefix_bg.clone()),
Some(prefix_post.clone()),
)
} else {
raw_prefix.clone()
};
for (i, line) in rendered_lines.iter().enumerate() {
// Emit prefix guide segments — extend_from_slice is one
// memcpy + N clones rather than N iterations of push.
segments.extend_from_slice(¤t_prefix);
// Emit line content segments.
segments.extend(line.iter().cloned());
// Emit newline.
segments.push(newline.clone());
// After the first line, change the last prefix element
// from FORK/END to CONTINUE/SPACE for continuation lines.
if i == 0 && !current_prefix.is_empty() {
let last_idx = raw_prefix.len() - 1;
let pstyle = raw_prefix[last_idx]
.style
.clone()
.unwrap_or_else(Style::null);
let cont_seg = make_guide(
if last { SPACE } else { CONTINUE },
&pstyle,
ascii_only,
legacy_windows,
);
let cont_styled = if has_prefix_style {
Segment::apply_style(
std::slice::from_ref(&cont_seg),
Some(prefix_bg.clone()),
Some(prefix_post.clone()),
)
.into_iter()
.next()
.unwrap_or(cont_seg)
} else {
cont_seg
};
let last_cp_idx = current_prefix.len() - 1;
current_prefix[last_cp_idx] = cont_styled;
}
}
}
// Recurse into children if expanded.
if node.expanded && !node.children.is_empty() {
// Update the current level's guide to continuation.
let last_level = levels.len() - 1;
let guide_style = levels[last_level].style.clone().unwrap_or_else(Style::null);
levels[last_level] = make_guide(
if last { SPACE } else { CONTINUE },
&guide_style,
ascii_only,
legacy_windows,
);
// Add a new level for the children.
// Use `node_guide_style` (the fully-resolved guide style from the
// stack) so that theme defaults from `root_guide_style` propagate
// into child-level guide characters.
let child_count = node.children.len();
let guide_type = if child_count == 1 { END } else { FORK };
levels.push(make_guide(
guide_type,
&node_guide_style,
ascii_only,
legacy_windows,
));
// Push the node's styles onto the stacks for the child subtree.
// Use the already-resolved styles (node_style / node_guide_style)
// so that ancestor accumulation carries forward correctly.
style_stack.push(node_style.clone());
guide_style_stack.push(node_guide_style.clone());
stack.push(StackFrame {
index: 0,
children: &node.children,
});
depth += 1;
}
}
segments
}
}
// ---------------------------------------------------------------------------
// Display
// ---------------------------------------------------------------------------
impl std::fmt::Display for Tree {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let mut console = Console::builder()
.width(f.width().unwrap_or(80))
.force_terminal(true)
.no_color(true)
.build();
console.begin_capture();
console.print(self);
let output = console.end_capture();
write!(f, "{}", output.trim_end_matches('\n'))
}
}
// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------
#[cfg(test)]
mod tests {
use super::*;
/// Helper: build a console with a fixed width and no markup/highlight.
fn test_console(width: usize) -> Console {
Console::builder()
.width(width)
.markup(false)
.highlight(false)
.no_color(true)
.build()
}
/// Helper: render a tree to plain text (no ANSI codes).
fn render_tree(tree: &Tree, width: usize) -> String {
let console = test_console(width);
let opts = console.options();
let segments = tree.gilt_console(&console, &opts);
segments
.iter()
.filter(|s| !s.is_control())
.map(|s| s.text.as_str())
.collect()
}
// -- 1. Single node (no children) --
#[test]
fn test_single_node() {
let tree = Tree::new(Text::new("root", Style::null()));
let output = render_tree(&tree, 80);
assert!(output.contains("root"));
// Should have exactly one line.
let lines: Vec<&str> = output.lines().collect();
assert_eq!(lines.len(), 1);
// No guide characters for root.
assert!(!output.contains("\u{251c}"));
assert!(!output.contains("\u{2514}"));
}
// -- 2. Node with one child --
#[test]
fn test_one_child() {
let mut tree = Tree::new(Text::new("root", Style::null()));
tree.add(Text::new("child", Style::null()));
let output = render_tree(&tree, 80);
let lines: Vec<&str> = output.lines().collect();
assert_eq!(lines.len(), 2);
assert!(lines[0].contains("root"));
assert!(lines[1].contains("child"));
// Single child should use END guide.
assert!(output.contains("\u{2514}\u{2500}\u{2500}"));
}
// -- 3. Node with multiple children --
#[test]
fn test_multiple_children() {
let mut tree = Tree::new(Text::new("root", Style::null()));
tree.add(Text::new("child1", Style::null()));
tree.add(Text::new("child2", Style::null()));
tree.add(Text::new("child3", Style::null()));
let output = render_tree(&tree, 80);
let lines: Vec<&str> = output.lines().collect();
assert_eq!(lines.len(), 4);
assert!(lines[0].contains("root"));
// First two children use FORK, last uses END.
assert!(lines[1].contains("\u{251c}\u{2500}\u{2500}"));
assert!(lines[2].contains("\u{251c}\u{2500}\u{2500}"));
assert!(lines[3].contains("\u{2514}\u{2500}\u{2500}"));
}
// -- 4. Nested children (grandchildren) --
#[test]
fn test_nested_children() {
let mut tree = Tree::new(Text::new("root", Style::null()));
let child = tree.add(Text::new("child", Style::null()));
child
.children
.push(Tree::new(Text::new("grandchild", Style::null())));
let output = render_tree(&tree, 80);
let lines: Vec<&str> = output.lines().collect();
assert_eq!(lines.len(), 3);
assert!(lines[0].contains("root"));
assert!(lines[1].contains("child"));
assert!(lines[2].contains("grandchild"));
}
// -- 5. hide_root option --
#[test]
fn test_hide_root() {
let mut tree = Tree::new(Text::new("root", Style::null())).with_hide_root(true);
tree.add(Text::new("child1", Style::null()));
tree.add(Text::new("child2", Style::null()));
let output = render_tree(&tree, 80);
// Root should not appear.
assert!(!output.contains("root"));
let lines: Vec<&str> = output.lines().collect();
assert_eq!(lines.len(), 2);
assert!(lines[0].contains("child1"));
assert!(lines[1].contains("child2"));
}
// -- 6. Collapsed node (expanded=false) --
#[test]
fn test_collapsed_node() {
let mut tree = Tree::new(Text::new("root", Style::null()));
tree.children
.push(Tree::new(Text::new("branch", Style::null())).with_expanded(false));
// Add a child to the branch that should NOT be rendered.
tree.children[0]
.children
.push(Tree::new(Text::new("hidden", Style::null())));
let output = render_tree(&tree, 80);
assert!(output.contains("branch"));
assert!(!output.contains("hidden"));
}
// -- 7. ASCII mode guides --
#[test]
fn test_ascii_mode() {
let mut tree = Tree::new(Text::new("root", Style::null()));
tree.add(Text::new("child1", Style::null()));
tree.add(Text::new("child2", Style::null()));
let console = Console::builder()
.width(80)
.markup(false)
.highlight(false)
.no_color(true)
.build();
let mut opts = console.options();
opts.encoding = std::borrow::Cow::Borrowed("ascii");
let segments = tree.gilt_console(&console, &opts);
let output: String = segments
.iter()
.filter(|s| !s.is_control())
.map(|s| s.text.as_str())
.collect();
assert!(output.contains("+-- "));
assert!(output.contains("`-- "));
// Should not contain Unicode guide chars.
assert!(!output.contains("\u{251c}"));
assert!(!output.contains("\u{2514}"));
}
// -- 8. Guide character correctness --
#[test]
fn test_guide_characters() {
let mut tree = Tree::new(Text::new("root", Style::null()));
tree.add(Text::new("a", Style::null()));
tree.add(Text::new("b", Style::null()));
let output = render_tree(&tree, 80);
let lines: Vec<&str> = output.lines().collect();
// "a" line should have FORK guide (not last child).
assert!(lines[1].starts_with("\u{251c}\u{2500}\u{2500} "));
// "b" line should have END guide (last child).
assert!(lines[2].starts_with("\u{2514}\u{2500}\u{2500} "));
}
// -- 9. Multi-line label --
#[test]
fn test_multiline_label() {
// Force wrapping by using a narrow width.
let mut tree = Tree::new(Text::new("root", Style::null()));
tree.add(Text::new(
"This is a very long label that should wrap",
Style::null(),
));
let output = render_tree(&tree, 20);
let lines: Vec<&str> = output.lines().collect();
// Should have more than 2 lines due to wrapping.
assert!(lines.len() > 2);
// First continuation line of child should use SPACE (since it's the last child).
// The first line has the END guide, continuation lines have SPACE guide.
}
// -- 10. Measure --
#[test]
fn test_measure() {
let mut tree = Tree::new(Text::new("root", Style::null()));
tree.add(Text::new("child", Style::null()));
let console = test_console(80);
let opts = console.options();
let measurement = tree.measure(&console, &opts);
// root: 4 cells, child: 5 + 4 indent = 9 cells.
assert_eq!(measurement.minimum, 9);
assert_eq!(measurement.maximum, 9);
}
// -- 11. Builder pattern --
#[test]
fn test_builder_pattern() {
let style = Style::parse("bold");
let guide_style = Style::parse("red");
let tree = Tree::new(Text::new("root", Style::null()))
.with_style(style.clone())
.with_guide_style(guide_style.clone())
.with_expanded(false)
.with_hide_root(true);
assert_eq!(tree.style, style);
assert_eq!(tree.guide_style, guide_style);
assert!(!tree.expanded);
assert!(tree.hide_root);
}
// -- 12. add() returns mutable ref to child --
#[test]
fn test_add_returns_mut_ref() {
let mut tree = Tree::new(Text::new("root", Style::null()));
let child = tree.add(Text::new("child", Style::null()));
// We can modify the child through the returned ref.
child
.children
.push(Tree::new(Text::new("grandchild", Style::null())));
assert_eq!(tree.children.len(), 1);
assert_eq!(tree.children[0].children.len(), 1);
// label is now RenderableArc — verify the grandchild renders its text.
let console = test_console(80);
let opts = console.options();
let segs = tree.children[0].children[0]
.label
.gilt_console(&console, &opts);
let text: String = segs
.iter()
.filter(|s| !s.is_control())
.map(|s| s.text.as_str())
.collect();
assert!(
text.contains("grandchild"),
"grandchild label should render its text: {:?}",
text
);
}
// -- 13. Deep nesting (3+ levels) --
#[test]
fn test_deep_nesting() {
let mut tree = Tree::new(Text::new("L0", Style::null()));
let l1 = tree.add(Text::new("L1", Style::null()));
l1.children.push(Tree::new(Text::new("L2", Style::null())));
l1.children[0]
.children
.push(Tree::new(Text::new("L3", Style::null())));
let output = render_tree(&tree, 80);
let lines: Vec<&str> = output.lines().collect();
assert_eq!(lines.len(), 4);
assert!(lines[0].contains("L0"));
assert!(lines[1].contains("L1"));
assert!(lines[2].contains("L2"));
assert!(lines[3].contains("L3"));
// Verify increasing indentation.
for i in 1..lines.len() {
let current_content_start = lines[i].find('L').unwrap_or(0);
let prev_content_start = lines[i - 1].find('L').unwrap_or(0);
assert!(
current_content_start > prev_content_start,
"L{} should be indented more than L{}",
i,
i - 1
);
}
}
// -- 14. add() inherits style and guide_style --
#[test]
fn test_add_inherits_styles() {
let style = Style::parse("bold");
let guide_style = Style::parse("red");
let mut tree = Tree::new(Text::new("root", Style::null()))
.with_style(style.clone())
.with_guide_style(guide_style.clone());
tree.add(Text::new("child", Style::null()));
assert_eq!(tree.children[0].style, style);
assert_eq!(tree.children[0].guide_style, guide_style);
}
// -- 15. Empty tree (root only, no children) renders single line --
#[test]
fn test_empty_tree_no_guides() {
let tree = Tree::new(Text::new("alone", Style::null()));
let output = render_tree(&tree, 80);
let lines: Vec<&str> = output.lines().collect();
assert_eq!(lines.len(), 1);
assert_eq!(lines[0].trim(), "alone");
}
// -- 16. Multiple children with nesting shows correct continuation --
#[test]
fn test_continuation_guides() {
let mut tree = Tree::new(Text::new("root", Style::null()));
{
let child1 = tree.add(Text::new("child1", Style::null()));
child1
.children
.push(Tree::new(Text::new("grandchild1", Style::null())));
}
tree.add(Text::new("child2", Style::null()));
let output = render_tree(&tree, 80);
let lines: Vec<&str> = output.lines().collect();
assert_eq!(lines.len(), 4);
// The grandchild line should have CONTINUE (|) guide from the parent level
// because child1 is not the last child (child2 follows).
assert!(
lines[2].contains("\u{2502}"),
"grandchild1 line should contain continue guide: {:?}",
lines[2]
);
}
// -- 17. hide_root with deep nesting --
#[test]
fn test_hide_root_deep() {
let mut tree = Tree::new(Text::new("ROOT", Style::null())).with_hide_root(true);
let child = tree.add(Text::new("child", Style::null()));
child
.children
.push(Tree::new(Text::new("grandchild", Style::null())));
let output = render_tree(&tree, 80);
assert!(!output.contains("ROOT"));
assert!(output.contains("child"));
assert!(output.contains("grandchild"));
}
// -- 18. Measure with hide_root --
#[test]
fn test_measure_hide_root() {
let mut tree = Tree::new(Text::new("LONG_ROOT_NAME", Style::null())).with_hide_root(true);
tree.add(Text::new("short", Style::null()));
let console = test_console(80);
let opts = console.options();
let measurement = tree.measure(&console, &opts);
// With hide_root: root is excluded from measurement.
// child: "short" (5) + 0 indent (since root is hidden, child is at effective level 0).
assert_eq!(measurement.minimum, 5);
}
// -- 19. Measure deep nesting --
#[test]
fn test_measure_deep() {
let mut tree = Tree::new(Text::new("r", Style::null()));
let c = tree.add(Text::new("cc", Style::null()));
c.children.push(Tree::new(Text::new("ggg", Style::null())));
let console = test_console(80);
let opts = console.options();
let measurement = tree.measure(&console, &opts);
// r: 1 + 0 = 1
// cc: 2 + 4 = 6
// ggg: 3 + 8 = 11
assert_eq!(measurement.maximum, 11);
}
// -- 20. Collapsed subtree not measured --
#[test]
fn test_measure_collapsed() {
let mut tree = Tree::new(Text::new("r", Style::null()));
let mut branch = Tree::new(Text::new("branch", Style::null())).with_expanded(false);
branch.children.push(Tree::new(Text::new(
"very_very_very_long_hidden_label",
Style::null(),
)));
tree.children.push(branch);
let console = test_console(80);
let opts = console.options();
let measurement = tree.measure(&console, &opts);
// The hidden label should not affect measurement.
// r: 1, branch: 6 + 4 = 10.
assert_eq!(measurement.maximum, 10);
}
// -- 21. Guide style selects bold guide set --
#[test]
fn test_bold_guide_style() {
let mut tree =
Tree::new(Text::new("root", Style::null())).with_guide_style(Style::parse("bold"));
tree.add(Text::new("child1", Style::null()));
tree.add(Text::new("child2", Style::null()));
let output = render_tree(&tree, 80);
// Bold guides: FORK = \u{2523}\u{2501}\u{2501}, END = \u{2517}\u{2501}\u{2501}
assert!(output.contains("\u{2523}\u{2501}\u{2501}"));
assert!(output.contains("\u{2517}\u{2501}\u{2501}"));
}
// -- 22. Guide style selects double guide set --
#[test]
fn test_double_guide_style() {
let mut tree = Tree::new(Text::new("root", Style::null()))
.with_guide_style(Style::parse("underline2"));
tree.add(Text::new("child1", Style::null()));
tree.add(Text::new("child2", Style::null()));
let output = render_tree(&tree, 80);
// Double guides: FORK = \u{2560}\u{2550}\u{2550}, END = \u{255a}\u{2550}\u{2550}
assert!(output.contains("\u{2560}\u{2550}\u{2550}"));
assert!(output.contains("\u{255a}\u{2550}\u{2550}"));
}
// -- 23. Guide characters are exactly 4 cells wide --
#[test]
fn test_guide_width() {
for guide in &ASCII_GUIDES {
assert_eq!(cell_len(guide), 4, "ASCII guide {:?} is not 4 cells", guide);
}
for set in &TREE_GUIDES {
for guide in set {
assert_eq!(
cell_len(guide),
4,
"Unicode guide {:?} is not 4 cells",
guide
);
}
}
}
// -- 24. Render produces Segment::line() newlines --
#[test]
fn test_segments_contain_newlines() {
let mut tree = Tree::new(Text::new("root", Style::null()));
tree.add(Text::new("child", Style::null()));
let console = test_console(80);
let opts = console.options();
let segments = tree.gilt_console(&console, &opts);
let newline_count = segments.iter().filter(|s| s.text == "\n").count();
assert_eq!(newline_count, 2, "Expected 2 newlines (one per line)");
}
// -- 25. hide_root with no children produces no output --
#[test]
fn test_hide_root_no_children() {
let tree = Tree::new(Text::new("hidden", Style::null())).with_hide_root(true);
let output = render_tree(&tree, 80);
assert!(
output.trim().is_empty(),
"hide_root with no children should produce empty output"
);
}
#[test]
fn test_display_trait() {
let mut tree = Tree::new(Text::new("root", Style::null()));
tree.add(Text::new("child1", Style::null()));
tree.add(Text::new("child2", Style::null()));
let s = format!("{}", tree);
assert!(!s.is_empty());
assert!(s.contains("root"));
assert!(s.contains("child1"));
assert!(s.contains("child2"));
}
// -- CJK / emoji content tests ------------------------------------------
#[test]
fn test_tree_cjk_labels() {
let mut tree = Tree::new(Text::new("根", Style::null()));
tree.add(Text::new("子ノード一", Style::null()));
tree.add(Text::new("子ノード二", Style::null()));
let output = render_tree(&tree, 40);
assert!(output.contains("根"));
assert!(output.contains("子ノード一"));
assert!(output.contains("子ノード二"));
}
#[test]
fn test_tree_emoji_labels() {
let mut tree = Tree::new(Text::new("🌳 Root", Style::null()));
tree.add(Text::new("🍎 Apple", Style::null()));
tree.add(Text::new("🍊 Orange", Style::null()));
let output = render_tree(&tree, 40);
assert!(output.contains("🌳"));
assert!(output.contains("🍎"));
assert!(output.contains("🍊"));
}
// -- Deep nesting test --------------------------------------------------
#[test]
fn test_tree_deep_nesting() {
// Build a tree 50 levels deep — should not stack overflow.
// Each guide is 4 cells wide, so 50 levels needs ~200 cells of guides
// plus room for labels. Use width=300 to ensure labels fit.
let mut root = Tree::new(Text::new("level_0", Style::null()));
let mut current = &mut root;
for i in 1..50 {
current = current.add(Text::new(&format!("level_{}", i), Style::null()));
}
let output = render_tree(&root, 300);
assert!(output.contains("level_0"));
assert!(output.contains("level_49"));
}
// -- Extreme width boundary tests ---------------------------------------
#[test]
fn test_tree_width_one() {
let mut tree = Tree::new(Text::new("root", Style::null()));
tree.add(Text::new("child", Style::null()));
// Should not panic at width=1
let _output = render_tree(&tree, 1);
}
// -- Task 1 new tests: ancestor style-stack + guide-prefix styling ------
/// A styled parent should tint its child label: the child's rendered
/// segments must carry a style that includes the parent's bold attribute.
#[test]
fn test_styled_parent_tints_child_label() {
// Build a console that preserves ANSI styles (force_terminal so that
// color/bold is not stripped to plain text).
let console = Console::builder()
.width(80)
.markup(false)
.highlight(false)
.force_terminal(true)
.build();
let opts = console.options();
let parent_style = Style::parse("bold");
let mut tree = Tree::new(Text::new("root", Style::null())).with_style(parent_style.clone());
tree.add(Text::new("child", Style::null()));
let segments = tree.gilt_console(&console, &opts);
// Collect non-control, non-newline segments that contain "child".
let child_segments: Vec<&Segment> = segments
.iter()
.filter(|s| !s.is_control() && s.text.contains("child"))
.collect();
// At least one segment carrying "child" must have a bold style
// (inherited from the parent's accumulated style stack).
let has_bold = child_segments.iter().any(|s| {
s.style
.as_ref()
.map(|st| st.bold() == Some(true))
.unwrap_or(false)
});
assert!(
has_bold,
"Child label segment should carry parent's bold style; segments = {:?}",
child_segments
);
}
/// Guide prefix segments should carry the resolved guide style so that
/// a red guide style shows up on the prefix characters.
#[test]
fn test_guide_prefix_carries_guide_style() {
let console = Console::builder()
.width(80)
.markup(false)
.highlight(false)
.force_terminal(true)
.build();
let opts = console.options();
// A tree with a red guide style.
let guide_style = Style::parse("red");
let mut tree =
Tree::new(Text::new("root", Style::null())).with_guide_style(guide_style.clone());
tree.add(Text::new("child", Style::null()));
let segments = tree.gilt_console(&console, &opts);
// Guide prefix segments are non-control segments that contain guide
// characters (e.g. "└── " or "+-- ") and should carry a red style.
let guide_segs: Vec<&Segment> = segments
.iter()
.filter(|s| {
!s.is_control()
&& (s.text.contains('\u{2514}') // └
|| s.text.contains('\u{251c}') // ├
|| s.text.contains("`-- ")
|| s.text.contains("+-- "))
})
.collect();
assert!(
!guide_segs.is_empty(),
"Expected at least one guide prefix segment in the output"
);
// Every guide segment must carry a non-None style (the guide style
// applies background/foreground color from the stacked guide style).
// After apply_style the guide segments get the red color from the
// label style's background (or guide style post-application).
// We check that style is present (non-null after apply_style).
for seg in &guide_segs {
let style_present = seg.style.as_ref().map(|s| !s.is_null()).unwrap_or(false);
// The guide segments were built with the guide_style (red), so the
// segment style should be Some and non-null.
assert!(
style_present,
"Guide prefix segment {:?} should have a non-null style",
seg
);
}
}
/// Ancestor guide style accumulates: a child's guide_style combines with
/// the parent's guide_style for nested levels.
#[test]
fn test_guide_style_stack_accumulation() {
let console = Console::builder()
.width(80)
.markup(false)
.highlight(false)
.force_terminal(true)
.build();
let opts = console.options();
let root_guide = Style::parse("bold");
let mut tree =
Tree::new(Text::new("root", Style::null())).with_guide_style(root_guide.clone());
let child = tree.add(Text::new("child", Style::null()));
child
.children
.push(Tree::new(Text::new("grandchild", Style::null())));
// Should not panic, and should render all three nodes.
let segments = tree.gilt_console(&console, &opts);
let text: String = segments
.iter()
.filter(|s| !s.is_control())
.map(|s| s.text.as_str())
.collect();
assert!(text.contains("root"));
assert!(text.contains("child"));
assert!(text.contains("grandchild"));
}
// -- gilt_measure override ------------------------------------------
#[test]
fn tree_gilt_measure_matches_standalone() {
let console = test_console(80);
let opts = console.options();
let mut tree = Tree::new(Text::new("root", Style::null()));
tree.add(Text::new("child node", Style::null()));
assert_eq!(
tree.gilt_measure(&console, &opts),
tree.measure(&console, &opts),
"Tree::gilt_measure must delegate to Tree::measure"
);
}
#[test]
fn tree_gilt_measure_nested_matches_standalone() {
let console = test_console(80);
let opts = console.options();
let mut tree = Tree::new(Text::new("parent", Style::null()));
let child = tree.add(Text::new("child", Style::null()));
child.children.push(Tree::new(Text::new(
"a long grandchild label",
Style::null(),
)));
assert_eq!(
tree.gilt_measure(&console, &opts),
tree.measure(&console, &opts),
"Tree::gilt_measure nested must delegate to Tree::measure"
);
}
// -- Task 4.3: RenderableArc label tests ----------------------------------
/// Tree::new must accept a Panel as the root label (any Renderable).
#[test]
fn tree_new_accepts_panel_label() {
use crate::panel::Panel;
let label = Panel::new(Text::new("root panel", Style::null()));
let tree = Tree::new(label);
let output = render_tree(&tree, 80);
assert!(
output.contains("root panel"),
"Panel label text should appear in tree output: {:?}",
output
);
}
/// tree.add must accept a Rule as the child label.
#[test]
fn tree_add_accepts_rule_label() {
use crate::rule::Rule;
let mut tree = Tree::new(Text::new("root", Style::null()));
tree.add(Rule::with_title("divider"));
let output = render_tree(&tree, 40);
// Rule renders a horizontal line; the tree should not panic and should
// include at least the root label.
assert!(
output.contains("root"),
"Root label should appear in output: {:?}",
output
);
}
/// Debug output for Tree should print "<renderable>" for the label field.
#[test]
fn tree_debug_impl() {
let tree = Tree::new(Text::new("debug_test", Style::null()));
let debug_str = format!("{:?}", tree);
assert!(
debug_str.contains("<renderable>"),
"Debug impl should print '<renderable>' for label: {}",
debug_str
);
assert!(
!debug_str.contains("debug_test"),
"Debug impl should NOT print the label content: {}",
debug_str
);
}
// -- Item 1: add_with() companion method ------------------------------------
/// add_with() with explicit style overrides must use the provided style,
/// not inherit from the parent.
#[test]
fn test_add_with_style_override() {
let parent_style = Style::parse("bold");
let child_style = Style::parse("italic");
let mut tree = Tree::new(Text::new("root", Style::null())).with_style(parent_style.clone());
tree.add_with(
Text::new("child", Style::null()),
Some(child_style.clone()),
None,
None,
None,
);
assert_eq!(
tree.children[0].style, child_style,
"add_with() style override should be used, not inherited from parent"
);
}
/// add_with() with None style must inherit from parent (same as add()).
#[test]
fn test_add_with_style_inherits_when_none() {
let parent_style = Style::parse("bold");
let mut tree = Tree::new(Text::new("root", Style::null())).with_style(parent_style.clone());
tree.add_with(Text::new("child", Style::null()), None, None, None, None);
assert_eq!(
tree.children[0].style, parent_style,
"add_with() with None style should inherit parent's style"
);
}
/// add_with() with explicit guide_style must use the provided guide_style.
#[test]
fn test_add_with_guide_style_override() {
let parent_guide = Style::parse("red");
let child_guide = Style::parse("blue");
let mut tree =
Tree::new(Text::new("root", Style::null())).with_guide_style(parent_guide.clone());
tree.add_with(
Text::new("child", Style::null()),
None,
Some(child_guide.clone()),
None,
None,
);
assert_eq!(
tree.children[0].guide_style, child_guide,
"add_with() guide_style override should be used"
);
}
/// add_with() with explicit expanded=false must not be expanded.
#[test]
fn test_add_with_expanded_override() {
let mut tree = Tree::new(Text::new("root", Style::null()));
tree.add_with(
Text::new("child", Style::null()),
None,
None,
Some(false),
None,
);
assert!(
!tree.children[0].expanded,
"add_with() expanded=false should set node to not expanded"
);
}
/// add_with() with expanded=None must inherit from the parent's expanded state.
#[test]
fn test_add_with_expanded_none_inherits_parent() {
// Parent is expanded (default true) — child should also be expanded.
let mut tree = Tree::new(Text::new("root", Style::null())); // expanded=true
tree.add_with(Text::new("child", Style::null()), None, None, None, None);
assert!(
tree.children[0].expanded,
"add_with() with expanded=None should inherit parent's expanded=true"
);
// Parent is collapsed — child with None should also be collapsed.
let mut collapsed_tree = Tree::new(Text::new("root", Style::null())).with_expanded(false);
collapsed_tree.add_with(Text::new("child", Style::null()), None, None, None, None);
assert!(
!collapsed_tree.children[0].expanded,
"add_with() with expanded=None should inherit parent's expanded=false"
);
}
/// add_with() with highlight=Some(true) must set highlight on the child.
#[test]
fn test_add_with_highlight_override() {
let mut tree = Tree::new(Text::new("root", Style::null())); // parent highlight=false
tree.add_with(
Text::new("child", Style::null()),
None,
None,
None,
Some(true),
);
assert!(
tree.children[0].highlight,
"add_with() highlight=Some(true) should enable highlight"
);
}
/// add_with() with highlight=None must inherit from parent.
#[test]
fn test_add_with_highlight_inherits_when_none() {
let mut tree = Tree::new(Text::new("root", Style::null())).with_highlight(true);
tree.add_with(Text::new("child", Style::null()), None, None, None, None);
assert!(
tree.children[0].highlight,
"add_with() with highlight=None should inherit parent's highlight"
);
}
// -- Item 2: remove_guide_styles — negating style, not additive -------------
/// Guide-style bold must NOT appear on the label segments.
/// When guide_style is "bold", the label segments must not be bold — the
/// negating remove_guide_styles style should strip the bold attribute.
#[test]
fn test_guide_style_bold_not_on_label() {
let console = Console::builder()
.width(80)
.markup(false)
.highlight(false)
.force_terminal(true)
.build();
let opts = console.options();
// Tree with bold guide_style; label has no explicit style.
let mut tree =
Tree::new(Text::new("root", Style::null())).with_guide_style(Style::parse("bold"));
tree.add(Text::new("child", Style::null()));
let segments = tree.gilt_console(&console, &opts);
// Find label segments that contain "child" text.
let child_label_segs: Vec<&Segment> = segments
.iter()
.filter(|s| !s.is_control() && s.text.contains("child"))
.collect();
assert!(
!child_label_segs.is_empty(),
"Expected segments containing 'child'"
);
// None of the label segments should have bold set to true.
let has_bold = child_label_segs.iter().any(|s| {
s.style
.as_ref()
.map(|st| st.bold() == Some(true))
.unwrap_or(false)
});
assert!(
!has_bold,
"Child label segments must NOT be bold when guide_style has bold; \
guide bold should be negated so it doesn't bleed into labels. \
Segments: {:?}",
child_label_segs
);
}
/// The guide-removal negation (`prefix_post`) must actually strip bold from
/// guide/prefix segments after `Segment::apply_style`. This unit-tests the
/// negation logic directly: build a bold segment, apply a negating style that
/// sets bold=false, and confirm the result has bold == Some(false).
#[test]
fn test_guide_removal_negation_applies_to_prefix() {
// A bold guide segment — simulates what make_guide produces when the
// guide_style carries "bold".
let bold_style = Style::parse("bold");
let guide_seg = Segment::styled("\u{2514}\u{2500}\u{2500} ", bold_style.clone());
// Build the negating prefix_post style the same way the renderer does:
// if node_guide_style.bold().is_some() → set bold = Some(false).
let node_guide_style = bold_style.clone();
let mut prefix_post = Style::null();
if node_guide_style.bold().is_some() {
prefix_post.set_bold(Some(false));
}
// Apply the negating style (post_style) to the guide segment.
let negated =
Segment::apply_style(std::slice::from_ref(&guide_seg), None, Some(prefix_post));
assert_eq!(negated.len(), 1);
let result_bold = negated[0].style.as_ref().and_then(|s| s.bold());
assert_eq!(
result_bold,
Some(false),
"After applying the negating prefix_post style, bold must be Some(false) \
(negated off), not Some(true). Result segment: {:?}",
negated[0]
);
}
// -- Item 4: Default tree/tree.line theme styles ----------------------------
/// When tree style is null and a "tree" theme style is installed,
/// the rendered output should reflect that theme style.
#[test]
fn test_default_tree_theme_style_applied() {
use crate::theme::Theme;
use std::collections::HashMap;
// Build a console with a custom theme that maps "tree" -> "bold".
let mut console = Console::builder()
.width(80)
.markup(false)
.highlight(false)
.force_terminal(true)
.build();
// Install "tree" -> "bold" into the theme.
let mut styles = HashMap::new();
styles.insert("tree".to_string(), Style::parse("bold"));
let theme = Theme::new(Some(styles), true);
console.push_theme(theme, true);
// Tree with null style — should pick up theme "tree" as base style.
let tree = Tree::new(Text::new("root", Style::null()));
let opts = console.options();
let segments = tree.gilt_console(&console, &opts);
// The label "root" should appear with bold style from the theme.
let root_segs: Vec<&Segment> = segments
.iter()
.filter(|s| !s.is_control() && s.text.contains("root"))
.collect();
assert!(!root_segs.is_empty(), "Expected segments containing 'root'");
let has_bold = root_segs.iter().any(|s| {
s.style
.as_ref()
.map(|st| st.bold() == Some(true))
.unwrap_or(false)
});
assert!(
has_bold,
"Root label segments should carry bold from the 'tree' theme style; \
segments: {:?}",
root_segs
);
}
/// When tree guide_style is null and "tree.line" theme style is installed,
/// guide segments should carry that theme style.
#[test]
fn test_default_tree_line_theme_style_applied() {
use crate::theme::Theme;
use std::collections::HashMap;
let mut console = Console::builder()
.width(80)
.markup(false)
.highlight(false)
.force_terminal(true)
.build();
// Install "tree.line" -> "red" into the theme.
let mut styles = HashMap::new();
styles.insert("tree.line".to_string(), Style::parse("red"));
let theme = Theme::new(Some(styles), true);
console.push_theme(theme, true);
// Tree with null guide_style — should pick up theme "tree.line".
let mut tree = Tree::new(Text::new("root", Style::null()));
tree.add(Text::new("child", Style::null()));
let opts = console.options();
let segments = tree.gilt_console(&console, &opts);
// Guide segments contain guide chars (like └──).
let guide_segs: Vec<&Segment> = segments
.iter()
.filter(|s| {
!s.is_control()
&& (s.text.contains('\u{2514}')
|| s.text.contains('\u{251c}')
|| s.text.contains('\u{2502}')
|| s.text.contains("`-- ")
|| s.text.contains("+-- "))
})
.collect();
assert!(
!guide_segs.is_empty(),
"Expected at least one guide segment in output"
);
// At least one guide segment should carry a foreground color from the theme.
let has_color = guide_segs.iter().any(|s| {
s.style
.as_ref()
.map(|st| st.color().is_some())
.unwrap_or(false)
});
assert!(
has_color,
"Guide segments should carry a color from the 'tree.line' theme style; \
segments: {:?}",
guide_segs
);
}
}