|
| 1 | +import {FitAddon} from '@xterm/addon-fit' |
| 2 | +import {Terminal as XTerm} from '@xterm/xterm' |
| 3 | +import {X} from 'lucide-react' |
| 4 | +import {useTheme} from 'next-themes' |
| 5 | +import {useEffect, useRef} from 'react' |
| 6 | +import '@xterm/xterm/css/xterm.css' |
| 7 | +import {getOrCreateSession} from './ptysessions' |
| 8 | + |
| 9 | +const lightTheme = { |
| 10 | + background: '#ffffff', |
| 11 | + foreground: '#1e1e1e', |
| 12 | + cursor: '#1e1e1e', |
| 13 | + selectionBackground: '#add6ff', |
| 14 | + black: '#1e1e1e', |
| 15 | + red: '#cd3131', |
| 16 | + green: '#00bc7c', |
| 17 | + yellow: '#949800', |
| 18 | + blue: '#0451a5', |
| 19 | + magenta: '#bc05bc', |
| 20 | + cyan: '#0598bc', |
| 21 | + white: '#555555', |
| 22 | + brightBlack: '#666666', |
| 23 | + brightRed: '#cd3131', |
| 24 | + brightGreen: '#14ce14', |
| 25 | + brightYellow: '#b5ba00', |
| 26 | + brightBlue: '#0451a5', |
| 27 | + brightMagenta: '#bc05bc', |
| 28 | + brightCyan: '#0598bc', |
| 29 | + brightWhite: '#a5a5a5', |
| 30 | +} |
| 31 | + |
| 32 | +const darkTheme = { |
| 33 | + background: '#1e1e1e', |
| 34 | + foreground: '#d4d4d4', |
| 35 | + cursor: '#d4d4d4', |
| 36 | + selectionBackground: '#264f78', |
| 37 | + black: '#1e1e1e', |
| 38 | + red: '#f44747', |
| 39 | + green: '#6a9955', |
| 40 | + yellow: '#d7ba7d', |
| 41 | + blue: '#569cd6', |
| 42 | + magenta: '#c586c0', |
| 43 | + cyan: '#4ec9b0', |
| 44 | + white: '#d4d4d4', |
| 45 | + brightBlack: '#808080', |
| 46 | + brightRed: '#f44747', |
| 47 | + brightGreen: '#6a9955', |
| 48 | + brightYellow: '#d7ba7d', |
| 49 | + brightBlue: '#569cd6', |
| 50 | + brightMagenta: '#c586c0', |
| 51 | + brightCyan: '#4ec9b0', |
| 52 | + brightWhite: '#e8e8e8', |
| 53 | +} |
| 54 | + |
| 55 | +interface Props { |
| 56 | + sessionKey: string |
| 57 | + cwd: string |
| 58 | + onClose: () => void |
| 59 | +} |
| 60 | + |
| 61 | +export default function Terminal({sessionKey, cwd, onClose}: Props) { |
| 62 | + const containerRef = useRef<HTMLDivElement>(null) |
| 63 | + const xtermRef = useRef<XTerm | null>(null) |
| 64 | + const fitAddonRef = useRef<FitAddon | null>(null) |
| 65 | + const {resolvedTheme} = useTheme() |
| 66 | + const isDark = resolvedTheme === 'dark' |
| 67 | + |
| 68 | + useEffect(() => { |
| 69 | + if (xtermRef.current) { |
| 70 | + xtermRef.current.options.theme = isDark ? darkTheme : lightTheme |
| 71 | + } |
| 72 | + }, [isDark]) |
| 73 | + |
| 74 | + useEffect(() => { |
| 75 | + if (!containerRef.current) return |
| 76 | + |
| 77 | + const theme = isDark ? darkTheme : lightTheme |
| 78 | + |
| 79 | + const xterm = new XTerm({ |
| 80 | + cursorBlink: true, |
| 81 | + fontSize: 13, |
| 82 | + fontFamily: '"Fira Code", "SF Mono", Menlo, Monaco, "Courier New", monospace', |
| 83 | + theme, |
| 84 | + allowProposedApi: true, |
| 85 | + scrollback: 5000, |
| 86 | + }) |
| 87 | + |
| 88 | + const fitAddon = new FitAddon() |
| 89 | + xterm.loadAddon(fitAddon) |
| 90 | + xterm.open(containerRef.current) |
| 91 | + |
| 92 | + requestAnimationFrame(() => { |
| 93 | + fitAddon.fit() |
| 94 | + }) |
| 95 | + |
| 96 | + xtermRef.current = xterm |
| 97 | + fitAddonRef.current = fitAddon |
| 98 | + |
| 99 | + const session = getOrCreateSession(sessionKey, cwd, xterm.cols, xterm.rows) |
| 100 | + |
| 101 | + // Replay buffered output from previous views |
| 102 | + for (const chunk of session.buffer) { |
| 103 | + xterm.write(chunk) |
| 104 | + } |
| 105 | + |
| 106 | + // Connect live output forwarding |
| 107 | + session.writeToXterm = (text: string) => xterm.write(text) |
| 108 | + |
| 109 | + // Keyboard input -> PTY |
| 110 | + const inputListener = xterm.onData((data: string) => { |
| 111 | + session.pty.write(data) |
| 112 | + }) |
| 113 | + |
| 114 | + // PTY exit -> close panel |
| 115 | + const exitListener = session.pty.onExit(() => { |
| 116 | + onClose() |
| 117 | + }) |
| 118 | + |
| 119 | + // Resize PTY when container resizes |
| 120 | + const observer = new ResizeObserver(() => { |
| 121 | + requestAnimationFrame(() => { |
| 122 | + if (fitAddonRef.current && xtermRef.current) { |
| 123 | + fitAddonRef.current.fit() |
| 124 | + session.pty.resize(xtermRef.current.cols, xtermRef.current.rows) |
| 125 | + } |
| 126 | + }) |
| 127 | + }) |
| 128 | + observer.observe(containerRef.current) |
| 129 | + |
| 130 | + // Let CMD+J close the terminal (only on keydown) |
| 131 | + xterm.attachCustomKeyEventHandler((event: KeyboardEvent) => { |
| 132 | + if (event.type === 'keydown' && (event.metaKey || event.ctrlKey) && event.key === 'j') { |
| 133 | + event.preventDefault() |
| 134 | + onClose() |
| 135 | + return false |
| 136 | + } |
| 137 | + return true |
| 138 | + }) |
| 139 | + |
| 140 | + xterm.focus() |
| 141 | + |
| 142 | + return () => { |
| 143 | + observer.disconnect() |
| 144 | + session.writeToXterm = null |
| 145 | + inputListener.dispose() |
| 146 | + exitListener.dispose() |
| 147 | + xterm.dispose() |
| 148 | + xtermRef.current = null |
| 149 | + fitAddonRef.current = null |
| 150 | + } |
| 151 | + }, [sessionKey, cwd]) |
| 152 | + |
| 153 | + return ( |
| 154 | + <div className="flex flex-col h-full"> |
| 155 | + <div className="flex items-center justify-between px-3 py-1 bg-muted/50 border-b border-border"> |
| 156 | + <span className="text-xs text-muted-foreground font-medium">Terminal</span> |
| 157 | + <button |
| 158 | + type="button" |
| 159 | + onClick={onClose} |
| 160 | + className="p-0.5 rounded hover:bg-muted-foreground/20 text-muted-foreground" |
| 161 | + title="Close terminal (⌘J)" |
| 162 | + > |
| 163 | + <X size={14} /> |
| 164 | + </button> |
| 165 | + </div> |
| 166 | + <div className="flex-1 min-h-0 overflow-hidden p-2"> |
| 167 | + <div ref={containerRef} className="h-full w-full" /> |
| 168 | + </div> |
| 169 | + </div> |
| 170 | + ) |
| 171 | +} |
0 commit comments