Skip to content

Commit f7a153f

Browse files
authored
fix(desktop-terminal): follow alternate-screen switches in stream order (#10523)
Alternate-screen switches were tracked over a whole read before its shell markers were applied, so when one command ended and the next entered the alternate screen in the same read, the end marker's reset ran last and the tab reported no full-screen program while one was open. Track each segment between markers in order, the same way output is attributed.
1 parent 09c0b11 commit f7a153f

2 files changed

Lines changed: 36 additions & 9 deletions

File tree

‎apps/desktop/src/main/terminal/session.test.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,30 @@ describe('TerminalSession command lifecycle', () => {
128128
}
129129
})
130130

131+
it('shows a full-screen program as open when it starts in the same read the previous command ended', () => {
132+
const originalShell = process.env.SHELL
133+
process.env.SHELL = '/bin/zsh'
134+
const session = TerminalSession.create({
135+
terminalId: 'terminal-1',
136+
cwd: '/tmp',
137+
cols: 80,
138+
rows: 24,
139+
callbacks: { onData: () => {}, onState: () => {}, onCommand: () => {}, onExit: () => {} },
140+
})
141+
142+
try {
143+
// Typed-ahead `ls` then `vim`: the end of one and the start of the next can share a read.
144+
ptyStub.dataHandler?.(
145+
'\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'
146+
)
147+
expect(session.tabState(true).interactive).toBe(true)
148+
} finally {
149+
session.dispose()
150+
if (originalShell === undefined) process.env.SHELL = undefined
151+
else process.env.SHELL = originalShell
152+
}
153+
})
154+
131155
it('keeps normal long-command activity until the foreground process exits', async () => {
132156
vi.useFakeTimers()
133157
const originalShell = process.env.SHELL

‎apps/desktop/src/main/terminal/session.ts‎

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -828,24 +828,22 @@ export class TerminalSession {
828828

829829
if (text) {
830830
this.lastOutputAt = Date.now()
831-
this.trackAltScreen(text)
832831
this.scrollback += text
833832
if (this.scrollback.length > MAX_SCROLLBACK_CHARS + TRIM_SLACK_CHARS) {
834833
this.scrollback = this.scrollback.slice(-MAX_SCROLLBACK_CHARS)
835834
}
836835
}
837836

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

861-
/** Adds text to the running command's output; true when it entered the alternate screen. */
862-
private captureCommandOutput(text: string): boolean {
863-
if (!text || !this.pendingCommand?.capturing) return false
859+
/**
860+
* Follows the screen switches in `text` and adds it to the running command's output; true when
861+
* that command entered the alternate screen.
862+
*/
863+
private applyText(text: string): boolean {
864+
if (!text) return false
865+
this.trackAltScreen(text)
866+
if (!this.pendingCommand?.capturing) return false
864867
this.pendingCommand.lastActivityAt = Date.now()
865868
this.captureOutput(this.pendingCommand, text)
866869
return text.includes(ALT_SCREEN_ENTER)

0 commit comments

Comments
 (0)