forked from f/atom-term2
-
Notifications
You must be signed in to change notification settings - Fork 17
/
index.coffee
311 lines (273 loc) · 9.03 KB
/
index.coffee
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
path = require 'path'
TermView = require './lib/term-view'
ListView = require './lib/build/list-view'
Terminals = require './lib/terminal-model'
{Emitter} = require 'event-kit'
keypather = do require 'keypather'
{CompositeDisposable} = require 'event-kit'
capitalize = (str)-> str[0].toUpperCase() + str[1..].toLowerCase()
getColors = ->
{
normalBlack, normalRed, normalGreen, normalYellow
normalBlue, normalPurple, normalCyan, normalWhite
brightBlack, brightRed, brightGreen, brightYellow
brightBlue, brightPurple, brightCyan, brightWhite
background, foreground
} = (atom.config.getAll 'term3.colors')[0].value
[
normalBlack, normalRed, normalGreen, normalYellow
normalBlue, normalPurple, normalCyan, normalWhite
brightBlack, brightRed, brightGreen, brightYellow
brightBlue, brightPurple, brightCyan, brightWhite
background, foreground
].map (color) -> color.toHexString()
config =
autoRunCommand:
type: 'string'
default: ''
titleTemplate:
type: 'string'
default: "Terminal ({{ bashName }})"
fontFamily:
type: 'string'
default: ''
fontSize:
type: 'string'
default: ''
colors:
type: 'object'
properties:
normalBlack:
type: 'color'
default: '#2e3436'
normalRed:
type: 'color'
default: '#cc0000'
normalGreen:
type: 'color'
default: '#4e9a06'
normalYellow:
type: 'color'
default: '#c4a000'
normalBlue:
type: 'color'
default: '#3465a4'
normalPurple:
type: 'color'
default: '#75507b'
normalCyan:
type: 'color'
default: '#06989a'
normalWhite:
type: 'color'
default: '#d3d7cf'
brightBlack:
type: 'color'
default: '#555753'
brightRed:
type: 'color'
default: '#ef2929'
brightGreen:
type: 'color'
default: '#8ae234'
brightYellow:
type: 'color'
default: '#fce94f'
brightBlue:
type: 'color'
default: '#729fcf'
brightPurple:
type: 'color'
default: '#ad7fa8'
brightCyan:
type: 'color'
default: '#34e2e2'
brightWhite:
type: 'color'
default: '#eeeeec'
background:
type: 'color'
default: '#000000'
foreground:
type: 'color'
default: '#f0f0f0'
scrollback:
type: 'integer'
default: 1000
cursorBlink:
type: 'boolean'
default: true
shellOverride:
type: 'string'
default: ''
shellArguments:
type: 'string'
default: do ({SHELL, HOME}=process.env) ->
switch path.basename SHELL && SHELL.toLowerCase()
when 'bash' then "--init-file #{path.join HOME, '.bash_profile'}"
when 'zsh' then "-l"
else ''
openPanesInSameSplit:
type: 'boolean'
default: false
module.exports =
termViews: []
focusedTerminal: off
emitter: new Emitter()
config: config
disposables: null
activate: (@state) ->
@disposables = new CompositeDisposable()
unless process.env.LANG
console.warn "Term3: LANG environment variable is not set. Fancy characters (å, ñ, ó, etc`) may be corrupted. The only work-around is to quit Atom and run `atom` from your shell."
['up', 'right', 'down', 'left'].forEach (direction) =>
@disposables.add atom.commands.add "atom-workspace", "term3:open-split-#{direction}", @splitTerm.bind(this, direction)
@disposables.add atom.commands.add "atom-workspace", "term3:open", @newTerm.bind(this)
@disposables.add atom.commands.add "atom-workspace", "term3:pipe-path", @pipeTerm.bind(this, 'path')
@disposables.add atom.commands.add "atom-workspace", "term3:pipe-selection", @pipeTerm.bind(this, 'selection')
atom.packages.activatePackage('tree-view').then (treeViewPkg) =>
node = new ListView()
treeViewPkg.mainModule.treeView.find(".tree-view-scroller").prepend node
service_0_1_3: () ->
{
getTerminals: @getTerminals.bind(this),
onTerm: @onTerm.bind(this),
newTerm: @newTerm.bind(this),
}
getTerminals: ->
Terminals.map (t) ->
t.term
onTerm: (callback) ->
@emitter.on 'term', callback
attachSubscriptions: (termView, item, pane) ->
subscriptions = new CompositeDisposable
focusNextTick = (activeItem) ->
process.nextTick ->
termView.focus()
# HACK!
# so, term.js allows for a special _textarea because of iframe shenanigans,
# but, it is the constructor instead of the instance!!!1 - probably to avoid having to bind this as a premature
# optimization.
atomPane = activeItem.parentsUntil("atom-pane").parent()[0]
if termView.term
termView.term.constructor._textarea = atomPane
subscriptions.add pane.onDidActivate ->
activeItem = pane.getActiveItem()
if activeItem != item
return
@focusedTerminal = termView
termView.focus()
focusNextTick activeItem
subscriptions.add pane.onDidChangeActiveItem (activeItem) ->
if activeItem != termView
if termView.term
termView.term.constructor._textarea = null
return
focusNextTick activeItem
subscriptions.add termView.onExit () ->
Terminals.remove termView.id
subscriptions.add pane.onWillRemoveItem (itemRemoved, index) =>
if itemRemoved.item == item
item.destroy()
Terminals.remove termView.id
@disposables.remove subscriptions
subscriptions.dispose()
subscriptions
newTerm: (forkPTY=true, rows=30, cols=80, title='tty') ->
termView = @createTermView forkPTY, rows, cols, title
pane = atom.workspace.getActivePane()
item = pane.addItem termView
@disposables.add @attachSubscriptions(termView, item, pane)
pane.activateItem item
termView
createTermView: (forkPTY=true, rows=30, cols=80, title='tty') ->
opts =
runCommand : atom.config.get 'term3.autoRunCommand'
shellOverride : atom.config.get 'term3.shellOverride'
shellArguments: atom.config.get 'term3.shellArguments'
titleTemplate : atom.config.get 'term3.titleTemplate'
cursorBlink : atom.config.get 'term3.cursorBlink'
fontFamily : atom.config.get 'term3.fontFamily'
fontSize : atom.config.get 'term3.fontSize'
colors : getColors()
forkPTY : forkPTY
rows : rows
cols : cols
if opts.shellOverride
opts.shell = opts.shellOverride
else
opts.shell = process.env.SHELL or 'bash'
# opts.shellArguments or= ''
editorPath = keypather.get atom, 'workspace.getEditorViews[0].getEditor().getPath()'
opts.cwd = opts.cwd or atom.project.getPaths()[0] or editorPath or process.env.HOME
termView = new TermView opts
model = Terminals.add {
local: !!forkPTY,
term: termView,
title: title,
}
id = model.id
termView.id = id
termView.on 'remove', @handleRemoveTerm.bind this
termView.on 'click', =>
# get focus in the terminal
# avoid double click to get focus
termView.term.element.focus()
termView.term.focus()
@focusedTerminal = termView
termView.onDidChangeTitle () ->
if forkPTY
model.title = termView.getTitle()
else
model.title = title + '-' + termView.getTitle()
@termViews.push? termView
process.nextTick () => @emitter.emit 'term', termView
termView
splitTerm: (direction) ->
openPanesInSameSplit = atom.config.get 'term3.openPanesInSameSplit'
termView = @createTermView()
direction = capitalize direction
splitter = =>
pane = activePane["split#{direction}"] items: [termView]
activePane.termSplits[direction] = pane
@focusedTerminal = [pane, pane.items[0]]
@disposables.add @attachSubscriptions(termView, pane.items[0], pane)
activePane = atom.workspace.getActivePane()
activePane.termSplits or= {}
if openPanesInSameSplit
if activePane.termSplits[direction] and activePane.termSplits[direction].items.length > 0
pane = activePane.termSplits[direction]
item = pane.addItem termView
pane.activateItem item
@focusedTerminal = [pane, item]
@disposables.add @attachSubscriptions(termView, item, pane)
else
splitter()
else
splitter()
pipeTerm: (action) ->
editor = atom.workspace.getActiveTextEditor()
if !editor
return
stream = switch action
when 'path'
editor.getBuffer().file.path
when 'selection'
editor.getSelectedText()
if stream and @focusedTerminal
if Array.isArray @focusedTerminal
[pane, item] = @focusedTerminal
pane.activateItem item
else
item = @focusedTerminal
item.pty.write stream.trim()
item.term.focus()
handleRemoveTerm: (termView)->
@termViews.splice @termViews.indexOf(termView), 1
deactivate:->
@termViews.forEach (view) -> view.exit()
@termViews = []
@disposables.dispose
serialize:->
termViewsState = this.termViews.map (view)-> view.serialize()
{termViews: termViewsState}