-
Notifications
You must be signed in to change notification settings - Fork 246
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
205 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { MainChannels } from '@onlook/models/constants'; | ||
import { ipcMain } from 'electron'; | ||
import terminal from '../terminal'; | ||
|
||
export function listenForTerminalMessages() { | ||
ipcMain.handle(MainChannels.TERMINAL_CREATE, (e: Electron.IpcMainInvokeEvent, args) => { | ||
const { id, options } = args as { id: string; options: { cwd?: string } }; | ||
terminal.createTerminal(id, options); | ||
}); | ||
|
||
ipcMain.handle(MainChannels.TERMINAL_INPUT, (_, args) => { | ||
const { id, data } = args; | ||
terminal.write(id, data); | ||
}); | ||
|
||
ipcMain.handle(MainChannels.TERMINAL_RESIZE, (_, args) => { | ||
const { id, cols, rows } = args; | ||
terminal.resize(id, cols, rows); | ||
}); | ||
|
||
ipcMain.handle(MainChannels.TERMINAL_KILL, (_, args) => { | ||
const { id } = args; | ||
terminal.kill(id); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { MainChannels } from '@onlook/models/constants'; | ||
import * as pty from 'node-pty'; | ||
import os from 'os'; | ||
import { mainWindow } from '..'; | ||
|
||
class TerminalManager { | ||
private static instance: TerminalManager; | ||
private processes: Map<string, pty.IPty>; | ||
|
||
private constructor() { | ||
this.processes = new Map(); | ||
} | ||
|
||
static getInstance(): TerminalManager { | ||
if (!TerminalManager.instance) { | ||
TerminalManager.instance = new TerminalManager(); | ||
} | ||
return TerminalManager.instance; | ||
} | ||
|
||
createTerminal(id: string, options?: { cwd?: string }): void { | ||
const shell = os.platform() === 'win32' ? 'powershell.exe' : 'bash'; | ||
|
||
const ptyProcess = pty.spawn(shell, [], { | ||
name: 'xterm-color', | ||
cols: 80, | ||
rows: 24, | ||
cwd: options?.cwd ?? process.env.HOME, | ||
env: process.env, | ||
}); | ||
|
||
ptyProcess.onData((data: string) => { | ||
mainWindow?.webContents.send(MainChannels.TERMINAL_DATA_STREAM, { | ||
id, | ||
data, | ||
}); | ||
}); | ||
|
||
this.processes.set(id, ptyProcess); | ||
} | ||
|
||
write(id: string, data: string): void { | ||
this.processes.get(id)?.write(data); | ||
} | ||
|
||
resize(id: string, cols: number, rows: number): void { | ||
this.processes.get(id)?.resize(cols, rows); | ||
} | ||
|
||
kill(id: string): void { | ||
const process = this.processes.get(id); | ||
if (process) { | ||
process.kill(); | ||
this.processes.delete(id); | ||
} | ||
} | ||
|
||
killAll(): void { | ||
this.processes.forEach((process) => process.kill()); | ||
this.processes.clear(); | ||
} | ||
} | ||
|
||
export default TerminalManager.getInstance(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import { MainChannels } from '@onlook/models/constants'; | ||
import { FitAddon } from '@xterm/addon-fit'; | ||
import { useEffect, useRef } from 'react'; | ||
import { Terminal as XTerm } from 'xterm'; | ||
import 'xterm/css/xterm.css'; | ||
|
||
interface TerminalProps { | ||
id?: string; | ||
} | ||
|
||
interface TerminalMessage { | ||
id: string; | ||
data: string; | ||
} | ||
|
||
const TERMINAL_CONFIG = { | ||
cursorBlink: true, | ||
fontSize: 14, | ||
fontFamily: 'monospace', | ||
} as const; | ||
|
||
const Terminal = ({ id = 'default' }: TerminalProps) => { | ||
const terminalRef = useRef<HTMLDivElement>(null); | ||
const xtermRef = useRef<XTerm>(); | ||
|
||
useEffect(() => { | ||
if (!terminalRef.current) { | ||
return; | ||
} | ||
|
||
const setupTerminal = async () => { | ||
await window.api.invoke(MainChannels.TERMINAL_CREATE, { id }); | ||
|
||
const term = new XTerm(TERMINAL_CONFIG); | ||
const fitAddon = new FitAddon(); | ||
|
||
initializeTerminal(term, fitAddon); | ||
setupEventListeners(term, fitAddon); | ||
|
||
xtermRef.current = term; | ||
return term; | ||
}; | ||
|
||
const cleanup = setupTerminal(); | ||
|
||
return () => { | ||
cleanup.then((term) => { | ||
term.dispose(); | ||
window.api.invoke(MainChannels.TERMINAL_KILL, { id }); | ||
}); | ||
}; | ||
}, [id]); | ||
|
||
const initializeTerminal = (term: XTerm, fitAddon: FitAddon) => { | ||
term.loadAddon(fitAddon); | ||
term.open(terminalRef.current!); | ||
fitAddon.fit(); | ||
}; | ||
|
||
const setupEventListeners = (term: XTerm, fitAddon: FitAddon) => { | ||
const handleResize = () => { | ||
fitAddon.fit(); | ||
const { cols, rows } = term; | ||
window.api.invoke(MainChannels.TERMINAL_RESIZE, { id, cols, rows }); | ||
}; | ||
|
||
const handleTerminalData = (message: TerminalMessage) => { | ||
if (message.id === id) { | ||
term.write(message.data); | ||
} | ||
}; | ||
|
||
term.onData((data) => { | ||
window.api.invoke(MainChannels.TERMINAL_INPUT, { id, data }); | ||
}); | ||
|
||
window.api.on(MainChannels.TERMINAL_DATA_STREAM, handleTerminalData); | ||
window.addEventListener('resize', handleResize); | ||
|
||
handleResize(); | ||
|
||
return () => { | ||
window.removeEventListener('resize', handleResize); | ||
}; | ||
}; | ||
|
||
return ( | ||
<div className="p-2 bg-black"> | ||
<div ref={terminalRef} className="h-full w-full" /> | ||
</div> | ||
); | ||
}; | ||
|
||
export default Terminal; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters