some terminal session fixes

This commit is contained in:
Amrit Subramanian 2025-10-02 13:22:03 -04:00
parent 272f763e71
commit 7fd4968a35
2 changed files with 47 additions and 52 deletions

94
main.ts
View file

@ -5,6 +5,53 @@ import * as os from "os";
let mainWindow: BrowserWindow; let mainWindow: BrowserWindow;
const terminals = new Map<string, pty.IPty>(); const terminals = new Map<string, pty.IPty>();
// Create new terminal
ipcMain.on("create-terminal", (event) => {
const terminalId = `terminal-${Date.now()}`;
const shell = os.platform() === "darwin" ? "zsh" : "bash";
const ptyProcess = pty.spawn(shell, [], {
name: "xterm-color",
cols: 80,
rows: 30,
cwd: process.env.HOME,
env: process.env,
});
terminals.set(terminalId, ptyProcess);
ptyProcess.onData((data) => {
mainWindow.webContents.send("terminal-output", terminalId, data);
});
event.reply("terminal-created", terminalId);
});
// Handle terminal input
ipcMain.on("terminal-input", (_event, terminalId: string, data: string) => {
const terminal = terminals.get(terminalId);
if (terminal) {
terminal.write(data);
}
});
// Handle terminal resize
ipcMain.on("terminal-resize", (_event, terminalId: string, cols: number, rows: number) => {
const terminal = terminals.get(terminalId);
if (terminal) {
terminal.resize(cols, rows);
}
});
// Handle terminal close
ipcMain.on("close-terminal", (_event, terminalId: string) => {
const terminal = terminals.get(terminalId);
if (terminal) {
terminal.kill();
terminals.delete(terminalId);
}
});
const createWindow = () => { const createWindow = () => {
mainWindow = new BrowserWindow({ mainWindow = new BrowserWindow({
width: 1400, width: 1400,
@ -16,53 +63,6 @@ const createWindow = () => {
}); });
mainWindow.loadFile("index.html"); mainWindow.loadFile("index.html");
// Create new terminal
ipcMain.on("create-terminal", (event) => {
const terminalId = `terminal-${Date.now()}`;
const shell = os.platform() === "darwin" ? "zsh" : "bash";
const ptyProcess = pty.spawn(shell, [], {
name: "xterm-color",
cols: 80,
rows: 30,
cwd: process.env.HOME,
env: process.env,
});
terminals.set(terminalId, ptyProcess);
ptyProcess.onData((data) => {
mainWindow.webContents.send("terminal-output", terminalId, data);
});
event.reply("terminal-created", terminalId);
});
// Handle terminal input
ipcMain.on("terminal-input", (_event, terminalId: string, data: string) => {
const terminal = terminals.get(terminalId);
if (terminal) {
terminal.write(data);
}
});
// Handle terminal resize
ipcMain.on("terminal-resize", (_event, terminalId: string, cols: number, rows: number) => {
const terminal = terminals.get(terminalId);
if (terminal) {
terminal.resize(cols, rows);
}
});
// Handle terminal close
ipcMain.on("close-terminal", (_event, terminalId: string) => {
const terminal = terminals.get(terminalId);
if (terminal) {
terminal.kill();
terminals.delete(terminalId);
}
});
}; };
app.whenReady().then(() => { app.whenReady().then(() => {

View file

@ -198,8 +198,3 @@ ipcRenderer.on("terminal-created", (_event, terminalId: string) => {
document.getElementById("new-terminal")?.addEventListener("click", () => { document.getElementById("new-terminal")?.addEventListener("click", () => {
ipcRenderer.send("create-terminal"); ipcRenderer.send("create-terminal");
}); });
// Create first terminal on load
window.addEventListener("DOMContentLoaded", () => {
ipcRenderer.send("create-terminal");
});