> For the complete documentation index, see [llms.txt](https://www.openclawbook.xyz/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.openclawbook.xyz/en/ch41-tui/41.3-tui-advanced-features.md). Agents can also search and read this documentation through its [MCP server](https://www.openclawbook.xyz/~gitbook/mcp).

# 41.3 TUI Advanced Features

> **Generated by**: Claude Opus 5 (claude-opus-5\[1m]) **Token usage**: Input \~218,000 tokens, Output \~9,000 tokens (this section) **Source baseline**: `v2026.3.9` (`v2026.3.8-170-g665f67726`)

***

§41.2 covered interaction mechanisms. This section covers rendering: **how untrusted text is made safe to display, how Markdown renders in a terminal, and how theming and performance are handled.**

## 41.3.1 Text Sanitization and Safe Rendering

A terminal is not a passive display device. **Text can contain escape sequences that change terminal state** — moving the cursor, altering colors, even (on some terminals) triggering responses.

Model output and channel messages are both untrusted from the terminal's point of view, so the TUI sanitizes before rendering:

* Strip or neutralize control characters and unexpected escape sequences;
* Preserve only the formatting the renderer itself intends to emit.

> This is the terminal counterpart of §19.4.2's envelope escaping: **anywhere untrusted content enters a structured context, prevent it from forging structure.** In a terminal the "structure" is ANSI escape sequences.

One legitimate exception is `src/tui/osc8-hyperlinks.ts`, which emits OSC 8 hyperlink sequences deliberately so file paths and URLs become clickable in supporting terminals.

## 41.3.2 The Markdown Rendering Pipeline

Agent replies are Markdown; terminals render plain text. The pipeline converts one to the other:

```
Markdown source
   ↓ parse
structured nodes (headings, lists, code blocks, emphasis, tables)
   ↓ map to terminal capabilities
ANSI-styled text with wrapping
```

Two constraints make this harder than it looks:

**Width awareness.** Terminal width varies and changes on resize. Wrapping has to be recomputed, and **CJK characters occupy two columns** — a naive character count produces misaligned tables and broken borders.

**Code blocks must not wrap.** Wrapping code destroys its readability and makes copy-paste useless. Code blocks instead scroll horizontally or are truncated with an indicator.

`src/tui/tui-formatters.ts` holds this logic.

## 41.3.3 Tool Execution Visualization

When the agent runs tools, the TUI shows what's happening rather than freezing.

Each tool call renders as a collapsible block: the tool name and key arguments on one line, with output beneath. Long output is truncated with an expand affordance.

That mirrors what §22.5's tool summaries provide on chat channels — the same information, presented for a different surface.

## 41.3.4 Light/Dark Theme Adaptation

`src/tui/theme/` handles this. The problem: a terminal's background may be light or dark, and a color scheme that reads well on one is unreadable on the other.

The TUI detects the terminal background where possible and selects a palette accordingly, falling back to a scheme that works acceptably on both when detection fails.

> **Falling back to "acceptable on both" rather than guessing** is the right default. A wrong guess produces unreadable text; a compromise produces merely suboptimal text.

## 41.3.5 Fuzzy Search and Pickers

`src/tui/tui-overlays.ts` implements overlay pickers — for models, sessions, and commands.

Fuzzy matching matters here because the lists are long: dozens of models, potentially hundreds of sessions. Requiring exact prefixes would make the picker useless.

The picker is also where local commands justify overriding remote ones (§41.2.3): `/model` opening a searchable list is a genuinely better terminal experience than typing a model identifier.

## 41.3.6 Performance Optimization

Three strategies:

**Render only on change.** Terminal repaints are expensive and visually noisy. The TUI diffs state and repaints only what changed.

**Coalesce stream deltas.** Model output arrives in small pieces; repainting per delta would flicker badly. The stream assembler (§41.2.2) batches before rendering — the same instinct as §18.6.2's outbound coalescer, applied to a different surface.

**Bound scrollback.** Session history can grow indefinitely; the TUI keeps a bounded buffer in memory and reads further history from the Gateway on demand rather than holding everything.

***

## Section Summary

1. **Terminals are not passive displays** — untrusted text can carry escape sequences that change terminal state, so the TUI sanitizes before rendering. The terminal counterpart of envelope escaping: **prevent untrusted content from forging structure**, where here the structure is ANSI sequences.
2. **OSC 8 hyperlinks are a deliberate exception**, making paths and URLs clickable in supporting terminals.
3. **Markdown rendering has two hard constraints**: width awareness (**CJK characters occupy two columns**, so naive counting breaks tables) and **code blocks must not wrap** (wrapping destroys readability and copy-paste).
4. **Tool execution renders as collapsible blocks** — the same information as chat-channel tool summaries, presented for a different surface.
5. **Theme detection falls back to "acceptable on both" rather than guessing** — a wrong guess makes text unreadable; a compromise makes it merely suboptimal.
6. **Three performance strategies**: repaint only what changed, coalesce stream deltas before rendering (the same instinct as the outbound coalescer), and bound scrollback with on-demand history fetching.
