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
24 changes: 24 additions & 0 deletions apps/desktop/src/main/terminal/session.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,30 @@ describe('TerminalSession command lifecycle', () => {
}
})

it('shows a full-screen program as open when it starts in the same read the previous command ended', () => {
const originalShell = process.env.SHELL
process.env.SHELL = '/bin/zsh'
const session = TerminalSession.create({
terminalId: 'terminal-1',
cwd: '/tmp',
cols: 80,
rows: 24,
callbacks: { onData: () => {}, onState: () => {}, onCommand: () => {}, onExit: () => {} },
})

try {
// Typed-ahead `ls` then `vim`: the end of one and the start of the next can share a read.
ptyStub.dataHandler?.(
'\u001b]633;C;test-nonce\u0007a b\r\n\u001b]633;D;0;test-nonce\u0007\u001b]633;A;test-nonce\u0007% \u001b]633;C;test-nonce\u0007\u001b[?1049h'
)
expect(session.tabState(true).interactive).toBe(true)
} finally {
session.dispose()
if (originalShell === undefined) process.env.SHELL = undefined
else process.env.SHELL = originalShell
}
})

it('keeps normal long-command activity until the foreground process exits', async () => {
vi.useFakeTimers()
const originalShell = process.env.SHELL
Expand Down
21 changes: 12 additions & 9 deletions apps/desktop/src/main/terminal/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -828,24 +828,22 @@ export class TerminalSession {

if (text) {
this.lastOutputAt = Date.now()
this.trackAltScreen(text)
this.scrollback += text
if (this.scrollback.length > MAX_SCROLLBACK_CHARS + TRIM_SLACK_CHARS) {
this.scrollback = this.scrollback.slice(-MAX_SCROLLBACK_CHARS)
}
}

// Text is attributed in stream order, so a command keeps only the output between its own
// start and end markers even when they share this chunk with the output.
// Text is applied in stream order, so a command keeps only the output between its own start
// and end markers, and a screen switch lands on whichever command was running when it came.
let from = 0
let enteredFullScreen = false
markers.forEach((marker, index) => {
enteredFullScreen =
this.captureCommandOutput(text.slice(from, offsets[index])) || enteredFullScreen
enteredFullScreen = this.applyText(text.slice(from, offsets[index])) || enteredFullScreen
from = offsets[index]
this.applyMarker(marker)
})
enteredFullScreen = this.captureCommandOutput(text.slice(from)) || enteredFullScreen
enteredFullScreen = this.applyText(text.slice(from)) || enteredFullScreen
// Judged once the whole read is applied: a program that opened and closed the alternate
// screen and finished within it already has its result, and one that left it is not holding it.
if (enteredFullScreen && this.pendingCommand?.capturing && this.altScreen) {
Expand All @@ -858,9 +856,14 @@ export class TerminalSession {
}
}

/** Adds text to the running command's output; true when it entered the alternate screen. */
private captureCommandOutput(text: string): boolean {
if (!text || !this.pendingCommand?.capturing) return false
/**
* Follows the screen switches in `text` and adds it to the running command's output; true when
* that command entered the alternate screen.
*/
private applyText(text: string): boolean {
if (!text) return false
this.trackAltScreen(text)
if (!this.pendingCommand?.capturing) return false
this.pendingCommand.lastActivityAt = Date.now()
this.captureOutput(this.pendingCommand, text)
return text.includes(ALT_SCREEN_ENTER)
Expand Down
Loading