From 3e06fe84dc589f7a385130b4163d492e3e9246ab Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Mon, 19 Jan 2026 05:05:36 +0000 Subject: [PATCH] feat: add TUI code block syntax highlighting (#1200) (thanks @vignesh07) --- CHANGELOG.md | 1 + src/tui/theme/theme.test.ts | 3 ++- src/tui/theme/theme.ts | 1 + 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b7ad5b6bc..e95e86f97 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ Docs: https://docs.clawd.bot ### Changes - Usage: add `/usage cost` summaries and macOS menu cost submenu with daily charting. - Agents: clarify node_modules read-only guidance in agent instructions. +- TUI: add syntax highlighting for code blocks. (#1200) — thanks @vignesh07. ### Fixes - Plugins: surface plugin load/register/config errors in gateway logs with plugin/source context. diff --git a/src/tui/theme/theme.test.ts b/src/tui/theme/theme.test.ts index 648ff87d8..ac3ebd304 100644 --- a/src/tui/theme/theme.test.ts +++ b/src/tui/theme/theme.test.ts @@ -102,7 +102,8 @@ console.log(message);`; const result = markdownTheme.highlightCode!(code, "javascript"); // Strip ANSI codes to verify content is preserved - const stripAnsi = (str: string) => str.replace(/\x1b\[[0-9;]*m/g, ""); + const stripAnsi = (str: string) => + str.replace(new RegExp(`${String.fromCharCode(27)}\\[[0-9;]*m`, "g"), ""); expect(stripAnsi(result[0])).toBe(`const message = "Hello, World!";`); expect(stripAnsi(result[1])).toBe("console.log(message);"); }); diff --git a/src/tui/theme/theme.ts b/src/tui/theme/theme.ts index 4c46a6fa5..1ceef7f63 100644 --- a/src/tui/theme/theme.ts +++ b/src/tui/theme/theme.ts @@ -88,6 +88,7 @@ const syntaxTheme = { */ function highlightCode(code: string, lang?: string): string[] { try { + // Auto-detect can be slow for very large blocks; prefer explicit language when available. // Check if language is supported, fall back to auto-detect const language = lang && supportsLanguage(lang) ? lang : undefined; const highlighted = highlight(code, {