co-mono/packages/tui/src
nathyong 6e4270a286 tui: only check for emojis in visibleWidth when necessary
The initial render of a session, and any re-draws caused by terminal
resizing are noticeably slow, especially on conversations with 20+
turns and many tool calls.

From profiling with `bun --cpu-prof` (available since bun 1.3.2), the
majority of the rendering (90%) is spent on detection of emojis in the
string-width library, running the expensive `/\p{RGI_Emoji}$/v`
regular expression on every individual grapheme cluster in the entire
scrollback. I believe it essentially expands to a fixed search against
every possible emoji sequence, hence the amount of CPU time spent in it.

This change replaces the `stringWidth` from string-width with a
`graphemeWidth` function that performs a similar check, but avoids
running the `/\p{RGI_Emoji}$/v` regex for emoji detection unless it
contains codepoints that could be emojis.

The `visibleWidth` function also has two more optimisations:
- Short-circuits string length detection for strings that are entirely
  printable ASCII characters
- Adds a cache for non-ASCII segments to avoid recomputing string length
  when resizing
2026-01-02 01:56:11 +01:00
..
components Add setEditorText/getEditorText to hook UI context, improve custom() API 2026-01-01 00:04:56 +01:00
autocomplete.ts TUI: Improve file completion display (fixes #280) 2025-12-22 16:36:42 +01:00
index.ts Add setEditorText/getEditorText to hook UI context, improve custom() API 2026-01-01 00:04:56 +01:00
keys.ts Tree selector improvements: active line highlight and tool filter 2025-12-30 22:42:25 +01:00
terminal-image.ts Fix Ghostty detection inside tmux for inline images (#299) 2025-12-24 12:04:41 +01:00
terminal.ts Add Kitty keyboard protocol support for Shift+Enter and other modifier keys 2025-12-18 19:20:30 +02:00
tui.ts Add global debug key (Shift+Ctrl+D), iterative tree sorting to avoid stack overflow 2025-12-30 22:42:22 +01:00
utils.ts tui: only check for emojis in visibleWidth when necessary 2026-01-02 01:56:11 +01:00