/
atom-xterm.js
524 lines (489 loc) · 18.7 KB
/
atom-xterm.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
/** @babel */
/** @module atom-xterm */
/*
* Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
* Copyright 2017-2018 Andres Mejia <amejia004@gmail.com>. All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this
* software and associated documentation files (the "Software"), to deal in the Software
* without restriction, including without limitation the rights to use, copy, modify,
* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
import { CompositeDisposable } from 'atom'
import * as config from './atom-xterm-config'
import { AtomXtermElement } from './atom-xterm-element'
import { AtomXtermModel, isAtomXtermModel } from './atom-xterm-model'
import { ATOM_XTERM_BASE_URI, AtomXtermProfilesSingleton } from './atom-xterm-profiles'
import { AtomXtermProfileMenuElement } from './atom-xterm-profile-menu-element'
import { AtomXtermProfileMenuModel } from './atom-xterm-profile-menu-model'
import { AtomXtermDeleteProfileElement } from './atom-xterm-delete-profile-element'
import { AtomXtermDeleteProfileModel } from './atom-xterm-delete-profile-model'
import { AtomXtermOverwriteProfileElement } from './atom-xterm-overwrite-profile-element'
import { AtomXtermOverwriteProfileModel } from './atom-xterm-overwrite-profile-model'
import { AtomXtermSaveProfileElement } from './atom-xterm-save-profile-element'
import { AtomXtermSaveProfileModel } from './atom-xterm-save-profile-model'
import { URL } from 'whatwg-url'
export default {
'config': {
'spawnPtySettings': {
'title': 'Shell Process Settings',
'description': 'Settings related to the process running the shell.',
'type': 'object',
'properties': {
'command': {
'title': 'Command',
'description': 'Command to run',
'type': 'string',
'default': config.getDefaultShellCommand()
},
'args': {
'title': 'Arguments',
'description': 'Arguments to pass to command, must be in a JSON array.',
'type': 'string',
'default': config.getDefaultArgs()
},
'name': {
'title': 'Terminal Type',
'description': 'The terminal type to use.',
'type': 'string',
'default': config.getDefaultTermType()
},
'cwd': {
'title': 'Working Directory',
'description': 'The working directory to use when launching command.',
'type': 'string',
'default': config.getDefaultCwd()
},
'env': {
'title': 'Environment',
'description': 'The environment to use when launching command, must be in a JSON object. If not set, defaults to the current environment.',
'type': 'string',
'default': config.getDefaultEnv()
},
'setEnv': {
'title': 'Environment Overrides',
'description': 'Environment variables to use in place of the atom process environment, must be in a JSON object.',
'type': 'string',
'default': config.getDefaultSetEnv()
},
'deleteEnv': {
'title': 'Environment Deletions',
'description': 'Environment variables to delete from original environment, must be in a JSON array.',
'type': 'string',
'default': config.getDefaultDeleteEnv()
},
'encoding': {
'title': 'Character Encoding',
'description': 'Character encoding to use in spawned terminal.',
'type': 'string',
'default': config.getDefaultEncoding()
}
}
},
'terminalSettings': {
'title': 'Terminal Emulator Settings',
'description': 'Settings for the terminal emulator.',
'type': 'object',
'properties': {
'fontSize': {
'title': 'Font Size',
'description': 'Font size used in terminal emulator.',
'type': 'integer',
'default': config.getDefaultFontSize(),
'minimum': config.getMinimumFontSize(),
'maximum': config.getMaximumFontSize()
},
'leaveOpenAfterExit': {
'title': 'Leave Open After Exit',
'description': 'Whether to leave terminal emulators open after their shell processes have exited.',
'type': 'boolean',
'default': config.getDefaultLeaveOpenAfterExit()
},
'allowRelaunchingTerminalsOnStartup': {
'title': 'Allow relaunching terminals on startup',
'description': 'Whether to allow relaunching terminals on startup.',
'type': 'boolean',
'default': config.getDefaultAllowRelaunchingTerminalsOnStartup()
},
'relaunchTerminalOnStartup': {
'title': 'Relaunch terminal on startup',
'description': 'Whether to relaunch terminal on startup.',
'type': 'boolean',
'default': config.getDefaultRelaunchTerminalOnStartup()
},
'title': {
'title': 'Terminal tab title',
'description': 'Title to use for terminal tabs.',
'type': 'string',
'default': config.getDefaultTitle()
},
'xtermOptions': {
'title': 'xterm.js Terminal Options',
'description': 'Options to apply to xterm.js Terminal objects.',
'type': 'string',
'default': config.getDefaultXtermOptions()
}
}
}
},
activate (state) {
// Load profiles configuration.
this.profilesSingleton = AtomXtermProfilesSingleton.instance
// Reset base profile in case this package was deactivated then
// reactivated.
this.profilesSingleton.resetBaseProfile()
// Disposables for this plugin.
this.disposables = new CompositeDisposable()
// Set holding all terminals available at any moment.
this.terminals_set = new Set()
// Monitor for changes to all config values.
let configKeys = [
'atom-xterm.spawnPtySettings.command',
'atom-xterm.spawnPtySettings.args',
'atom-xterm.spawnPtySettings.name',
'atom-xterm.spawnPtySettings.cwd',
'atom-xterm.spawnPtySettings.env',
'atom-xterm.spawnPtySettings.setEnv',
'atom-xterm.spawnPtySettings.deleteEnv',
'atom-xterm.spawnPtySettings.encoding',
'atom-xterm.terminalSettings.fontSize',
'atom-xterm.terminalSettings.leaveOpenAfterExit',
'atom-xterm.terminalSettings.allowRelaunchingTerminalsOnStartup',
'atom-xterm.terminalSettings.relaunchTerminalOnStartup',
'atom-xterm.terminalSettings.title',
'atom-xterm.terminalSettings.xtermOptions'
]
for (let key of configKeys) {
this.disposables.add(atom.config.onDidChange(key, ({newValue, oldValue}) => {
this.profilesSingleton.resetBaseProfile()
}))
}
// Register view provider for terminal emulator item.
this.disposables.add(atom.views.addViewProvider(AtomXtermModel, (atomXtermModel) => {
let atomXtermElement = new AtomXtermElement()
atomXtermElement.initialize(atomXtermModel)
return atomXtermElement
}))
// Register view provider for terminal emulator profile menu item.
this.disposables.add(atom.views.addViewProvider(AtomXtermProfileMenuModel, (atomXtermProfileMenuModel) => {
let atomXtermProfileMenuElement = new AtomXtermProfileMenuElement()
atomXtermProfileMenuElement.initialize(atomXtermProfileMenuModel)
return atomXtermProfileMenuElement
}))
// Register view profile for modal items.
this.disposables.add(atom.views.addViewProvider(AtomXtermDeleteProfileModel, (atomXtermDeleteProfileModel) => {
let atomXtermDeleteProfileElement = new AtomXtermDeleteProfileElement()
atomXtermDeleteProfileElement.initialize(atomXtermDeleteProfileModel)
return atomXtermDeleteProfileElement
}))
this.disposables.add(atom.views.addViewProvider(AtomXtermOverwriteProfileModel, (atomXtermOverwriteProfileModel) => {
let atomXtermOverwriteProfileElement = new AtomXtermOverwriteProfileElement()
atomXtermOverwriteProfileElement.initialize(atomXtermOverwriteProfileModel)
return atomXtermOverwriteProfileElement
}))
this.disposables.add(atom.views.addViewProvider(AtomXtermSaveProfileModel, (atomXtermSaveProfileModel) => {
let atomXtermSaveProfileElement = new AtomXtermSaveProfileElement()
atomXtermSaveProfileElement.initialize(atomXtermSaveProfileModel)
return atomXtermSaveProfileElement
}))
// Add opener for terminal emulator item.
this.disposables.add(atom.workspace.addOpener((uri) => {
if (uri.startsWith(ATOM_XTERM_BASE_URI)) {
let item = new AtomXtermModel({
uri: uri,
terminals_set: this.terminals_set
})
return item
}
}))
// Set callback to run on current and future panes.
this.disposables.add(atom.workspace.observePanes((pane) => {
// In callback, set another callback to run on current and future items.
this.disposables.add(pane.observeItems((item) => {
// In callback, set current pane for terminal items.
if (isAtomXtermModel(item)) {
item.setNewPane(pane)
}
}))
}))
// Add callbacks to run for current and future active items on active panes.
this.disposables.add(atom.workspace.observeActivePaneItem((item) => {
// In callback, focus specifically on terminal when item is terminal item.
if (isAtomXtermModel(item)) {
item.focusOnTerminal()
}
}))
// Add commands.
this.disposables.add(atom.commands.add('atom-workspace', {
'atom-xterm:open': () => {
this.openInCenterOrDock(atom.workspace)
},
'atom-xterm:open-split-up': () => this.open(
this.profilesSingleton.generateNewUri(),
{'split': 'up'}
),
'atom-xterm:open-split-down': () => this.open(
this.profilesSingleton.generateNewUri(),
{'split': 'down'}
),
'atom-xterm:open-split-left': () => this.open(
this.profilesSingleton.generateNewUri(),
{'split': 'left'}
),
'atom-xterm:open-split-right': () => this.open(
this.profilesSingleton.generateNewUri(),
{'split': 'right'}
),
'atom-xterm:open-split-bottom-dock': () => {
this.openInCenterOrDock(atom.workspace.getBottomDock())
},
'atom-xterm:open-split-left-dock': () => {
this.openInCenterOrDock(atom.workspace.getLeftDock())
},
'atom-xterm:open-split-right-dock': () => {
this.openInCenterOrDock(atom.workspace.getRightDock())
},
'atom-xterm:toggle-profile-menu': () => this.toggleProfileMenu(),
'atom-xterm:reorganize': () => this.reorganize('current'),
'atom-xterm:reorganize-top': () => this.reorganize('top'),
'atom-xterm:reorganize-bottom': () => this.reorganize('bottom'),
'atom-xterm:reorganize-left': () => this.reorganize('left'),
'atom-xterm:reorganize-right': () => this.reorganize('right'),
'atom-xterm:reorganize-bottom-dock': () => this.reorganize('bottom-dock'),
'atom-xterm:reorganize-left-dock': () => this.reorganize('left-dock'),
'atom-xterm:reorganize-right-dock': () => this.reorganize('right-dock'),
'atom-xterm:close-all': () => this.exitAllTerminals(),
'atom-xterm:need-this-to-activate-package-for-tests-do-not-remove': () => {
/*
* This is just here for activating the package in tests.
* From the main program, the 'core:loaded-shell-environment'
* activation hook is used to activate the package.
*/
}
}))
this.disposables.add(atom.commands.add('atom-xterm', {
'atom-xterm:close': () => this.close(),
'atom-xterm:restart': () => this.restart(),
'atom-xterm:copy': () => this.copy(),
'atom-xterm:paste': () => this.paste(),
'atom-xterm:open-link': () => this.openLink(),
'atom-xterm:copy-link': () => this.copyLink()
}))
},
deactivate () {
this.exitAllTerminals()
this.disposables.dispose()
},
deserializeAtomXtermModel (serializedModel, atomEnvironment) {
let pack = atom.packages.enablePackage('atom-xterm')
pack.preload()
pack.activateNow()
let allowRelaunchingTerminalsOnStartup = atom.config.get('atom-xterm.terminalSettings.allowRelaunchingTerminalsOnStartup')
if (!allowRelaunchingTerminalsOnStartup) {
return
}
let url = new URL(serializedModel.uri)
let relaunchTerminalOnStartup = url.searchParams.get('relaunchTerminalOnStartup')
if (relaunchTerminalOnStartup === 'false') {
return
}
return new AtomXtermModel({
uri: url.href,
terminals_set: this.terminals_set
})
},
openInCenterOrDock (centerOrDock) {
let options = {}
let pane = centerOrDock.getActivePane()
if (pane) {
options.pane = pane
}
this.open(
this.profilesSingleton.generateNewUri(),
options
)
},
refitAllTerminals () {
let currentActivePane = atom.workspace.getActivePane()
let currentActiveItem = currentActivePane.getActiveItem()
for (let terminal of this.terminals_set) {
// To refit, simply bring the terminal in focus in order for the
// resize event to refit the terminal.
let paneActiveItem = terminal.pane.getActiveItem()
terminal.pane.getElement().focus()
terminal.pane.setActiveItem(terminal)
terminal.pane.setActiveItem(paneActiveItem)
}
currentActivePane.getElement().focus()
currentActivePane.setActiveItem(currentActiveItem)
},
exitAllTerminals () {
for (let terminal of this.terminals_set) {
terminal.exit()
}
},
async open (uri, options = {}) {
let url = new URL(uri)
let relaunchTerminalOnStartup = url.searchParams.get('relaunchTerminalOnStartup')
if (relaunchTerminalOnStartup === null) {
relaunchTerminalOnStartup = this.profilesSingleton.getBaseProfile().relaunchTerminalOnStartup
if (!relaunchTerminalOnStartup) {
url.searchParams.set('relaunchTerminalOnStartup', false)
}
}
return atom.workspace.open(url.href, options)
},
/**
* Service function which is a wrapper around 'atom.workspace.open()'. The
* only difference with this function from 'atom.workspace.open()' is that it
* accepts a profile Object as the first argument.
*
* @async
* @function
* @param {Object} profile Profile data to use when opening terminal.
* @param {Object} options Options to pass to call to 'atom.workspace.open()'.
* @return {AtomXtermModel} Instance of AtomXtermModel.
*/
async openTerminal (profile, options = {}) {
return this.open(
AtomXtermProfilesSingleton.instance.generateNewUrlFromProfileData(profile),
options
)
},
/**
* Function providing service functions offered by 'atom-xterm' package.
*
* @function
* @returns {Object} Object holding service functions.
*/
provideAtomXtermService () {
return {
openTerminal: async (...args) => {
return this.openTerminal(...args)
}
}
},
performOperationOnItem (operation) {
let item = atom.workspace.getActivePaneItem()
if (isAtomXtermModel(item)) {
switch (operation) {
case 'close':
item.exit()
break
case 'restart':
item.restartPtyProcess()
break
case 'copy':
atom.clipboard.write(item.copyFromTerminal())
break
case 'paste':
item.pasteToTerminal(atom.clipboard.read())
break
case 'open-link':
item.openHoveredLink()
break
case 'copy-link':
let link = item.getHoveredLink()
if (link) {
atom.clipboard.write(link)
}
break
default:
throw new Error('Unknown operation: ' + operation)
}
}
},
close () {
this.performOperationOnItem('close')
},
restart () {
this.performOperationOnItem('restart')
},
copy () {
this.performOperationOnItem('copy')
},
paste () {
this.performOperationOnItem('paste')
},
openLink () {
this.performOperationOnItem('open-link')
},
copyLink () {
this.performOperationOnItem('copy-link')
},
toggleProfileMenu () {
let item = atom.workspace.getActivePaneItem()
if (isAtomXtermModel(item)) {
item.toggleProfileMenu()
}
},
reorganize (orientation) {
if (this.terminals_set.size === 0) {
return
}
let activePane = atom.workspace.getActivePane()
let activeItem = activePane.getActiveItem()
let newPane
switch (orientation) {
case 'current':
newPane = activePane
break
case 'top':
newPane = activePane.findTopmostSibling().splitUp()
break
case 'bottom':
newPane = activePane.findBottommostSibling().splitDown()
break
case 'left':
newPane = activePane.findLeftmostSibling().splitLeft()
break
case 'right':
newPane = activePane.findRightmostSibling().splitRight()
break
case 'bottom-dock':
newPane = atom.workspace.getBottomDock().getActivePane()
break
case 'left-dock':
newPane = atom.workspace.getLeftDock().getActivePane()
break
case 'right-dock':
newPane = atom.workspace.getRightDock().getActivePane()
break
default:
throw new Error('Unknown orientation: ' + orientation)
}
for (let item of this.terminals_set) {
item.pane.moveItemToPane(item, newPane, -1)
}
if (isAtomXtermModel(activeItem)) {
if (atom.workspace.getPanes().length > 1) {
// When reorganizing still leaves more than one pane in the
// workspace, another pane that doesn't include the newly
// reorganized terminal tabs needs to be focused in order for
// the terminal views to get properly resized in the new pane.
// All this is yet another quirk.
for (let pane of atom.workspace.getPanes()) {
if (pane !== activeItem.pane) {
pane.getElement().focus()
break
}
}
}
activeItem.pane.getElement().focus()
activeItem.pane.setActiveItem(activeItem)
} else if (activeItem instanceof HTMLElement) {
activeItem.focus()
} else if (typeof activeItem.getElement === 'function') {
activeItem = activeItem.getElement()
activeItem.focus()
}
}
}