fix(terminal): send the final size to a newly started PTY (#5473)

A new project's first terminal can end up with the wrong size. While the shell is starting, the terminal may resize, for example when the font finishes loading. That new size never reaches the shell. Full-screen programs such as vim then draw at the old width and the text looks broken. This fix sends the current size once the shell is ready.

Co-authored-by: zhzhsx <1379025+zhzhsx@users.noreply.github.com>
This commit is contained in:
zhzhsx
2026-11-24 00:09:43 +08:00
committed by GitHub
co-authored by zhzhsx
parent 348f4c2a2e
commit dc2924db17
2 changed files with 54 additions and 0 deletions
+52
View File
@@ -389,6 +389,58 @@ describe("terminal restart", () => {
});
});
describe("terminal startup size", () => {
beforeEach(() => vi.clearAllMocks());
it("resizes a freshly spawned PTY to the grid reached while startup was pending", async () => {
let release: ((value: { id: number; title: string }) => void) | undefined;
ipc.commands.termSpawn.mockImplementationOnce(
() =>
new Promise<{ id: number; title: string }>((resolve) => {
release = resolve;
}),
);
const session = new TerminalSession({ key: "s-start-size", cwd: "/repo", onChange() {}, onFocus() {} });
try {
session.term.resize(80, 24);
const starting = session.start();
await vi.waitFor(() => expect(release).toBeTypeOf("function"));
expect(ipc.commands.termSpawn).toHaveBeenCalledWith(
expect.objectContaining({ cols: 80, rows: 24 }),
expect.anything(),
);
// Resize while startup is still pending.
session.term.resize(100, 30);
release!({ id: 12, title: "zsh" });
await starting;
expect(ipc.commands.termResize).toHaveBeenCalledWith(12, 100, 30);
} finally {
session.dispose();
}
});
it("leaves the PTY alone when the grid did not change during startup", async () => {
let release: ((value: { id: number; title: string }) => void) | undefined;
ipc.commands.termSpawn.mockImplementationOnce(
() =>
new Promise<{ id: number; title: string }>((resolve) => {
release = resolve;
}),
);
const session = new TerminalSession({ key: "s-start-same", cwd: "/repo", onChange() {}, onFocus() {} });
try {
session.term.resize(80, 24);
const starting = session.start();
await vi.waitFor(() => expect(release).toBeTypeOf("function"));
release!({ id: 13, title: "zsh" });
await starting;
expect(ipc.commands.termResize).not.toHaveBeenCalled();
} finally {
session.dispose();
}
});
});
describe("terminal open", () => {
afterEach(() => vi.useRealTimers());
+2
View File
@@ -858,6 +858,8 @@ export class TerminalSession {
}
this.ptyId = info.id;
this.update({ status: "running", title: info.title || "Local" });
// Send the new size if it changed while the program was starting.
if (this.term.cols !== cols || this.term.rows !== rows) this.resizePty(this.term.cols, this.term.rows);
this.flushPendingInput();
} catch (e) {
this.pendingInput = "";