-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTerminalPanel.tsx
More file actions
223 lines (203 loc) · 6.99 KB
/
Copy pathTerminalPanel.tsx
File metadata and controls
223 lines (203 loc) · 6.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import { useEffect, useRef, useCallback } from 'react'
import { useStore } from '../stores/useStore'
import { Terminal } from '@xterm/xterm'
import { FitAddon } from '@xterm/addon-fit'
import { WebLinksAddon } from '@xterm/addon-web-links'
import '@xterm/xterm/css/xterm.css'
function getTermTheme(isLight: boolean) {
return {
background: isLight ? '#f5f6f8' : '#0d0d1a',
foreground: isLight ? '#1a1a2e' : '#e0e0e0',
cursor: isLight ? '#3b7dd8' : '#4a9eff',
selectionBackground: isLight ? '#3b7dd833' : '#4a9eff44',
black: '#555555',
red: '#ef4444',
green: '#4ade80',
yellow: '#fbbf24',
blue: '#4a9eff',
magenta: '#c084fc',
cyan: '#22d3ee',
white: isLight ? '#1a1a2e' : '#e0e0e0',
brightBlack: '#888888',
brightRed: '#f87171',
brightGreen: '#6ee7a0',
brightYellow: '#fcd34d',
brightBlue: '#6ab0ff',
brightMagenta: '#d8b4fe',
brightCyan: '#67e8f9',
brightWhite: isLight ? '#000000' : '#ffffff'
}
}
function TerminalInstance({ tabId }: { tabId: string }) {
const ref = useRef<HTMLDivElement>(null)
const termRef = useRef<Terminal | null>(null)
const fitRef = useRef<FitAddon | null>(null)
const activeTabId = useStore(s => s.activeTabId)
const theme = useStore(s => s.theme)
const isActive = activeTabId === tabId
const isLight = theme === 'light'
const initTerminal = useCallback(() => {
if (!ref.current) return
if (termRef.current) return
const term = new Terminal({
fontSize: 13,
fontFamily: '"SF Mono", Menlo, Monaco, "Courier New", monospace',
theme: getTermTheme(isLight),
cursorBlink: true,
cursorStyle: 'bar',
scrollback: 5000,
allowTransparency: true,
allowProposedApi: true
})
const fit = new FitAddon()
const webLinks = new WebLinksAddon()
term.loadAddon(fit)
term.loadAddon(webLinks)
term.open(ref.current)
try { fit.fit() } catch {}
term.onData(data => { window.api.ptyWrite(tabId, data) })
termRef.current = term
fitRef.current = fit
}, [tabId])
useEffect(() => { initTerminal() }, [initTerminal])
useEffect(() => {
return () => {
if (termRef.current) {
termRef.current.dispose()
termRef.current = null
}
}
}, [])
useEffect(() => {
if (termRef.current) {
termRef.current.options.theme = getTermTheme(isLight)
}
}, [isLight])
useEffect(() => {
const unsub = window.api.onPtyData(({ tabId: tid, data }) => {
if (tid === tabId && termRef.current) termRef.current.write(data)
})
return unsub
}, [tabId])
useEffect(() => {
const unsub = window.api.onPtyExit(({ tabId: tid }) => {
if (tid === tabId && termRef.current) {
termRef.current.write('\r\n\x1b[90m[Process exited]\x1b[0m\r\n')
}
})
return unsub
}, [tabId])
useEffect(() => {
if (isActive && termRef.current && fitRef.current) {
setTimeout(() => {
try {
fitRef.current!.fit()
termRef.current!.focus()
const dims = fitRef.current!.proposeDimensions()
if (dims) window.api.ptyResize(tabId, dims.cols, dims.rows)
} catch {}
}, 50)
}
}, [isActive, tabId])
useEffect(() => {
const handleResize = () => {
if (fitRef.current && termRef.current) {
try {
fitRef.current.fit()
const dims = fitRef.current.proposeDimensions()
if (dims) window.api.ptyResize(tabId, dims.cols, dims.rows)
} catch {}
}
}
window.addEventListener('resize', handleResize)
return () => window.removeEventListener('resize', handleResize)
}, [tabId])
return (
<div
ref={ref}
style={{
width: '100%', height: '100%',
display: isActive ? 'block' : 'none',
padding: '4px 0 0 4px'
}}
/>
)
}
export function TerminalPanel({ fullscreen = false }: { fullscreen?: boolean }) {
const terminalTabs = useStore(s => s.terminalTabs)
const activeTabId = useStore(s => s.activeTabId)
const setActiveTab = useStore(s => s.setActiveTab)
const closeTab = useStore(s => s.closeTab)
const spawnTerminal = useStore(s => s.spawnTerminal)
const setTerminalFullscreen = useStore(s => s.setTerminalFullscreen)
useEffect(() => {
if (!fullscreen) return
const handler = (e: KeyboardEvent) => {
if (e.key === 'Escape') setTerminalFullscreen(false)
}
window.addEventListener('keydown', handler)
return () => window.removeEventListener('keydown', handler)
}, [fullscreen, setTerminalFullscreen])
if (terminalTabs.length === 0) return null
return (
<div style={{
height: fullscreen ? '100vh' : 300,
minHeight: fullscreen ? '100vh' : 200,
borderTop: fullscreen ? 'none' : '1px solid var(--border)',
display: 'flex', flexDirection: 'column',
background: 'var(--terminal-bg)'
}}>
<div style={{
display: 'flex', alignItems: 'center',
background: 'var(--bg-secondary)',
borderBottom: '1px solid var(--border)',
padding: '0 8px', height: 32, gap: 2, flexShrink: 0
}}>
{terminalTabs.map(tab => (
<div
key={tab.id}
onClick={() => setActiveTab(tab.id)}
style={{
padding: '4px 10px', fontSize: 11,
borderRadius: '4px 4px 0 0', cursor: 'pointer',
background: activeTabId === tab.id ? 'var(--bg-primary)' : 'transparent',
color: activeTabId === tab.id ? 'var(--text-primary)' : 'var(--text-secondary)',
display: 'flex', alignItems: 'center', gap: 6,
border: activeTabId === tab.id ? '1px solid var(--border)' : '1px solid transparent',
borderBottom: 'none', maxWidth: 160, overflow: 'hidden', whiteSpace: 'nowrap'
}}
>
<span style={{ overflow: 'hidden', textOverflow: 'ellipsis' }}>{tab.title}</span>
<span
onClick={e => { e.stopPropagation(); closeTab(tab.id) }}
style={{ color: 'var(--text-muted)', fontSize: 14, lineHeight: 1, padding: '0 2px' }}
>×</span>
</div>
))}
<button
onClick={spawnTerminal}
style={{
background: 'none', border: 'none', color: 'var(--text-muted)',
fontSize: 16, cursor: 'pointer', padding: '0 6px', lineHeight: '32px'
}}
>+</button>
<button
onClick={() => setTerminalFullscreen(!fullscreen)}
title={fullscreen ? 'Exit fullscreen (Esc)' : 'Fullscreen'}
style={{
marginLeft: 'auto', background: 'none', border: 'none',
color: fullscreen ? 'var(--accent)' : 'var(--text-muted)',
cursor: 'pointer', fontSize: 14, padding: '4px 6px'
}}
>
{fullscreen ? '⬒' : '⬓'}
</button>
</div>
<div style={{ flex: 1, position: 'relative', overflow: 'hidden' }}>
{terminalTabs
.filter(tab => tab.id === activeTabId)
.map(tab => <TerminalInstance key={tab.id} tabId={tab.id} />)}
</div>
</div>
)
}