Skip to content
This repository was archived by the owner on Oct 22, 2021. It is now read-only.

Commit 2d23d2a

Browse files
committed
✨ [Core] Backend support for extra TTYs
Look at this madlad making one feature in two commits #260
1 parent f7d6970 commit 2d23d2a

File tree

2 files changed

+70
-3
lines changed

2 files changed

+70
-3
lines changed

src/_boot.js

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,14 @@ process.on("uncaughtException", e => {
55
signale.fatal(e);
66
dialog.showErrorBox("eEDEX-UI failed to launch", e.message || "Cannot retrieve error message.");
77
if (tty) {
8-
tty.tty.kill();
8+
tty.close();
9+
}
10+
if (extraTtys) {
11+
Object.keys(extraTtys).forEach(key => {
12+
if (extraTtys[key] !== null) {
13+
extraTtys[key].close();
14+
}
15+
});
916
}
1017
});
1118

@@ -26,7 +33,7 @@ ipc.on("log", (e, type, content) => {
2633
signale[type](content);
2734
});
2835

29-
var win, tty;
36+
var win, tty, extraTtys;
3037
const settingsFile = path.join(electron.app.getPath("userData"), "settings.json");
3138
const themesDir = path.join(electron.app.getPath("userData"), "themes");
3239
const innerThemesDir = path.join(__dirname, "assets/themes");
@@ -173,6 +180,57 @@ app.on('ready', () => {
173180
});
174181

175182
createWindow(settings);
183+
184+
// Support for more terminals, used for creating tabs (currently limited to 4 extra terms)
185+
extraTtys = {};
186+
let basePort = settings.port || 3000;
187+
basePort = Number(basePort) + 2;
188+
189+
for (let i = 0; i < 4; i++) {
190+
extraTtys[basePort+i] = null;
191+
}
192+
193+
ipc.on("ttyspawn", (e, arg) => {
194+
let port = null;
195+
Object.keys(extraTtys).forEach(key => {
196+
if (extraTtys[key] === null && port === null) {
197+
extraTtys[key] = {};
198+
port = key;
199+
}
200+
});
201+
202+
if (port === null) {
203+
signale.error("TTY spawn denied (Reason: exceeded max TTYs number)");
204+
e.sender.send("ttyspawn-reply", "ERROR: max number of ttys reached");
205+
} else {
206+
signale.pending(`Creating new TTY process on port ${port}`);
207+
let term = new Terminal({
208+
role: "server",
209+
shell: settings.shell.split(" ")[0],
210+
params: settings.shell.split(" ").splice(1),
211+
cwd: tty.tty._cwd || settings.cwd,
212+
port: port
213+
});
214+
signale.success(`New terminal back-end initialized at ${port}`);
215+
term.onclosed = (code, signal) => {
216+
tty.ondisconnected = () => {};
217+
signale.complete(`TTY exited at ${port}`, code, signal);
218+
extraTtys[term.port] = null;
219+
delete term;
220+
};
221+
term.onopened = () => {
222+
signale.success(`TTY ${port} connected to frontend`);
223+
};
224+
term.ondisconnected = () => {
225+
term.onclosed = () => {};
226+
term.close();
227+
extraTtys[term.port] = null;
228+
delete term;
229+
};
230+
231+
extraTtys[port] = term;
232+
}
233+
});
176234
});
177235

178236
app.on('web-contents-created', (e, contents) => {
@@ -193,6 +251,11 @@ app.on('window-all-closed', () => {
193251
});
194252

195253
app.on('before-quit', () => {
196-
tty.tty.kill();
254+
tty.close();
255+
Object.keys(extraTtys).forEach(key => {
256+
if (extraTtys[key] !== null) {
257+
extraTtys[key].close();
258+
}
259+
});
197260
signale.complete("Shutting down...");
198261
});

src/classes/terminal.class.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,10 @@ class Terminal {
314314
}
315315
});
316316
});
317+
318+
this.close = () => {
319+
this.tty.kill();
320+
};
317321
} else {
318322
throw "Unknown purpose";
319323
}

0 commit comments

Comments
 (0)