Skip to content

feat: add --color flag for ANSI escape sequence passthrough#2

Merged
onesuper merged 1 commit into
onesuper:mainfrom
phiat:feat/ansi-color-passthrough
Apr 10, 2026
Merged

feat: add --color flag for ANSI escape sequence passthrough#2
onesuper merged 1 commit into
onesuper:mainfrom
phiat:feat/ansi-color-passthrough

Conversation

@phiat

@phiat phiat commented Apr 8, 2026

Copy link
Copy Markdown
Contributor

Problem

tui-use strips all color and style information from terminal output. The README documents this as a known limitation:

TUI color/style info is mostly lost — screen contains plain text only; colors and most formatting are stripped.

This means agents can't distinguish between red error text and green success text, can't detect colored status indicators, and lose visual semantics that many CLI tools rely on to communicate state.

Solution

Add an opt-in --color flag to snapshot and wait that re-encodes xterm buffer cell attributes back into ANSI SGR escape sequences. The xterm emulator already parses and stores all style data in its cell buffer — this feature reads it back out.

Without --color (unchanged):

"screen": "RED GREEN BOLD_BLUE normal"

With --color:

"screen": "\u001b[31mRED\u001b[0m \u001b[32mGREEN\u001b[0m \u001b[1;34mBOLD_BLUE\u001b[0m normal"

What's supported

  • 8-color, 16-color, 256-color palette
  • 24-bit RGB colors (foreground and background)
  • Bold, dim, italic, underline, blink, inverse, strikethrough
  • Proper reset handling at style transitions and line boundaries
  • Wide character awareness (CJK glyphs)
  • Background-colored padding cells preserved

Design decisions

  • Opt-in: Default behavior is completely unchanged. The --color flag must be explicitly passed.
  • Change detection uses plain text: lastSnapshot always stores plain text regardless of color mode, so wait() change detection works correctly whether or not color is enabled.
  • Smart trimming: Trailing default-styled whitespace is trimmed, but styled trailing content (e.g. background-colored padding) is preserved to avoid orphaning ANSI escape sequences.

Changes

  • session.ts — ANSI re-encoding helpers + color option on snapshot()/wait()
  • protocol.tscolor?: boolean on SnapshotRequest/WaitRequest
  • daemon.ts — passes color flag through to session
  • cli.ts--color option on snapshot and wait commands

Testing

All 32 existing tests pass. Manually verified with:

  • echo -e with explicit ANSI codes (8-color, bold, inverse)
  • ls --color=always (256-color palette for directories/executables)
  • htop (full-screen TUI with 486 escape sequences captured)

…wait output

tui-use strips all color and style information from terminal output, which is
documented as a known limitation. This adds an opt-in --color flag to the
snapshot and wait commands that re-encodes xterm buffer cell attributes back
into ANSI SGR escape sequences.

Supports the full range of terminal styling: 8/16/256-color palette,
24-bit RGB, bold, dim, italic, underline, blink, inverse, and strikethrough.
Default behavior is unchanged — plain text unless --color is specified.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@onesuper

Copy link
Copy Markdown
Owner

@phiat Thank you for you contribution? Btw, why this tool and color mode is useful for you?

@onesuper

Copy link
Copy Markdown
Owner

@phiat Thanks for this feature! There were merge conflicts with main (the debounce feature was added after your branch). I've resolved them in #6 and preserved your commits

@phiat

phiat commented Apr 10, 2026

Copy link
Copy Markdown
Contributor Author

I'm building claude-esp, a TUI that streams Claude Code's hidden output (thinking, tool calls, subagents) to a separate terminal. It uses Lipgloss/Bubbletea for styling — different colors for thinking blocks, tool headers, error states, etc.

I've been using tui-use as test infrastructure to drive claude-esp and verify its rendering. The plain text snapshots work great for content assertions, but I couldn't test that styling was correct — e.g., that error text actually renders red, or that tool headers use the right color. With --color, I can assert on the ANSI sequences in snapshot output and catch style regressions.

By the way — tui-use worked for me on first try. Thanks for building it, and thanks for resolving the conflicts in #6!

onesuper added a commit that referenced this pull request Apr 10, 2026
Merge #2: Add --color flag to preserve ANSI escape sequences (with debounce fix)
@onesuper onesuper merged commit 0fb3e5a into onesuper:main Apr 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants