diff --git a/src/features/terminal/embedded.test.ts b/src/features/terminal/embedded.test.ts index d140ade5f..919f4cc08 100644 --- a/src/features/terminal/embedded.test.ts +++ b/src/features/terminal/embedded.test.ts @@ -78,16 +78,17 @@ afterEach(() => { }); it.each([ - ["windows", 19045, { backend: "conpty", buildNumber: 19045 }], - ["windows", 22631, { backend: "conpty", buildNumber: 22631 }], - ["windows", null, { backend: "conpty", buildNumber: undefined }], - ["macos", null, undefined], - ["linux", null, undefined], -] as const)("configures embedded terminals for %s build %s", (os, windowsBuildNumber, expected) => { + ["windows", 19045, { backend: "conpty", buildNumber: 19045 }, false], + ["windows", 22631, { backend: "conpty", buildNumber: 22631 }, true], + ["windows", null, { backend: "conpty", buildNumber: undefined }, false], + ["macos", null, undefined, false], + ["linux", null, undefined, false], +] as const)("configures embedded terminals for %s build %s", (os, windowsBuildNumber, expected, reflowCursorLine) => { usePlatform.setState({ os, windowsBuildNumber }); const term = createEmbeddedTerminal(document.createElement("div"), { onData() {}, onResize() {} }); try { expect(terminal.options.windowsPty).toEqual(expected); + expect(terminal.options.reflowCursorLine).toBe(reflowCursorLine); } finally { term.dispose(); } diff --git a/src/features/terminal/embedded.ts b/src/features/terminal/embedded.ts index b49a32297..4ae4b6232 100644 --- a/src/features/terminal/embedded.ts +++ b/src/features/terminal/embedded.ts @@ -21,7 +21,7 @@ import { useProject } from "@/platform/project"; import { usePlatform } from "@/platform/platformInfo"; import { workflowSettings } from "@/platform/workflowSettings"; import { fitScheduler, settledResize } from "./fit"; -import { watchWindowsPty, windowsPty } from "./windowsPty"; +import { watchWindowsPty, windowsPtyOptions } from "./windowsPty"; import { findTerminalLinks } from "./links"; import { LINK_HANDLER, fontFor, openExternal, openFileLink } from "./session"; import { MIN_CONTRAST_RATIO, readLinkColor, readTerminalTheme, remeasureWhenFontsLoad } from "./theme"; @@ -42,7 +42,7 @@ export function createEmbeddedTerminal(host: HTMLElement, opts: EmbeddedTerminal const settings = workflowSettings().terminal; const font = fontFor(settings); const term = new Terminal({ - windowsPty: windowsPty(), + ...windowsPtyOptions(), linkHandler: LINK_HANDLER, fontFamily: font.family, fontSize: font.size, diff --git a/src/features/terminal/session.test.ts b/src/features/terminal/session.test.ts index 90add1ece..3db2c8837 100644 --- a/src/features/terminal/session.test.ts +++ b/src/features/terminal/session.test.ts @@ -51,16 +51,17 @@ describe("Windows PTY metadata", () => { afterEach(() => usePlatform.setState(initialPlatform)); it.each([ - ["windows", 19045, { backend: "conpty", buildNumber: 19045 }], - ["windows", 22631, { backend: "conpty", buildNumber: 22631 }], - ["windows", null, { backend: "conpty", buildNumber: undefined }], - ["macos", null, {}], - ["linux", null, {}], - ] as const)("configures %s build %s", (os, windowsBuildNumber, expected) => { + ["windows", 19045, { backend: "conpty", buildNumber: 19045 }, false], + ["windows", 22631, { backend: "conpty", buildNumber: 22631 }, true], + ["windows", null, { backend: "conpty", buildNumber: undefined }, false], + ["macos", null, {}, false], + ["linux", null, {}, false], + ] as const)("configures %s build %s", (os, windowsBuildNumber, expected, reflowCursorLine) => { usePlatform.setState({ os, windowsBuildNumber }); const session = new TerminalSession({ key: "pty-options", cwd: "/repo", onChange() {}, onFocus() {} }); try { expect(session.term.options.windowsPty).toEqual(expected); + expect(session.term.options.reflowCursorLine).toBe(reflowCursorLine); } finally { session.dispose(); } @@ -69,11 +70,39 @@ describe("Windows PTY metadata", () => { it("updates a restored terminal when platform info arrives and unsubscribes on disposal", () => { usePlatform.setState({ os: "windows", windowsBuildNumber: null }); const session = new TerminalSession({ key: "restored", cwd: "/repo", onChange() {}, onFocus() {} }); + expect(session.term.options.reflowCursorLine).toBe(false); usePlatform.setState({ windowsBuildNumber: 22631 }); expect(session.term.options.windowsPty).toEqual({ backend: "conpty", buildNumber: 22631 }); + expect(session.term.options.reflowCursorLine).toBe(true); session.dispose(); usePlatform.setState({ windowsBuildNumber: 19045 }); expect(session.term.options.windowsPty).toEqual({ backend: "conpty", buildNumber: 22631 }); + expect(session.term.options.reflowCursorLine).toBe(true); + }); + + it.each([ + ["idle prompt", ""], + ["wrapped pending input", "Write-Output 'pending input spanning more than one terminal row"], + ])("preserves the %s when a ConPTY session narrows and widens", async (_, input) => { + usePlatform.setState({ os: "windows", windowsBuildNumber: 26200 }); + const session = new TerminalSession({ key: "pty-reflow", cwd: "/repo", onChange() {}, onFocus() {} }); + try { + const prompt = "PS C:\\workspace\\a-long-project-directory\\src> "; + const content = prompt + input; + const { term } = session; + const terminalText = () => { + const buffer = term.buffer.active; + return Array.from({ length: buffer.length }, (_, i) => buffer.getLine(i)?.translateToString(true)).join(""); + }; + term.resize(80, 8); + await new Promise((resolve) => term.write(content, resolve)); + term.resize(30, 8); + expect(terminalText()).toBe(content); + term.resize(80, 8); + expect(terminalText()).toBe(content); + } finally { + session.dispose(); + } }); }); diff --git a/src/features/terminal/session.ts b/src/features/terminal/session.ts index a66a08789..28acf50bb 100644 --- a/src/features/terminal/session.ts +++ b/src/features/terminal/session.ts @@ -16,7 +16,7 @@ import { tx } from "@/platform/i18n"; import { editorService, notifications, registerFontZoomSurface } from "@/workbench/contributions"; import { OutputActivity } from "./activity"; import { fitScheduler, settledResize } from "./fit"; -import { watchWindowsPty, windowsPty } from "./windowsPty"; +import { watchWindowsPty, windowsPtyOptions } from "./windowsPty"; import { installTerminalDrop } from "./drop"; import { installDomRowCache } from "./domRowCache"; import { installSelectionHighlights, type SelectionHighlightController } from "./highlight"; @@ -311,7 +311,7 @@ export class TerminalSession { const settings = workflowSettings().terminal; const font = fontFor(settings); this.term = new Terminal({ - windowsPty: windowsPty(), + ...windowsPtyOptions(), fontFamily: font.family, fontSize: font.size, lineHeight: font.lineHeight, diff --git a/src/features/terminal/windowsPty.test.ts b/src/features/terminal/windowsPty.test.ts index 98c20366c..1128d836b 100644 --- a/src/features/terminal/windowsPty.test.ts +++ b/src/features/terminal/windowsPty.test.ts @@ -1,5 +1,5 @@ import { afterEach, expect, it } from "vitest"; -import { windowsPty } from "./windowsPty"; +import { windowsPtyOptions } from "./windowsPty"; import { usePlatform } from "@/platform/platformInfo"; import { useRemote } from "@/platform/remote"; @@ -15,5 +15,5 @@ it.each(["linux", "windows"])("does not apply the desktop build to a remote %s P useRemote.setState({ session: { os, host: "test", root: "/repo", target: { kind: "ssh", config: "test" }, commands: [] }, }); - expect(windowsPty()).toBeUndefined(); + expect(windowsPtyOptions()).toEqual({ reflowCursorLine: false }); }); diff --git a/src/features/terminal/windowsPty.ts b/src/features/terminal/windowsPty.ts index 68d50c8ac..fab4f5fa8 100644 --- a/src/features/terminal/windowsPty.ts +++ b/src/features/terminal/windowsPty.ts @@ -2,19 +2,24 @@ import type { ITerminalOptions, Terminal } from "@xterm/xterm"; import { usePlatform } from "@/platform/platformInfo"; import { useRemote } from "@/platform/remote"; -/** Report the local ConPTY backend even when the OS build is unavailable. */ -export function windowsPty(): ITerminalOptions["windowsPty"] { +export function windowsPtyOptions(): Pick { const { os, windowsBuildNumber } = usePlatform.getState(); // Remote PTYs must not inherit the desktop's Windows build or backend. - if (useRemote.getState().session) return undefined; - return os === "windows" ? { backend: "conpty", buildNumber: windowsBuildNumber ?? undefined } : undefined; + if (os !== "windows" || useRemote.getState().session) return { reflowCursorLine: false }; + return { + windowsPty: { backend: "conpty", buildNumber: windowsBuildNumber ?? undefined }, + // Modern ConPTY can leave the prompt untouched on resize and echo input with absolute cursor moves. + reflowCursorLine: windowsBuildNumber !== null && windowsBuildNumber >= 21376, + }; } /** Restored terminals can be constructed before appInfo answers. */ export function watchWindowsPty(term: Terminal) { const unsubscribe = usePlatform.subscribe((next, previous) => { if (next.os !== previous.os || next.windowsBuildNumber !== previous.windowsBuildNumber) { - term.options.windowsPty = windowsPty() ?? {}; + const { windowsPty, reflowCursorLine } = windowsPtyOptions(); + term.options.windowsPty = windowsPty ?? {}; + term.options.reflowCursorLine = reflowCursorLine; } }); return { dispose: unsubscribe };