-
Notifications
You must be signed in to change notification settings - Fork 7
feat: add integrated terminal panel #88
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| import * as pty from "node-pty"; | ||
| import os from "os"; | ||
| import { BrowserWindow } from "electron"; | ||
|
|
||
| interface TerminalInstance { | ||
| pty: pty.IPty; | ||
| windowId: number; | ||
| } | ||
|
|
||
| const terminals = new Map<string, TerminalInstance>(); | ||
| let idCounter = 0; | ||
|
|
||
| function getDefaultShell(): string { | ||
| if (process.platform === "win32") { | ||
| return process.env.COMSPEC || "powershell.exe"; | ||
| } | ||
| return process.env.SHELL || "/bin/bash"; | ||
| } | ||
|
|
||
| export function createTerminal( | ||
| window: BrowserWindow, | ||
| cwd: string, | ||
| cols: number, | ||
| rows: number, | ||
| ): string { | ||
| const id = `term-${++idCounter}`; | ||
| const shell = getDefaultShell(); | ||
|
|
||
| const env: Record<string, string> = { ...process.env } as Record<string, string>; | ||
| if (!env.LANG) env.LANG = "ru_RU.UTF-8"; | ||
| if (!env.LC_CTYPE) env.LC_CTYPE = env.LANG; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This |
||
| if (!env.LC_ALL) env.LC_ALL = env.LANG; | ||
|
|
||
| const ptyProcess = pty.spawn(shell, [], { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The At minimum we need:
This is especially important since CodeMux supports remote access — a compromised renderer could spawn shells in arbitrary directories. |
||
| name: "xterm-256color", | ||
| cols, | ||
| rows, | ||
| cwd, | ||
| env, | ||
| }); | ||
|
|
||
| ptyProcess.onData((data) => { | ||
| if (window.isDestroyed()) return; | ||
| window.webContents.send("terminal:data", id, data); | ||
| }); | ||
|
|
||
| ptyProcess.onExit(() => { | ||
| terminals.delete(id); | ||
| if (!window.isDestroyed()) { | ||
| window.webContents.send("terminal:exit", id); | ||
| } | ||
| }); | ||
|
|
||
| terminals.set(id, { pty: ptyProcess, windowId: window.id }); | ||
| return id; | ||
| } | ||
|
|
||
| export function writeTerminal(id: string, data: string): void { | ||
| const term = terminals.get(id); | ||
| if (term) { | ||
| term.pty.write(data); | ||
| } | ||
| } | ||
|
|
||
| export function resizeTerminal(id: string, cols: number, rows: number): void { | ||
| const term = terminals.get(id); | ||
| if (term) { | ||
| term.pty.resize(cols, rows); | ||
| } | ||
| } | ||
|
|
||
| export function destroyTerminal(id: string): void { | ||
| const term = terminals.get(id); | ||
| if (term) { | ||
| term.pty.kill(); | ||
| terminals.delete(id); | ||
| } | ||
| } | ||
|
|
||
| export function destroyAllTerminals(): void { | ||
| for (const [id, term] of terminals) { | ||
| term.pty.kill(); | ||
| terminals.delete(id); | ||
| } | ||
| } | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Also — there's no error handling around |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Per the architecture discussion — the terminal service should go through the Gateway WebSocket layer (as new request/notification types like
terminal.create,terminal.write,terminal.data) rather than IPC.Reasoning:
node-pty+xterm.jsnatively supports WebSocket transport (same as VS Code Server / code-server)electron-api.tswrapper all become unnecessaryThe
TerminalPanelfrontend stays mostly the same — just swapterminalAPI.*calls forgateway.terminal*()methods.