forked from f/atom-term2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.coffee
211 lines (189 loc) · 6.28 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
path = require 'path'
TermView = require './lib/TermView'
capitalize = (str)-> str[0].toUpperCase() + str[1..].toLowerCase()
module.exports =
termViews: []
focusedTerminal: off
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.toLowerCase()
when 'bash' then "--init-file #{path.join HOME, '.bash_profile'}"
when 'zsh' then ""
else ''
openPanesInSameSplit:
type: 'boolean'
default: false
activate: (@state)->
['up', 'right', 'down', 'left'].forEach (direction)=>
atom.commands.add "atom-workspace", "term2:open-split-#{direction}", @splitTerm.bind(this, direction)
atom.commands.add "atom-workspace", "term2:open", @newTerm.bind(this)
atom.commands.add "atom-workspace", "term2:pipe-path", @pipeTerm.bind(this, 'path')
atom.commands.add "atom-workspace", "term2:pipe-selection", @pipeTerm.bind(this, 'selection')
getColors: ->
{
normalBlack, normalRed, normalGreen, normalYellow
normalBlue, normalPurple, normalCyan, normalWhite
brightBlack, brightRed, brightGreen, brightYellow
brightBlue, brightPurple, brightCyan, brightWhite
background, foreground
} = (atom.config.getAll 'term2.colors')[0].value
[
normalBlack, normalRed, normalGreen, normalYellow
normalBlue, normalPurple, normalCyan, normalWhite
brightBlack, brightRed, brightGreen, brightYellow
brightBlue, brightPurple, brightCyan, brightWhite
background, foreground
]
createTermView:->
opts =
runCommand : atom.config.get 'term2.autoRunCommand'
shellOverride : atom.config.get 'term2.shellOverride'
shellArguments: atom.config.get 'term2.shellArguments'
titleTemplate : atom.config.get 'term2.titleTemplate'
cursorBlink : atom.config.get 'term2.cursorBlink'
fontFamily : atom.config.get 'term2.fontFamily'
fontSize : atom.config.get 'term2.fontSize'
colors : @getColors()
termView = new TermView opts
termView.on 'remove', @handleRemoveTerm.bind this
@termViews.push? termView
termView
splitTerm: (direction)->
openPanesInSameSplit = atom.config.get 'term2.openPanesInSameSplit'
termView = @createTermView()
direction = capitalize direction
splitter = =>
pane = activePane["split#{direction}"] items: [termView]
activePane.termSplits[direction] = pane
@attachFocusEvents pane, pane.items[0], termView
@focusedTerminal = [pane, pane.items[0]]
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
@attachFocusEvents pane, item, termView
pane.activateItem item
@focusedTerminal = [pane, item]
else
splitter()
else
splitter()
newTerm: ->
termView = @createTermView()
pane = atom.workspace.getActivePane()
item = pane.addItem termView
@attachFocusEvents pane, item, termView
pane.activateItem item
pipeTerm: (action)->
editor = @getActiveEditor()
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.deactivate()
serialize:->
termViewsState = this.termViews.map (view)-> view.serialize()
{termViews: termViewsState}
attachFocusEvents: (pane, item, termView)->
pane.onDidActivate =>
if pane.getActiveItem() == item
@focusedTerminal = termView
termView.focus()
pane.onDidChangeActiveItem (activeItem)=>
if activeItem == item
@focusedTerminal = termView
termView.focus()