From e2819afce181e11c9348c439d44b40840e5976ab Mon Sep 17 00:00:00 2001 From: Zack Hankin Date: Sat, 16 Dec 2023 20:57:21 +1100 Subject: [PATCH] Terminal text cols adjusts to terminal container. (#285) Co-authored-by: Louis Lam --- .../terminal-socket-handler.ts | 43 +++++++++++++++++-- backend/terminal.ts | 1 + frontend/src/components/Terminal.vue | 31 ++++++++++++- frontend/src/main.ts | 4 +- frontend/src/mixins/socket.ts | 2 +- package.json | 7 +-- pnpm-lock.yaml | 31 +++++++++---- 7 files changed, 99 insertions(+), 20 deletions(-) diff --git a/backend/socket-handlers/terminal-socket-handler.ts b/backend/socket-handlers/terminal-socket-handler.ts index 3deed45c..9ae6656a 100644 --- a/backend/socket-handlers/terminal-socket-handler.ts +++ b/backend/socket-handlers/terminal-socket-handler.ts @@ -162,9 +162,44 @@ export class TerminalSocketHandler extends SocketHandler { } }); - // TODO: Resize Terminal - socket.on("terminalResize", async (rows : unknown) => { - - }); + // Resize Terminal + socket.on( + "terminalResize", + async (terminalName: unknown, rows: unknown, cols: unknown) => { + log.info("terminalResize", `Terminal: ${terminalName}`); + try { + checkLogin(socket); + if (typeof terminalName !== "string") { + throw new Error("Terminal name must be a string."); + } + + if (typeof rows !== "number") { + throw new Error("Command must be a number."); + } + if (typeof cols !== "number") { + throw new Error("Command must be a number."); + } + + let terminal = Terminal.getTerminal(terminalName); + + // log.info("terminal", terminal); + if (terminal instanceof Terminal) { + //log.debug("terminalInput", "Terminal found, writing to terminal."); + terminal.rows = rows; + terminal.cols = cols; + } else { + throw new Error(`${terminalName} Terminal not found.`); + } + } catch (e) { + log.debug( + "terminalResize", + // Added to prevent the lint error when adding the type + // and ts type checker saying type is unknown. + // @ts-ignore + `Error on ${terminalName}: ${e.message}` + ); + } + } + ); } } diff --git a/backend/terminal.ts b/backend/terminal.ts index 96a8b01b..dedc4e09 100644 --- a/backend/terminal.ts +++ b/backend/terminal.ts @@ -67,6 +67,7 @@ export class Terminal { set cols(cols : number) { this._cols = cols; + log.debug("Terminal", `Terminal cols: ${this._cols}`); // Added to check if cols is being set when changing terminal size. try { this.ptyProcess?.resize(this.cols, this.rows); } catch (e) { diff --git a/frontend/src/components/Terminal.vue b/frontend/src/components/Terminal.vue index 41551ba8..4391a579 100644 --- a/frontend/src/components/Terminal.vue +++ b/frontend/src/components/Terminal.vue @@ -5,7 +5,8 @@