Skip to content

Commit 647aa25

Browse files
committed
Fix TreeControl highlight width when node text contains Spectre markup
Change measurement cache from StripAnsiStringLength to StripSpectreLength so markup tags like [yellow] are excluded from visible width calculation. Escape expand indicators [-]/[+] as [[-]]/[[+]] so they coexist with Spectre markup in node text without causing parse failures.
1 parent 61a5039 commit 647aa25

2 files changed

Lines changed: 5 additions & 4 deletions

File tree

SharpConsoleUI/Controls/TreeControl/TreeControl.Rendering.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public override LayoutSize MeasureDOM(LayoutConstraints constraints)
101101
bool[] ancestorIsLast = GetAncestorIsLastArray(node);
102102
string prefix = BuildTreePrefix(depth, ancestorIsLast, guideChars);
103103
string displayText = node.Text ?? string.Empty;
104-
string expandIndicator = node.Children.Count > 0 ? "[-] " : "";
104+
string expandIndicator = node.Children.Count > 0 ? "[[-]] " : "";
105105
int itemWidth = GetCachedTextLength(prefix + expandIndicator + displayText);
106106
if (itemWidth > maxItemWidth) maxItemWidth = itemWidth;
107107
}
@@ -238,7 +238,8 @@ public override void PaintDOM(CharacterBuffer buffer, LayoutRect bounds, LayoutR
238238
}
239239

240240
// Add expand/collapse indicator on the left side
241-
string expandIndicator = node.Children.Count > 0 ? (node.IsExpanded ? "[-] " : "[+] ") : "";
241+
// Escape brackets so Spectre markup in displayText (e.g. colored badges) is processed correctly
242+
string expandIndicator = node.Children.Count > 0 ? (node.IsExpanded ? "[[-]] " : "[[+]] ") : "";
242243

243244
// Build full node text
244245
string nodeText = prefix + expandIndicator + displayText;

SharpConsoleUI/Controls/TreeControl/TreeControl.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public partial class TreeControl : BaseControl, IInteractiveControl, IFocusableC
4747
private int _lastClickIndex = -1;
4848

4949
// Performance: Cache for expensive text measurement operations
50-
private readonly TextMeasurementCache _textMeasurementCache = new(AnsiConsoleHelper.StripAnsiStringLength);
50+
private readonly TextMeasurementCache _textMeasurementCache = new(AnsiConsoleHelper.StripSpectreLength);
5151

5252
// Read-only helpers
5353
private int CurrentSelectedIndex => _selectedIndex;
@@ -83,7 +83,7 @@ public override int? ContentWidth
8383
bool[] ancestorIsLast = GetAncestorIsLastArray(node);
8484
string prefix = BuildTreePrefix(depth, ancestorIsLast, guideChars);
8585
string displayText = node.Text ?? string.Empty;
86-
string expandIndicator = node.Children.Count > 0 ? " [-]" : "";
86+
string expandIndicator = node.Children.Count > 0 ? " [[-]]" : "";
8787
int length = GetCachedTextLength(prefix + displayText + expandIndicator);
8888
if (length > maxLength) maxLength = length;
8989
}

0 commit comments

Comments
 (0)