Fix initial terminal resize

This commit is contained in:
GitHub Copilot
2026-01-27 23:14:49 +00:00
parent 6b1e17eda0
commit 5e84d00a30
3 changed files with 57 additions and 25 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
[tool.poetry] [tool.poetry]
name = "textual-webterm" name = "textual-webterm"
version = "0.3.20" version = "0.3.21"
description = "Serve terminal sessions over the web" description = "Serve terminal sessions over the web"
authors = ["Will McGugan <will@textualize.io>"] authors = ["Will McGugan <will@textualize.io>"]
license = "MIT" license = "MIT"
File diff suppressed because one or more lines are too long
+33 -1
View File
@@ -117,6 +117,8 @@ class WebTerminal {
this.send(["resize", { width: cols, height: rows }]); this.send(["resize", { width: cols, height: rows }]);
}); });
this.ensureInitialFit();
// Fit to container and handle resize changes // Fit to container and handle resize changes
this.scheduleFit(); this.scheduleFit();
window.addEventListener("resize", () => this.scheduleFit()); window.addEventListener("resize", () => this.scheduleFit());
@@ -129,6 +131,21 @@ class WebTerminal {
this.connect(); this.connect();
} }
private ensureInitialFit(): void {
if (!("fonts" in document)) {
return;
}
const fontSet = document.fonts;
if (fontSet.status === "loaded") {
return;
}
fontSet.ready
.then(() => this.scheduleFit())
.catch(() => {
// Ignore font readiness errors; resize observer will handle future resizes.
});
}
/** Fit terminal to container size */ /** Fit terminal to container size */
fit(): void { fit(): void {
try { try {
@@ -162,12 +179,27 @@ class WebTerminal {
this.element.classList.add("-connected"); this.element.classList.add("-connected");
this.element.classList.remove("-disconnected"); this.element.classList.remove("-disconnected");
// Send initial size // Send initial size.
// Important: avoid sending a bogus early resize before fonts/layout settle,
// otherwise the PTY will hard-wrap output at the wrong column count.
this.fit(); this.fit();
const sendResize = () => {
const dims = this.fitAddon.proposeDimensions(); const dims = this.fitAddon.proposeDimensions();
if (dims) { if (dims) {
this.send(["resize", { width: dims.cols, height: dims.rows }]); this.send(["resize", { width: dims.cols, height: dims.rows }]);
} }
};
if ("fonts" in document) {
document.fonts.ready
.then(() => {
this.scheduleFit();
sendResize();
})
.catch(() => sendResize());
} else {
sendResize();
}
// Focus terminal // Focus terminal
this.terminal.focus(); this.terminal.focus();