Skip to content

Commit e2819af

Browse files
Ozy-Vikinglouislam
andauthored
Terminal text cols adjusts to terminal container. (louislam#285)
Co-authored-by: Louis Lam <louislam@users.noreply.github.com>
1 parent 94ca8a1 commit e2819af

File tree

7 files changed

+99
-20
lines changed

7 files changed

+99
-20
lines changed

backend/socket-handlers/terminal-socket-handler.ts

+39-4
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,44 @@ export class TerminalSocketHandler extends SocketHandler {
162162
}
163163
});
164164

165-
// TODO: Resize Terminal
166-
socket.on("terminalResize", async (rows : unknown) => {
167-
168-
});
165+
// Resize Terminal
166+
socket.on(
167+
"terminalResize",
168+
async (terminalName: unknown, rows: unknown, cols: unknown) => {
169+
log.info("terminalResize", `Terminal: ${terminalName}`);
170+
try {
171+
checkLogin(socket);
172+
if (typeof terminalName !== "string") {
173+
throw new Error("Terminal name must be a string.");
174+
}
175+
176+
if (typeof rows !== "number") {
177+
throw new Error("Command must be a number.");
178+
}
179+
if (typeof cols !== "number") {
180+
throw new Error("Command must be a number.");
181+
}
182+
183+
let terminal = Terminal.getTerminal(terminalName);
184+
185+
// log.info("terminal", terminal);
186+
if (terminal instanceof Terminal) {
187+
//log.debug("terminalInput", "Terminal found, writing to terminal.");
188+
terminal.rows = rows;
189+
terminal.cols = cols;
190+
} else {
191+
throw new Error(`${terminalName} Terminal not found.`);
192+
}
193+
} catch (e) {
194+
log.debug(
195+
"terminalResize",
196+
// Added to prevent the lint error when adding the type
197+
// and ts type checker saying type is unknown.
198+
// @ts-ignore
199+
`Error on ${terminalName}: ${e.message}`
200+
);
201+
}
202+
}
203+
);
169204
}
170205
}

backend/terminal.ts

+1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ export class Terminal {
6767

6868
set cols(cols : number) {
6969
this._cols = cols;
70+
log.debug("Terminal", `Terminal cols: ${this._cols}`); // Added to check if cols is being set when changing terminal size.
7071
try {
7172
this.ptyProcess?.resize(this.cols, this.rows);
7273
} catch (e) {

frontend/src/components/Terminal.vue

+29-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
</template>
66

77
<script>
8-
import { Terminal } from "xterm";
8+
import { Terminal } from "@xterm/xterm";
9+
import { FitAddon } from "@xterm/addon-fit";
910
import { WebLinksAddon } from "xterm-addon-web-links";
1011
import { TERMINAL_COLS, TERMINAL_ROWS } from "../../../backend/util-common";
1112
@@ -122,10 +123,12 @@ export default {
122123
}
123124
});
124125
}
125-
126+
// Fit the terminal width to the div container size after terminal is created.
127+
this.updateTerminalSize();
126128
},
127129
128130
unmounted() {
131+
window.removeEventListener("resize", this.onResizeEvent); // Remove the resize event listener from the window object.
129132
this.$root.unbindTerminal(this.name);
130133
this.terminal.dispose();
131134
},
@@ -208,6 +211,30 @@ export default {
208211
}
209212
});
210213
});
214+
},
215+
216+
/**
217+
* Update the terminal size to fit the container size.
218+
*
219+
* If the terminalFitAddOn is not created, creates it, loads it and then fits the terminal to the appropriate size.
220+
* It then addes an event listener to the window object to listen for resize events and calls the fit method of the terminalFitAddOn.
221+
*/
222+
updateTerminalSize() {
223+
if (!Object.hasOwn(this, "terminalFitAddOn")) {
224+
this.terminalFitAddOn = new FitAddon();
225+
this.terminal.loadAddon(this.terminalFitAddOn);
226+
window.addEventListener("resize", this.onResizeEvent);
227+
}
228+
this.terminalFitAddOn.fit();
229+
},
230+
/**
231+
* Handles the resize event of the terminal component.
232+
*/
233+
onResizeEvent() {
234+
this.terminalFitAddOn.fit();
235+
let rows = this.terminal.rows;
236+
let cols = this.terminal.cols;
237+
this.$root.getSocket().emit("terminalResize", this.name, rows, cols);
211238
}
212239
}
213240
};

frontend/src/main.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ import { i18n } from "./i18n";
1010
// Dependencies
1111
import "bootstrap";
1212
import Toast, { POSITION, useToast } from "vue-toastification";
13-
import "xterm/lib/xterm.js";
13+
import "@xterm/xterm/lib/xterm.js";
1414

1515
// CSS
1616
import "@fontsource/jetbrains-mono";
1717
import "vue-toastification/dist/index.css";
18-
import "xterm/css/xterm.css";
18+
import "@xterm/xterm/css/xterm.css";
1919
import "./styles/main.scss";
2020

2121
// Minxins

frontend/src/mixins/socket.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { io } from "socket.io-client";
22
import { Socket } from "socket.io-client";
33
import { defineComponent } from "vue";
44
import jwtDecode from "jwt-decode";
5-
import { Terminal } from "xterm";
5+
import { Terminal } from "@xterm/xterm";
66

77
let socket : Socket;
88

package.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,6 @@
5555
"yaml": "~2.3.4"
5656
},
5757
"devDependencies": {
58-
"concurrently": "^8.2.2",
59-
"wait-on": "^7.2.0",
6058
"@actions/github": "^6.0.0",
6159
"@fontsource/jetbrains-mono": "^5.0.18",
6260
"@fortawesome/fontawesome-svg-core": "6.4.2",
@@ -71,8 +69,11 @@
7169
"@typescript-eslint/eslint-plugin": "~6.8.0",
7270
"@typescript-eslint/parser": "~6.8.0",
7371
"@vitejs/plugin-vue": "~4.5.2",
72+
"@xterm/addon-fit": "beta",
73+
"@xterm/xterm": "beta",
7474
"bootstrap": "5.3.2",
7575
"bootstrap-vue-next": "~0.14.10",
76+
"concurrently": "^8.2.2",
7677
"cross-env": "~7.0.3",
7778
"eslint": "~8.50.0",
7879
"eslint-plugin-jsdoc": "~46.8.2",
@@ -90,7 +91,7 @@
9091
"vue-qrcode": "~2.2.0",
9192
"vue-router": "~4.2.5",
9293
"vue-toastification": "2.0.0-rc.5",
93-
"xterm": "5.4.0-beta.37",
94+
"wait-on": "^7.2.0",
9495
"xterm-addon-web-links": "~0.9.0"
9596
}
9697
}

pnpm-lock.yaml

+23-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)