|
| 1 | +// This imports the webGL addon for xterm, which allows for gpu acceleration |
| 2 | +const { WebglAddon } = require("xterm-addon-webgl"); |
| 3 | +// Imports the getSettings function |
| 4 | +const { getSettings } = require("../BackEnd/settings"); |
| 5 | +// This imports the FitAddon which allows for window resizing |
| 6 | +//const { FitAddon } = require("../xterm-addon-fit/typings/xterm-addon-fit"); |
| 7 | +const { FitAddon } = require("xterm-addon-fit"); |
| 8 | +// This gets the ipc from electron |
| 9 | +const ipc = require("electron").ipcRenderer; |
| 10 | +// This gets the settings variables required in this file |
| 11 | +const { bgColor, CursorBlink, FontFamily, FontSize } = getSettings(); |
| 12 | + |
| 13 | +//const FitAddon = require("xterm-addon-fit"); |
| 14 | +// This creates the xterm.js front end |
| 15 | +var term = new Terminal({ |
| 16 | + // Sets the size of the text |
| 17 | + fontSize: FontSize, |
| 18 | + // Sets the font of the text |
| 19 | + fontFamily: FontFamily, |
| 20 | + // Sets if the cursor blinks or not |
| 21 | + cursorBlink: CursorBlink, |
| 22 | + // This gives the terminal name to other programs like neofetch |
| 23 | + termProgram: "Kaiium", |
| 24 | + experimentalCharAtlas: "dynamic", |
| 25 | + // This allows xterm.js to use rgba backgrounds |
| 26 | + allowTransparency: "true", |
| 27 | +}); |
| 28 | +// This sets the background color of xterm.js |
| 29 | +term.setOption("theme", { |
| 30 | + background: bgColor, |
| 31 | +}); |
| 32 | +// This instantiates the webGL addon |
| 33 | +const webgl = new WebglAddon(); |
| 34 | +//var fitAddon = new FitAddon.FitAddon(); |
| 35 | +// This instantiates the fitAddon addon |
| 36 | +const fitAddon = new FitAddon(); |
| 37 | + |
| 38 | +/*term.onData(function (data) { |
| 39 | + term.write(data); |
| 40 | +});*/ |
| 41 | +// This sets the background color of the window itself, so that you don't get white borders around xterm.js |
| 42 | +document.body.style.backgroundColor = bgColor; |
| 43 | +// This spawns xterm.js in the terminal div |
| 44 | +term.open(document.getElementById("terminal")); |
| 45 | +// Loads the fitAddon addon into xterm.js |
| 46 | +term.loadAddon(fitAddon); |
| 47 | +// Loads the webGL addon into xterm.js |
| 48 | +term.loadAddon(webgl); |
| 49 | +// This fits xterm.js into your window |
| 50 | +fitAddon.fit(); |
| 51 | + |
| 52 | +// Logs xterm.js dimensions, call this function to debug window resizing. |
| 53 | +function log() { |
| 54 | + console.log( |
| 55 | + term.cols, |
| 56 | + term.rows, |
| 57 | + viewport.style.lineHeight, |
| 58 | + viewport.style.height |
| 59 | + ); |
| 60 | +} |
| 61 | +// Gets the xterm viewport, which appears after spawning xterm.js |
| 62 | +var viewport = document.querySelector(".xterm-viewport"); |
| 63 | +//var canvas = document.querySelectorAll("canvas"); |
| 64 | +// This logs in the inspect element console, enable when needed |
| 65 | +// log(); |
| 66 | +// Fits again, because im paranoid |
| 67 | +term.onRender = function () { |
| 68 | + fitAddon.fit(); |
| 69 | +}; |
| 70 | +// Same here |
| 71 | +window.onresize = function () { |
| 72 | + // Logs the window dimensions when resized |
| 73 | + //log(); |
| 74 | + fitAddon.fit(); |
| 75 | +}; |
| 76 | +// Writes incoming data from the pty process into xterm.js |
| 77 | +ipc.on("terminal.incomingData", (event, data) => { |
| 78 | + term.write(data); |
| 79 | +}); |
| 80 | +// Does the same kindof |
| 81 | +term.onData((e) => { |
| 82 | + ipc.send("terminal.keystroke", e); |
| 83 | +}); |
| 84 | +// To debug the background color, enable if needed |
| 85 | +// console.log(bgColor); |
| 86 | +// This resizes the pty process itself |
| 87 | +term.onResize(function (size) { |
| 88 | + ipc.send("terminal.resize", size); |
| 89 | +}); |
| 90 | +// This writes the version number into the terminal window |
| 91 | +term.write("Kaiium V1.0.9 "); |
| 92 | +// This handles the copy and paste for the pty process |
| 93 | +term.attachCustomKeyEventHandler((arg) => { |
| 94 | + if (arg.ctrlKey && arg.code === "KeyV" && arg.type === "keydown") { |
| 95 | + navigator.clipboard.readText().then((text) => { |
| 96 | + term.write(text); |
| 97 | + }); |
| 98 | + } |
| 99 | + return true; |
| 100 | +}); |
0 commit comments