forked from kiddkai/atom-node-debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConsolePane.coffee
More file actions
158 lines (138 loc) · 4.37 KB
/
Copy pathConsolePane.coffee
File metadata and controls
158 lines (138 loc) · 4.37 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
{TextEditorView} = require 'atom-space-pen-views'
Event = require 'geval/event'
{merge, split} = require 'event-stream'
stream = require 'stream'
hg = require 'mercury'
{h} = hg
{CommandHistory} = require './consolepane-utils'
exports.create = (_debugger) ->
jsGrammar = atom.grammars.grammarForScopeName('source.js')
tokenizeLine = (text) ->
return h('div.line', {}, text) unless jsGrammar
{tokens} = jsGrammar.tokenizeLine(text)
h('div.line', {}, [
h('span.test.shell-session', {}, tokens.map((token) ->
h('span', {
className: token.scopes.join(' ').split('.').join(' ')
}, [token.value])
))
])
class ConsoleInput
constructor: (@debugger)->
@type = "Widget"
@_changer = Event()
@onEvalOrResult = @_changer.listen
init: ->
self = this
@editorView = new TextEditorView(mini: true)
@editor = @editorView.getModel()
@historyTracker = new CommandHistory(@editor)
@editorView.on 'keyup', (ev) ->
{keyCode} = ev
switch keyCode
when 13
text = self.editor.getText()
self._changer.broadcast(text)
self.editor.setText('')
self.historyTracker.saveIfNew(text)
self
.debugger
.eval(text)
.then (result) ->
self._changer.broadcast(result.text)
.catch (e) ->
if e.message?
self._changer.broadcast(e.message)
else
self._changer.broadcast(e)
when 38
self.historyTracker.moveUp()
when 40
self.historyTracker.moveDown()
return @editorView.get(0)
update: (prev, el) ->
return el
input = new ConsoleInput(_debugger)
ConsolePane = () =>
state = hg.state({
lines: hg.array([])
channels: {
clear: (state) -> state.lines.set([])
}
})
input.onEvalOrResult (text) ->
state.lines.push(text)
newWriter = () ->
new stream.Writable({
write: (chunk, encoding, next) ->
state.lines.push(chunk.toString())
next()
})
self = this
self.unsubscribeProcessCreated = _debugger.processManager.subscribe 'processCreated', ->
{stdout, stderr} = _debugger.processManager.process
self.unsubscribeLogData = stdout.subscribe 'data', (d) -> console.log(d.toString())
self.unsubscribeLogError = stderr.subscribe 'data', (d) -> console.log(d.toString())
stdout
.pipe(split())
.pipe(newWriter())
stderr
.pipe(split())
.pipe(newWriter())
self.unsubscribeReconnect = _debugger.subscribe 'reconnect', ({count,host,port,timeout}) ->
message = "Connection attempt #{count} to node process on #{host}:#{port} failed. Will try again in #{timeout}."
state.lines.push(message)
return state
ConsolePane.render = (state) ->
h('div.inset-panel', {
style: {
flex: '1 1 0'
display: 'flex'
flexDirection: 'column'
}
}, [
h('div.debugger-panel-heading', {
style: {
display: 'flex'
flexDirection: 'row'
'align-items': 'center'
'justify-content': 'center'
flex: '0 0 auto'
}
}
[
h('div', {}, 'stdout/stderr')
h('div', {
style: { 'margin-left': 'auto' },
className: 'icon-trashcan btn btn-primary'
'ev-click': hg.send state.channels.clear
'title': 'clear console'
})
])
h('div.panel-body.padded.native-key-bindings', {
attributes: {
tabindex: '-1'
}
style: {
flex: '1'
overflow: 'auto'
"font-family": "Menlo, Consolas, 'DejaVu Sans Mono', monospace"
}
}, state.lines.map(tokenizeLine))
h('div.debugger-editor', style: {
height: '33px'
flexBasis: '33px'
}, [
input
])
])
ConsolePane.cleanup = () =>
@unsubscribeProcessCreated() if @unsubscribeProcessCreated
@unsubscribeLogData() if @unsubscribeLogData
@unsubscribeLogError() if @unsubscribeLogError
@unsubscribeReconnect() if @unsubscribeReconnect
@unsubscribeProcessCreated = null
@unsubscribeLogData = null
@unsubscribeLogError = null
@unsubscribeReconnect = null
return ConsolePane