#4479: fix(tui): prevent crash when search matches ANSI escape sequences
## Summary
Fixes #4519
Prevents TUI crash when search input partially matches ANSI escape sequences in styled text.
## Changes
- Split text into ANSI and visible parts before highlighting
- Only apply search highlight to visible text portions
- Preserve ANSI escape sequences intact
## Testing
- Tested with colored model names in selector
- Verified search highlighting works without corrupting styles
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
This PR hardens `SearchableSelectList` rendering and highlighting so search highlighting doesn’t corrupt ANSI escape sequences (avoiding TUI crashes when a query partially matches escape codes). It does this by splitting text into ANSI vs visible segments before applying highlights, and by truncating rendered lines using `pi-tui`’s `visibleWidth`/`truncateToWidth` so ANSI styling is preserved.
The changes are localized to `src/tui/components/searchable-select-list.ts` and affect how list item labels/descriptions are highlighted and width-constrained, plus some minor input-handling guards.
<h3>Confidence Score: 4/5</h3>
- This PR is likely safe to merge and addresses a real crash class, with a couple of edge cases worth checking.
- The change is self-contained and improves ANSI-safety in highlighting and truncation, but the ANSI regex is narrow (SGR-only) and may miss other escape sequences, and there’s a small type/logic inconsistency (`items ?? []`) that should be clarified.
- src/tui/components/searchable-select-list.ts
<!-- greptile_other_comments_section -->
<sub>(3/5) Reply to the agent's comments like "Can you suggest a fix for this @greptileai?" or ask follow-up questions!</sub>
<!-- /greptile_comment -->
Most Similar PRs
#6260: fix(tui): prevent width overflow crashes from nested ANSI escape codes
by 0xktn · 2026-02-01
84.9%
#5509: Fix TUI crash when terminal window is too narrow
by omar-khaled-2 · 2026-01-31
82.1%
#17686: fix(memory): support non-ASCII characters in FTS query tokenization
by Phineas1500 · 2026-02-16
74.9%
#8741: fix(tui): allow j/k keys to be typed in search inputs (#7378)
by revenuestack · 2026-02-04
73.8%
#20516: fix(tui): preserve streamed text on finalize for pure text responses
by MisterGuy420 · 2026-02-19
71.8%
#17677: fix(tui): prevent crash when rendered line exceeds terminal width
by Phineas1500 · 2026-02-16
71.6%
#6819: fix(tui): handle unstructured tool results and errors in tool execu...
by TreyDong · 2026-02-02
71.4%
#12913: TUI: truncate lines to fit terminal width on narrow terminals
by kutayilmaaz · 2026-02-09
70.8%
#16733: fix(ui): avoid injected newlines when tool output is hidden
by jp117 · 2026-02-15
70.6%
#15204: fix(ui): preserve angle-bracketed text in chat
by bufordtjustice2918 · 2026-02-13
70.6%