Skip to content

Commit fafb195

Browse files
authored
Merge pull request #5693 from xtermjs/anthonykim1/demoPixelDimensions
Pass pixel dimensions to node-pty in demo
2 parents f0d291f + a0574f4 commit fafb195

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

demo/client/client.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,9 @@ function createTerminal(): Terminal {
328328
}
329329
const cols = size.cols;
330330
const rows = size.rows;
331-
const url = '/terminals/' + pid + '/size?cols=' + cols + '&rows=' + rows;
331+
const pixelWidth = Math.round(term!.dimensions?.css?.canvas?.width ?? 0);
332+
const pixelHeight = Math.round(term!.dimensions?.css?.canvas?.height ?? 0);
333+
const url = '/terminals/' + pid + '/size?cols=' + cols + '&rows=' + rows + '&pixelWidth=' + pixelWidth + '&pixelHeight=' + pixelHeight;
332334

333335
fetch(url, { method: 'POST' });
334336
});
@@ -379,7 +381,9 @@ function createTerminal(): Terminal {
379381
if (useRealTerminal instanceof HTMLInputElement && !useRealTerminal.checked) {
380382
runFakeTerminal();
381383
} else {
382-
const res = await fetch('/terminals?cols=' + term!.cols + '&rows=' + term!.rows, { method: 'POST' });
384+
const pixelWidth = Math.round(term!.dimensions?.css?.canvas?.width ?? 0);
385+
const pixelHeight = Math.round(term!.dimensions?.css?.canvas?.height ?? 0);
386+
const res = await fetch('/terminals?cols=' + term!.cols + '&rows=' + term!.rows + '&pixelWidth=' + pixelWidth + '&pixelHeight=' + pixelHeight, { method: 'POST' });
383387
const processId = await res.text();
384388
pid = processId;
385389
socketURL += processId;
@@ -626,7 +630,7 @@ function updateTerminalSize(): void {
626630
function getBox(width: number, height: number): any {
627631
return {
628632
string: '+',
629-
style: 'font-size: 1px; padding: ' + Math.floor(height/2) + 'px ' + Math.floor(width/2) + 'px; line-height: ' + height + 'px;'
633+
style: 'font-size: 1px; padding: ' + Math.floor(height / 2) + 'px ' + Math.floor(width / 2) + 'px; line-height: ' + height + 'px;'
630634
};
631635
}
632636
if (source instanceof HTMLCanvasElement) {

demo/server/server.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ function startServer(): void {
6565
}
6666
const cols = parseInt(req.query.cols);
6767
const rows = parseInt(req.query.rows);
68+
const pixelWidth = typeof req.query.pixelWidth === 'string' ? parseInt(req.query.pixelWidth) : 0;
69+
const pixelHeight = typeof req.query.pixelHeight === 'string' ? parseInt(req.query.pixelHeight) : 0;
6870
const isWindows = process.platform === 'win32';
6971
const term = pty.spawn(isWindows ? 'powershell.exe' : 'bash', [], {
7072
name: 'xterm-256color',
@@ -77,7 +79,13 @@ function startServer(): void {
7779
useConptyDll: isWindows,
7880
});
7981

80-
console.log('Created terminal with PID: ' + term.pid);
82+
// Set pixel dimensions immediately after spawn (pty.spawn doesn't support them)
83+
if (pixelWidth > 0 && pixelHeight > 0) {
84+
term.resize(cols, rows, { width: pixelWidth, height: pixelHeight });
85+
console.log('Created terminal with PID: ' + term.pid + ' (' + cols + 'x' + rows + ', ' + pixelWidth + 'px x ' + pixelHeight + 'px)');
86+
} else {
87+
console.log('Created terminal with PID: ' + term.pid);
88+
}
8189
terminals[term.pid] = term;
8290
unsentOutput[term.pid] = '';
8391
temporaryDisposable[term.pid] = term.onData(function(data) {
@@ -95,10 +103,17 @@ function startServer(): void {
95103
const pid = parseInt(req.params.pid);
96104
const cols = parseInt(req.query.cols);
97105
const rows = parseInt(req.query.rows);
106+
const pixelWidth = typeof req.query.pixelWidth === 'string' ? parseInt(req.query.pixelWidth) : 0;
107+
const pixelHeight = typeof req.query.pixelHeight === 'string' ? parseInt(req.query.pixelHeight) : 0;
98108
const term = terminals[pid];
99109

100-
term.resize(cols, rows);
101-
console.log('Resized terminal ' + pid + ' to ' + cols + ' cols and ' + rows + ' rows.');
110+
if (pixelWidth > 0 && pixelHeight > 0) {
111+
term.resize(cols, rows, { width: pixelWidth, height: pixelHeight });
112+
console.log('Resized terminal ' + pid + ' to ' + cols + ' cols, ' + rows + ' rows, ' + pixelWidth + 'px x ' + pixelHeight + 'px');
113+
} else {
114+
term.resize(cols, rows);
115+
console.log('Resized terminal ' + pid + ' to ' + cols + ' cols and ' + rows + ' rows.');
116+
}
102117
res.end();
103118
});
104119

0 commit comments

Comments
 (0)