simplify terminal code
This commit is contained in:
parent
939b3a29d4
commit
a437ccebc9
|
|
@ -94,7 +94,6 @@ vanX.list(EditorTabs, editors, EditorGrid);
|
|||
|
||||
document.addEventListener("keyup", (e) => {
|
||||
if (e.key === "t" && e.altKey) {
|
||||
console.log("Opening terminal");
|
||||
addTerminal();
|
||||
}
|
||||
});
|
||||
|
|
|
|||
|
|
@ -61,7 +61,7 @@ export class Terminal implements Displayable {
|
|||
);
|
||||
|
||||
this.unsubTerminalExit = window.electronAPI.onTerminalExit(
|
||||
(id, exitCode, signal) => {
|
||||
(id, exitCode) => {
|
||||
if (id === this.terminalId) {
|
||||
this.term.writeln(
|
||||
`\r\n[Process exited with code ${exitCode}]`,
|
||||
|
|
@ -101,10 +101,8 @@ export class Terminal implements Displayable {
|
|||
this.dom.clientWidth > 0 &&
|
||||
this.dom.clientHeight > 0
|
||||
) {
|
||||
console.log("Old size: ", this.term.rows, this.term.cols);
|
||||
this.fitAddon.fit();
|
||||
const { cols, rows } = this.term;
|
||||
console.log("New size: ", rows, cols);
|
||||
window.electronAPI.resizeTerminal(this.terminalId, cols, rows);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -100,22 +100,7 @@ app.whenReady().then(() => {
|
|||
ipcMain.handle(
|
||||
"terminal:create",
|
||||
async (event, shell?: string, args?: string[]) => {
|
||||
const terminalId = terminalManager.createTerminal(shell, args);
|
||||
|
||||
// Set up data forwarding to the renderer
|
||||
terminalManager.on("terminal-data", (id, data) => {
|
||||
if (id === terminalId) {
|
||||
event.sender.send("terminal:data", id, data);
|
||||
}
|
||||
});
|
||||
|
||||
terminalManager.on("terminal-exit", (id, exitCode, signal) => {
|
||||
if (id === terminalId) {
|
||||
event.sender.send("terminal:exit", id, exitCode, signal);
|
||||
}
|
||||
});
|
||||
|
||||
return terminalId;
|
||||
return terminalManager.createTerminal(event, shell, args);
|
||||
},
|
||||
);
|
||||
|
||||
|
|
@ -137,10 +122,6 @@ app.whenReady().then(() => {
|
|||
return terminalManager.closeTerminal(id);
|
||||
});
|
||||
|
||||
ipcMain.handle("terminal:list", async () => {
|
||||
return terminalManager.listTerminals();
|
||||
});
|
||||
|
||||
createWindow();
|
||||
if (process.platform === "darwin") {
|
||||
app.on("activate", function () {
|
||||
|
|
|
|||
|
|
@ -1,15 +1,18 @@
|
|||
import * as pty from "node-pty";
|
||||
import { EventEmitter } from "events";
|
||||
|
||||
export interface TerminalInstance {
|
||||
ptyProcess: pty.IPty;
|
||||
}
|
||||
|
||||
export class TerminalManager extends EventEmitter {
|
||||
export class TerminalManager {
|
||||
private terminals: Map<string, TerminalInstance> = new Map();
|
||||
private nextId: number = 1;
|
||||
|
||||
createTerminal(shell?: string, args?: string[]): string {
|
||||
createTerminal(
|
||||
event: Electron.IpcMainInvokeEvent,
|
||||
shell?: string,
|
||||
args?: string[],
|
||||
): string {
|
||||
const id = `terminal-${this.nextId++}`;
|
||||
|
||||
// Default shell based on platform
|
||||
|
|
@ -30,11 +33,11 @@ export class TerminalManager extends EventEmitter {
|
|||
};
|
||||
|
||||
ptyProcess.onData((data) => {
|
||||
this.emit("terminal-data", id, data);
|
||||
event.sender.send("terminal:data", id, data);
|
||||
});
|
||||
|
||||
ptyProcess.onExit(({ exitCode, signal }) => {
|
||||
this.emit("terminal-exit", id, exitCode, signal);
|
||||
ptyProcess.onExit(({ exitCode }) => {
|
||||
event.sender.send("terminal:exit", id, exitCode);
|
||||
this.terminals.delete(id);
|
||||
});
|
||||
|
||||
|
|
@ -42,10 +45,6 @@ export class TerminalManager extends EventEmitter {
|
|||
return id;
|
||||
}
|
||||
|
||||
getTerminal(id: string): TerminalInstance | undefined {
|
||||
return this.terminals.get(id);
|
||||
}
|
||||
|
||||
resizeTerminal(id: string, cols: number, rows: number): boolean {
|
||||
const terminal = this.terminals.get(id);
|
||||
if (terminal) {
|
||||
|
|
@ -73,10 +72,6 @@ export class TerminalManager extends EventEmitter {
|
|||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
listTerminals(): string[] {
|
||||
return Array.from(this.terminals.keys());
|
||||
}
|
||||
}
|
||||
|
||||
export const terminalManager = new TerminalManager();
|
||||
|
|
|
|||
|
|
@ -82,9 +82,6 @@ contextBridge.exposeInMainWorld("electronAPI", {
|
|||
closeTerminal: (id: string) =>
|
||||
ipcRenderer.invoke("terminal:close", id) as Promise<boolean>,
|
||||
|
||||
listTerminals: () =>
|
||||
ipcRenderer.invoke("terminal:list") as Promise<string[]>,
|
||||
|
||||
// Terminal events (subscribe/unsubscribe)
|
||||
onTerminalData: (callback: (id: string, data: string) => void) => {
|
||||
terminalDataCallbacks.add(callback);
|
||||
|
|
|
|||
|
|
@ -47,7 +47,6 @@ declare global {
|
|||
) => Promise<boolean>;
|
||||
writeToTerminal: (id: string, data: string) => Promise<boolean>;
|
||||
closeTerminal: (id: string) => Promise<boolean>;
|
||||
listTerminals: () => Promise<string[]>;
|
||||
onTerminalData: (
|
||||
callback: (id: string, data: string) => void,
|
||||
) => () => void;
|
||||
|
|
|
|||
Loading…
Reference in New Issue