Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

fix(md-view): drop terminal control sequences from the ⌘M output view

review9603fab6861b
diff --git a/src/renderer/lib/terminalOutputMarkdown.test.ts b/src/renderer/lib/terminalOutputMarkdown.test.ts
index 0c6950521..7698120e9 100644
--- a/src/renderer/lib/terminalOutputMarkdown.test.ts
+++ b/src/renderer/lib/terminalOutputMarkdown.test.ts
@@ -1,7 +1,7 @@
// @vitest-environment jsdom
import { describe, expect, it } from 'vitest'
import { marked } from 'marked'
-import { MD_OUTPUT_MAX_LINES, renderTerminalOutput, tailOutputLines } from './terminalOutputMarkdown'
+import { MD_OUTPUT_MAX_LINES, renderTerminalOutput, stripTerminalControls, tailOutputLines } from './terminalOutputMarkdown'
import { renderMarkdown } from './markdown'
/** Visible text of the rendered HTML, the way a user reads it. */
@@ -66,6 +66,38 @@ describe('renderTerminalOutput', () => {
)
})
+ it('drops the colour codes an SSH capture (capture-pane -e) carries', () => {
+ // Shaped like the reported screen: 256-colour SGR around file names, bold labels, a reset.
+ const raw = '\x1b[1mDosyalar\x1b[0m: \x1b[38;5;153mview/web/css/items.css\x1b[39m done\n\x1b[38;5;244m────\x1b[39m'
+ const html = renderTerminalOutput(raw)
+ expect(html).toContain('Dosyalar')
+ expect(html).toContain('view/web/css/items.css')
+ expect(html).not.toMatch(/\[[0-9;]*m/)
+ expect(html).not.toContain('\x1b')
+ })
+
+ it('drops OSC sequences (hyperlinks, titles) and keeps their visible text', () => {
+ const raw = 'see \x1b]8;;https://example.com\x1b\\the docs\x1b]8;;\x1b\\ and \x1b]0;title\x07done'
+ expect(stripTerminalControls(raw)).toBe('see the docs and done')
+ })
+
+ it('keeps tabs, newlines and a CRLF capture\'s carriage returns', () => {
+ expect(stripTerminalControls('a\tb\r\nc\x07\x00d')).toBe('a\tb\r\ncd')
+ })
+
+ it('stays linear on a long run of unterminated escapes', () => {
+ const raw = '\x1b['.repeat(200_000) + 'x'
+ const t = performance.now()
+ stripTerminalControls(raw)
+ expect(performance.now() - t).toBeLessThan(1000)
+ })
+
+ it('tailOutputLines counts lines after the codes are gone', () => {
+ const { text, dropped } = tailOutputLines('\x1b[31ma\x1b[0m\nb\nc', 2)
+ expect(text).toBe('b\nc')
+ expect(dropped).toBe(1)
+ })
+
it('renders empty input as empty', () => {
expect(renderTerminalOutput('')).toBe('')
expect(renderTerminalOutput('\n\n \n')).toBe('')
diff --git a/src/renderer/lib/terminalOutputMarkdown.ts b/src/renderer/lib/terminalOutputMarkdown.ts
index da9df9445..e2e07d49f 100644
--- a/src/renderer/lib/terminalOutputMarkdown.ts
+++ b/src/renderer/lib/terminalOutputMarkdown.ts
@@ -46,10 +46,33 @@ const terminalMarked = new Marked({
}
})
-/** Strip trailing whitespace (and a CRLF capture's `\r` — explicit, though JS's /m `$` and marked's
- * own CRLF normalization already cover it) from every line, then capture-pane's blank-line pad. */
+/**
+ * Remove terminal control sequences from a capture. An SSH project's capture comes from the
+ * remote tmux with `-e` (the same builder serves the reattach paint, which needs the colours), so
+ * every colour change arrived here as `ESC[38;5;153m`; the ESC byte vanished in rendering and the
+ * view showed `[38;5;153m` around every coloured word. A local capture has no `-e` and is unchanged.
+ * CSI (`ESC [ … final`), OSC (`ESC ] … BEL|ESC \`) and the two-byte ESC forms are removed, then any
+ * C0/C1 control other than tab and newline. Each pattern is linear: no nested quantifiers.
+ */
+export function stripTerminalControls(text: string): string {
+ return (
+ text
+ // eslint-disable-next-line no-control-regex
+ .replace(/\x1b\[[0-?]*[ -/]*[@-~]/g, '')
+ // eslint-disable-next-line no-control-regex
+ .replace(/\x1b\][^\x07\x1b]*(?:\x07|\x1b\\)?/g, '')
+ // eslint-disable-next-line no-control-regex
+ .replace(/\x1b[@-Z\\-_]?/g, '')
+ // eslint-disable-next-line no-control-regex
+ .replace(/[\x00-\x08\x0b-\x1f\x7f-\x9f]/g, (c) => (c === '\r' ? c : ''))
+ )
+}
+
+/** Strip control sequences, then trailing whitespace (and a CRLF capture's `\r` — explicit, though
+ * JS's /m `$` and marked's own CRLF normalization already cover it) from every line, then
+ * capture-pane's blank-line pad. */
function trimCapture(text: string): string {
- return text.replace(/[ \t\r]+$/gm, '').replace(/\s+$/, '')
+ return stripTerminalControls(text).replace(/[ \t\r]+$/gm, '').replace(/\s+$/, '')
}
export function renderTerminalOutput(text: string): string {