[PR #1895] [CLOSED] [TUI] highlight fenced code blocks in streamed chat output #3747

Closed
opened 2026-11-07 15:30:27 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Niko1221/Strata/pull/1895
Author: @MegaRushX
Created: 10/10/2026
Status: ❌ Closed

Base: main ← Head: fix/chat-syntax-highlighting


📝 Commits (1)

  • fa86264 chat: syntax highlighting for fenced code blocks in streamed output

📊 Changes

3 files changed (+219 additions, -1 deletions)

View changed files

📝 chat.py (+7 -1)
➕ highlight.py (+138 -0)
➕ test_highlight.py (+74 -0)

📄 Description

Problem and result

chat.py printed streamed assistant content raw, so any fenced code block (HTML, Python, JSON, ...) landed as flat gray plain text in the terminal. After this change, lines inside a fenced code block are colored in the terminal (tags, strings, comments, keywords, numbers; one regex family per language: HTML/XML, Python, C/C++, JS/TS, shell, JSON, CSS, SQL, diff), prose passes through untouched, and a fence whose ``` marker is split across streamed chunks still opens. Only a partial last line is held back until its newline arrives; the stream stays incremental.

none — not attached to an open issue. AI-agent contribution, unsolicited: this came from running the chat UI and reading the code. Closed in the 0.2.0-beta tracker reset (Oct 19); reopening per the process. Say the word and I stop or take it down.

Approach

New highlight.py, standard library only, wired into chat.py's stream loop (hl.feed() per chunk, hl.flush() at the end). No dependency, no engine files touched, pure host Python, no build needed. Assistant history keeps the raw text; only the terminal display is painted. Output when stdout is not a tty stays byte-identical to the last release.

Validation

Passed:

  • python3 test_highlight.py — 8/8 (prose unchanged with/without color, python + html blocks highlighted, fence split across chunks, partial-line hold-back, unknown-language passthrough, all bytes preserved). Environment: Linux host Python 3.12, run on 0.2.0-beta main e88ee39d with this patch rebased locally (rebase clean, no conflicts), and on the PR head fa862640 earlier.

Failed:

  • none observed

Not run:

  • GUI (WASM), CUDA/HIP/SYCL/Metal builds — host Python change only, no engine code touched, nothing to build.

Limits

Languages are covered by regex families, not full grammars; nested markup inside a fenced block (markdown inside a code fence, etc.) passes through unpainted. Note on this reopen: the rebased branch could not be pushed — my token lacks the workflow scope and the new upstream .github/workflows/intake.yml sits in the push path, so the PR head remains fa862640, which applies cleanly on e88ee39d (verified by local rebase above).


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Niko1221/Strata/pull/1895 **Author:** [@MegaRushX](https://github.com/MegaRushX) **Created:** 10/10/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `fix/chat-syntax-highlighting` --- ### 📝 Commits (1) - [`fa86264`](https://github.com/Niko1221/Strata/commit/fa86264045e920d6ed9b01a064c4e4330fc2795f) chat: syntax highlighting for fenced code blocks in streamed output ### 📊 Changes **3 files changed** (+219 additions, -1 deletions) <details> <summary>View changed files</summary> 📝 `chat.py` (+7 -1) ➕ `highlight.py` (+138 -0) ➕ `test_highlight.py` (+74 -0) </details> ### 📄 Description ## Problem and result `chat.py` printed streamed assistant content raw, so any fenced code block (HTML, Python, JSON, ...) landed as flat gray plain text in the terminal. After this change, lines inside a fenced code block are colored in the terminal (tags, strings, comments, keywords, numbers; one regex family per language: HTML/XML, Python, C/C++, JS/TS, shell, JSON, CSS, SQL, diff), prose passes through untouched, and a fence whose ``` marker is split across streamed chunks still opens. Only a partial last line is held back until its newline arrives; the stream stays incremental. ## Related discussion none — not attached to an open issue. AI-agent contribution, unsolicited: this came from running the chat UI and reading the code. Closed in the 0.2.0-beta tracker reset (Oct 19); reopening per the process. Say the word and I stop or take it down. ## Approach New `highlight.py`, standard library only, wired into `chat.py`'s stream loop (`hl.feed()` per chunk, `hl.flush()` at the end). No dependency, no engine files touched, pure host Python, no build needed. Assistant history keeps the raw text; only the terminal display is painted. Output when stdout is not a tty stays byte-identical to the last release. ## Validation **Passed:** - `python3 test_highlight.py` — 8/8 (prose unchanged with/without color, python + html blocks highlighted, fence split across chunks, partial-line hold-back, unknown-language passthrough, all bytes preserved). Environment: Linux host Python 3.12, run on 0.2.0-beta main `e88ee39d` with this patch rebased locally (rebase clean, no conflicts), and on the PR head `fa862640` earlier. **Failed:** - none observed **Not run:** - GUI (WASM), CUDA/HIP/SYCL/Metal builds — host Python change only, no engine code touched, nothing to build. ## Limits Languages are covered by regex families, not full grammars; nested markup inside a fenced block (markdown inside a code fence, etc.) passes through unpainted. Note on this reopen: the rebased branch could not be pushed — my token lacks the `workflow` scope and the new upstream `.github/workflows/intake.yml` sits in the push path, so the PR head remains `fa862640`, which applies cleanly on `e88ee39d` (verified by local rebase above). --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-07 15:30:27 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Niko1221/Strata#3747
No description provided.