Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion plugins/terminals/src/node/backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,22 @@ export function spawnPipe(options: SpawnBackendOptions): TerminalProcess {
const exitCbs: ((code: number) => void)[] = []
let exited = false

// A piped child has no controlling TTY, so its stdout/stderr carry bare `\n`
// line endings — a real PTY would apply the kernel's ONLCR translation. xterm
// only returns the cursor to column 0 on `\r`, so forwarding bare `\n` renders
// a staircase. Translate lone `\n` to `\r\n`, tracking a `\r` left dangling at
// a chunk boundary so an existing `\r\n` split across chunks isn't doubled.
let pendingCr = false
const normalizeNewlines = (data: string): string => {
const out = data.replace(/\r?\n/g, (match, offset: number) =>
match === '\n' && !(offset === 0 && pendingCr) ? '\r\n' : match)
pendingCr = out.endsWith('\r')
return out
}

const emitData = (data: string): void => {
for (const cb of dataCbs) cb(data)
const normalized = normalizeNewlines(data)
for (const cb of dataCbs) cb(normalized)
}
const emitExit = (code: number): void => {
if (exited)
Expand Down
20 changes: 20 additions & 0 deletions plugins/terminals/test/terminals.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,26 @@ describe('@devframes/plugin-terminals', () => {
})
})

it('normalizes bare LF from a piped readonly session to CRLF', async () => {
const client = bootClient(server.port)
await new Promise(r => setTimeout(r, 50))

// A piped child has no TTY to apply ONLCR, so it emits bare `\n`. Without
// normalization xterm renders a staircase; the backend must translate lone
// `\n` to `\r\n` while leaving an existing `\r\n` untouched.
const info = await call<TerminalSessionInfo>(client, 'devframes-plugin-terminals:spawn', {
command: NODE,
args: ['-e', 'process.stdout.write("a\\nb\\r\\nc")'],
mode: 'readonly',
})

const reader = subscribe(client, info.id)
const output = await collectUntil(reader, acc => acc.includes('a') && acc.includes('b') && acc.includes('c'))
expect(output).toContain('a\r\nb\r\nc')
expect(output).not.toContain('a\nb')
expect(output).not.toContain('\r\r\n')
})

it('rejects writes to a readonly session', async () => {
const client = bootClient(server.port)
await new Promise(r => setTimeout(r, 50))
Expand Down
Loading