/
terminal.js
268 lines (265 loc) · 8.09 KB
/
terminal.js
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import {spawn} from 'node-pty'
import {Terminal} from 'xterm'
import {FitAddon} from 'xterm-addon-fit'
import {SearchAddon} from 'xterm-addon-search'
import {WebLinksAddon} from 'xterm-addon-web-links'
import {LigaturesAddon} from 'xterm-addon-ligatures'
import {remote, shell} from 'electron'
import {getCwd, getWindowsProcessInfo} from '@/utils/terminal'
import {normalizeTheme} from '@/utils/theme'
import {unreactive} from '@/utils/object'
import {exec} from '@/utils/electron'
import {debounce} from 'lodash'
const variables = {
LANG: remote.app.getLocale().replace('-', '_') + '.UTF-8',
TERM_PROGRAM: remote.app.name,
TERM_PROGRAM_VERSION: remote.app.getVersion(),
}
const isPackaged = remote.app.isPackaged
export default {
namespaced: true,
state: {
tabs: [],
shells: [],
active: -1,
observer: null,
},
getters: {
current(state) {
const active = state.active
if (active === -1) return null
const tabs = state.tabs
const tab = tabs[active]
return tab
},
shell(state, getters, rootState) {
const settings = rootState.settings.settings
return settings['terminal.shell.path'] || (
process.platform === 'win32' ? process.env.COMSPEC : process.env.SHELL
)
},
},
mutations: {
updateTab(state, {id, ...props}) {
let tab = state.tabs.find(item => item.id === id)
if (tab) Object.assign(tab, props)
},
appendTab(state, tab) {
state.tabs.push(tab)
},
removeTab(state, index) {
state.tabs.splice(index, 1)
},
setShells(state, value) {
state.shells = value
},
setActive(state, value) {
state.active = value
},
setObserver(state, value) {
state.observer = value
},
},
actions: {
async load({commit}) {
if (process.platform === 'win32') return
const {stdout} = await exec('grep "^/" /etc/shells')
commit('setShells', stdout.trim().split('\n'))
},
spawn({state, getters, commit, dispatch, rootState}, payload) {
payload = payload || {}
const settings = rootState.settings.settings
const env = {
...process.env,
...settings['terminal.shell.env'],
...variables,
}
// Fix NVM `npm_config_prefix` error in development environment
if (!isPackaged && env.npm_config_prefix) delete env.npm_config_prefix
const shell = payload.shell || getters.shell
// Initialize node-pty process
const cwd = payload.cwd || env.HOME
const options = {
name: 'xterm-256color',
cwd,
env,
}
let args = settings['terminal.shell.args']
if (process.platform === 'win32') {
args = settings['terminal.shell.args.windows']
} else {
options.encoding = 'utf8'
}
const pty = spawn(shell, args, options)
const id = pty.pid
// Initialize xterm.js and attach it to the DOM
const xterm = new Terminal({
fontSize: settings['terminal.style.fontSize'],
fontFamily: settings['terminal.style.fontFamily'],
allowTransparency: true,
theme: normalizeTheme(rootState.theme.theme),
})
xterm.$ = {}
// Setup communication between xterm.js and node-pty
xterm.onData(data => {
pty.write(data)
})
pty.onData(data => {
xterm.write(data)
// TODO: performance review
// pty.process on Windows will be always equivalent to pty.name
if (process.platform !== 'win32') {
commit('updateTab', {id, process: pty.process})
}
})
pty.onExit(() => {
if (xterm.element) {
state.observer.unobserve(xterm.element)
}
xterm.dispose()
dispatch('remove', id)
})
xterm.onResize(({cols, rows}) => {
pty.resize(cols, rows)
})
xterm.onTitleChange(title => {
const patch = {id, title}
if (process.platform === 'win32') {
const info = getWindowsProcessInfo(getters.shell, title)
if (info) Object.assign(patch, info)
}
commit('updateTab', patch)
})
if (settings['terminal.tab.liveCwd']) {
const updateCwd = debounce(async () => {
const cwd = await getCwd(id)
if (cwd) commit('updateTab', {id, cwd})
}, 250)
xterm.onKey(({key, domEvent}) => {
if (domEvent.keyCode === 13) updateCwd()
})
}
// Create new tab for current terminal
// TODO: put this before pty and xterm created
const tab = {
id,
cwd,
shell,
title: '',
process: pty.process,
pty: unreactive(pty),
xterm: unreactive(xterm),
}
if (payload.launcher) tab.launcher = payload.launcher
commit('appendTab', tab)
commit('setActive', state.tabs.length - 1)
},
mount({state, commit, dispatch, rootState}, {tab, element}) {
let observer = state.observer
if (!observer) {
observer = new ResizeObserver(debounce(() => {
dispatch('resize')
}, 250))
commit('setObserver', observer)
}
const settings = rootState.settings.settings
const xterm = tab.xterm
xterm.open(element)
xterm.$.fit = new FitAddon()
xterm.loadAddon(xterm.$.fit)
xterm.$.search = new SearchAddon()
xterm.loadAddon(xterm.$.search)
xterm.loadAddon(new WebLinksAddon((event, uri) => {
if (event.altKey) shell.openExternal(uri)
}))
if (settings['terminal.style.fontLigatures']) {
xterm.$.ligatures = new LigaturesAddon()
xterm.loadAddon(xterm.$.ligatures)
}
observer.observe(element)
xterm.$.fit.fit()
xterm.focus()
},
interact({state, commit}, tab) {
const tabs = state.tabs
const index = tabs.findIndex(item => item.id === tab.id)
if (index !== -1) {
commit('setActive', index)
} else {
commit('appendTab', tab)
commit('setActive', state.tabs.length - 1)
}
},
resize({getters}) {
const current = getters.current
if (current && current.xterm && current.xterm.element) {
current.xterm.$.fit.fit()
}
},
input(store, {tab, data}) {
tab.pty.write(data)
},
activite({state, commit}, tab) {
const tabs = state.tabs
const index = tabs.indexOf(tab)
if (index !== -1) {
commit('setActive', index)
}
},
close({dispatch}, tab) {
if (tab.internal) {
dispatch('remove', tab.id)
} else {
tab.pty.kill()
}
},
remove({state, commit, dispatch, rootState}, id) {
const index = state.tabs.findIndex(tab => tab.id === id)
if (index === -1) return
commit('removeTab', index)
const length = state.tabs.length
if (!length) {
if (rootState.proxy.server) {
dispatch('spawn')
} else {
remote.getCurrentWindow().close()
}
} else {
const active = state.active > index ?
state.active - 1 : Math.min(index, length - 1)
commit('setActive', active)
}
},
find({getters}, {keyword, options, back}) {
const current = getters.current
if (back) {
current.xterm.$.search.findPrevious(keyword, options)
} else {
current.xterm.$.search.findNext(keyword, options)
}
},
clear({getters}) {
const current = getters.current
current.xterm.clear()
},
refresh({state, rootState}) {
const tabs = state.tabs
const settings = rootState.settings.settings
// TODO: performance review
for (const tab of tabs) {
if (tab.internal) continue
const xterm = tab.xterm
xterm.setOption('fontSize', settings['terminal.style.fontSize'])
xterm.setOption('fontFamily', settings['terminal.style.fontFamily'])
xterm.setOption('theme', normalizeTheme(rootState.theme.theme))
// TODO: unload when settings disabled
if (settings['terminal.style.fontLigatures']) {
if (!xterm.$.ligatures) {
xterm.$.ligatures = new LigaturesAddon()
xterm.loadAddon(xterm.$.ligatures)
}
}
}
},
},
}