Skip to content

Commit 2c9ea32

Browse files
authored
fix(demo): improve terminal resizing to fit container (#58)
1 parent d3c37df commit 2c9ea32

File tree

4 files changed

+22
-11
lines changed

4 files changed

+22
-11
lines changed

demo/bin/demo.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,16 @@ const HTML_TEMPLATE = `<!doctype html>
160160
padding: 0;
161161
min-height: 400px;
162162
height: 60vh;
163+
position: relative;
163164
}
164165
165166
#terminal {
166-
width: 100%;
167-
height: 100%;
167+
position: absolute;
168+
top: 0;
169+
left: 0;
170+
right: 0;
171+
bottom: 0;
172+
overflow: hidden;
168173
}
169174
</style>
170175
</head>
@@ -203,6 +208,7 @@ const HTML_TEMPLATE = `<!doctype html>
203208
const container = document.getElementById('terminal');
204209
await term.open(container);
205210
fitAddon.fit();
211+
fitAddon.observeResize(); // Auto-fit when container resizes
206212
207213
// Status elements
208214
const statusDot = document.getElementById('status-dot');
@@ -249,13 +255,17 @@ const HTML_TEMPLATE = `<!doctype html>
249255
}
250256
});
251257
252-
// Handle resize
253-
window.addEventListener('resize', () => {
254-
fitAddon.fit();
258+
// Handle resize - notify PTY when terminal dimensions change
259+
term.onResize(({ cols, rows }) => {
255260
if (ws && ws.readyState === WebSocket.OPEN) {
256-
ws.send(JSON.stringify({ type: 'resize', cols: term.cols, rows: term.rows }));
261+
ws.send(JSON.stringify({ type: 'resize', cols, rows }));
257262
}
258263
});
264+
265+
// Also handle window resize (for browsers that don't trigger ResizeObserver on window resize)
266+
window.addEventListener('resize', () => {
267+
fitAddon.fit();
268+
});
259269
</script>
260270
</body>
261271
</html>`;

demo/bun.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"name": "@ghostty-web/demo",
77
"dependencies": {
88
"@lydell/node-pty": "^1.0.1",
9-
"ghostty-web": "^0.2.1",
9+
"ghostty-web": "latest",
1010
},
1111
},
1212
},

demo/index.html

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@
138138
let ws;
139139
let fitAddon;
140140

141-
function initTerminal() {
141+
async function initTerminal() {
142142
term = new Terminal({
143143
cursorBlink: true,
144144
fontSize: 14,
@@ -153,10 +153,11 @@
153153
fitAddon = new FitAddon();
154154
term.loadAddon(fitAddon);
155155

156-
term.open(document.getElementById('terminal-container'));
156+
await term.open(document.getElementById('terminal-container'));
157157
fitAddon.fit();
158+
fitAddon.observeResize(); // Auto-fit when container resizes
158159

159-
// Handle window resize
160+
// Handle window resize (for browsers that don't trigger ResizeObserver on window resize)
160161
window.addEventListener('resize', () => {
161162
fitAddon.fit();
162163
});

lib/terminal.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export class Terminal implements ITerminalCore {
6767
// Components (created on open())
6868
private ghostty?: Ghostty;
6969
public wasmTerm?: GhosttyTerminal; // Made public for link providers
70-
private renderer?: CanvasRenderer;
70+
public renderer?: CanvasRenderer; // Made public for FitAddon
7171
private inputHandler?: InputHandler;
7272
private selectionManager?: SelectionManager;
7373
private canvas?: HTMLCanvasElement;

0 commit comments

Comments
 (0)