-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtui.go
389 lines (326 loc) · 10.6 KB
/
tui.go
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
package main
import (
"fmt"
"math"
"strings"
"time"
"github.com/ilmari-h/dlvtui/nav"
"github.com/gdamore/tcell/v2"
"github.com/go-delve/delve/service/api"
"github.com/go-delve/delve/service/rpc2"
"github.com/rivo/tview"
log "github.com/sirupsen/logrus"
)
type Mode int
const (
Normal Mode = iota
Cmd
)
type KeyPress struct {
mode Mode
event *tcell.EventKey
}
type DebuggerStep struct {
locals []api.Variable
args []api.Variable
globals []api.Variable
}
type DebuggerMove struct {
DbgState *api.DebuggerState
Stack []api.Stackframe
}
type View struct {
nwBlocking bool
commandChan chan string
keyHandler KeyHandler
currentMode Mode
fileChan chan *nav.File
pageView *PageView
masterView *tview.Flex
currentPage int
cmdLine *tview.InputField
indicatorText *tview.TextView
cmdHandler *CommandHandler
notificationLine *tview.TextView
dbgMoveChan chan *DebuggerMove
breakpointChan chan *nav.UiBreakpoint
navState *nav.Nav
goroutineChan chan []*api.Goroutine
}
func parseCommand(input string) LineCommand {
if len(input) == 0 {
return nil
}
args := strings.Fields(input)
command := args[0]
cargs := args[1:]
return StringToLineCommand(command, cargs)
}
type KeyHandler struct {
app *tview.Application
view *View
prevKey *tcell.EventKey
}
func (keyHandler *KeyHandler) handleKeyEvent(kp KeyPress) *tcell.EventKey {
view := keyHandler.view
rune := kp.event.Rune()
key := kp.event.Key()
keyHandler.prevKey = kp.event
// Block events if there's a notification prompt.
if view.notificationLine.GetText(true) != "" {
if key == tcell.KeyEnter {
view.clearNotification()
}
return nil
}
if rune == ':' {
view.toCmdMode()
return nil
}
if key == tcell.KeyEscape { // This is only relevant when typing commands
view.toNormalMode()
return nil
}
// Parse and run command from line input
if key == tcell.KeyEnter && view.cmdLine.HasFocus() {
linetext := view.cmdLine.GetText()
view.toNormalMode()
command := parseCommand(linetext)
if command != nil {
view.cmdHandler.RunCommand(command)
}
return nil
}
if view.currentMode != Cmd {
// Delegate to page view, which either changes page or delegates to current page.
return view.pageView.HandleKeyEvent(kp.event)
}
return kp.event
}
func (view *View) SetBlocking(blocking bool) {
view.nwBlocking = blocking
if blocking {
view.indicatorText.SetText(fmt.Sprintf("%s ", gConfig.Icons.IndRunning))
} else {
view.indicatorText.SetText(fmt.Sprintf("%s ", gConfig.Icons.IndStopped))
}
}
/**
* Open a file.
* If file has been opened previously, resume on that line. Otherwise open at
* the first line.
*/
func (view *View) OpenFile(file *nav.File, atLine int) {
view.navState.ChangeCurrentFile(file)
view.navState.SetLine(atLine)
view.pageView.LoadFile(file, atLine)
// Render current stack frame if one is selected.
if view.navState.CurrentStackFrame != nil {
view.pageView.RenderStack(
view.navState.CurrentStack,
view.navState.CurrentStackFrame,
view.navState.DbgState.CurrentThread.ReturnValues)
}
}
/**
* Listen to debugger state related messages from channels.
* These usually result in re-rendering of some ui elements.
*/
func (view *View) uiEventLoop() {
for {
select {
case dbgMove := <-view.dbgMoveChan:
view.onDebuggerMove(dbgMove)
case newFile := <-view.fileChan:
view.onNewFile(newFile)
case activeGoroutines := <-view.goroutineChan:
view.onNewGoroutines(activeGoroutines)
case newBp := <-view.breakpointChan:
view.onNewBreakpoint(newBp)
}
}
}
// Render that's done before a continue command, essentially just refresh current pages.
func (view *View) renderPendingContinue() {
view.navState.CurrentDebuggerPos = nav.DebuggerPos{File: "", Line: -1}
view.pageView.RenderBreakpoints(view.navState.GetAllBreakpoints())
view.pageView.RefreshCodePage()
}
/**
* Render a debugger move.
* A debugger move is any operation where the current line or file changes, and
* the current stack frame may also have new variables or function calls.
*/
func (view *View) onDebuggerMove(dbgMove *DebuggerMove) {
newState := dbgMove.DbgState
line := newState.CurrentThread.Line
file := newState.CurrentThread.File
view.navState.DbgState = newState
view.navState.CurrentDebuggerPos = nav.DebuggerPos{File: file, Line: line}
// Navigate to file and update call stack.
log.Printf("Debugger move inside file %s on line %d.", file, line-1)
view.OpenFile(view.navState.FileCache[file], line-1)
if len(dbgMove.Stack) > 0 {
view.navState.CurrentStack = dbgMove.Stack
view.navState.CurrentStackFrame = &dbgMove.Stack[0]
}
// If hit breakpoint.
if newState.CurrentThread.BreakpointInfo != nil {
log.Printf("Hit breakpoint in %s on line %d.", file, line)
// Update breakpoint that was hit
view.navState.Breakpoints[file][line] = &nav.UiBreakpoint{false, newState.CurrentThread.Breakpoint}
view.pageView.RenderBreakpointHit(dbgMove.DbgState.CurrentThread.BreakpointInfo)
}
// Update pages.
view.pageView.RenderBreakpoints(view.navState.GetAllBreakpoints())
view.pageView.RenderStack(
view.navState.CurrentStack,
view.navState.CurrentStackFrame,
view.navState.DbgState.CurrentThread.ReturnValues)
view.pageView.RenderJumpToLine(line - 1)
}
func (view *View) onNewFile(newFile *nav.File) {
view.OpenFile(
newFile,
view.navState.LineInFile(newFile.Path),
)
}
func (view *View) onNewBreakpoint(newBp *nav.UiBreakpoint) {
log.Printf("Got breakpoint in %s on line %d!", newBp.File, newBp.Line)
// ID -1 signifies deleted breakpoint.
if newBp.ID == -1 {
delete(view.navState.Breakpoints[newBp.File], newBp.Line)
view.pageView.RefreshCodePage()
return
}
if len(view.navState.Breakpoints[newBp.File]) == 0 {
view.navState.Breakpoints[newBp.File] = make(map[int]*nav.UiBreakpoint)
}
view.navState.Breakpoints[newBp.File][newBp.Line] = newBp
view.pageView.RenderBreakpoints(view.navState.GetAllBreakpoints())
view.pageView.RefreshCodePage()
}
func (view *View) onNewGoroutines(activeGoroutines []*api.Goroutine) {
view.navState.Goroutines = activeGoroutines
view.pageView.goroutinePage.RenderGoroutines(
activeGoroutines,
view.navState.DbgState.CurrentThread.GoroutineID,
)
}
func (view *View) toNormalMode() {
view.cmdLine.SetAutocompleteFunc(func(currentText string) (entries []string) {
return []string{}
})
view.cmdLine.SetLabel("")
view.cmdLine.SetText("")
view.keyHandler.app.SetFocus(view.masterView)
view.currentMode = Normal
}
func (view *View) toCmdMode() {
view.cmdLine.SetAutocompleteFunc(view.cmdHandler.GetSuggestions)
view.cmdLine.SetLabel(":")
view.keyHandler.app.SetFocus(view.cmdLine)
view.currentMode = Cmd
}
func (view *View) clearNotification() {
view.notificationLine.SetText("")
view.masterView.ResizeItem(view.notificationLine, 0, 0)
view.masterView.ResizeItem(view.pageView.GetWidget(), 0, 1)
go func() {
time.Sleep(time.Second / 10)
view.pageView.RefreshCodePage()
}()
}
func (view *View) notifyProgramEnded(exitCode int) {
msg := fmt.Sprintf("Program has finished with exit status %d.", exitCode)
log.Print(msg)
view.showNotification(msg, false)
if exitCode == 0 {
view.indicatorText.SetText(fmt.Sprintf("%s ", gConfig.Icons.IndExitSuccess))
} else {
view.indicatorText.SetText(fmt.Sprintf("%s %d ", gConfig.Icons.IndExitError, exitCode))
}
}
func (view *View) showNotification(msg string, error bool) {
msgLen := len(msg)
msg += fmt.Sprintf("\n[%s::b]Press Enter to continue", iToColorS(gConfig.Colors.NotifPromptFg))
if error {
msgLen += 7
view.notificationLine.SetText(fmt.Sprintf("[%s::b]Error[%s:-:-]: %s",
iToColorS(gConfig.Colors.NotifErrorFg),
iToColorS(gConfig.Colors.NotifMsgFg),
msg,
))
} else {
view.notificationLine.SetText(fmt.Sprintf("[%s]%s", iToColorS(gConfig.Colors.NotifMsgFg), msg))
}
_, _, boxWidth, _ := view.notificationLine.GetRect()
lines := int(math.Ceil(float64(msgLen) / float64(boxWidth)))
_, _, _, linesText := view.pageView.pagesView.GetInnerRect()
view.masterView.ResizeItem(view.notificationLine, lines+1, 1)
view.masterView.ResizeItem(view.pageView.GetWidget(), linesText-lines, 1)
view.pageView.ResizeCodePage(linesText - lines - 1)
}
func CreateTui(app *tview.Application, navState *nav.Nav, rpcClient *rpc2.RPCClient) View {
var view = View{
nwBlocking: false,
commandChan: make(chan string, 1024),
fileChan: make(chan *nav.File, 1024),
dbgMoveChan: make(chan *DebuggerMove, 1024),
goroutineChan: make(chan []*api.Goroutine, 1024),
breakpointChan: make(chan *nav.UiBreakpoint, 1024),
navState: navState,
currentMode: Normal,
pageView: nil,
}
view.cmdHandler = NewCommandHandler(&view, app, rpcClient)
view.pageView = NewPageView(view.cmdHandler, navState, app)
view.keyHandler = KeyHandler{app: app, view: &view}
flex := tview.NewFlex().SetDirection(tview.FlexRow)
flex.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
return view.keyHandler.handleKeyEvent(KeyPress{event: event, mode: view.currentMode})
})
commandLine := tview.NewInputField().
SetLabel("").
SetFieldWidth(0).
SetDoneFunc(func(key tcell.Key) {
event := tcell.NewEventKey(key, 0, tcell.ModNone)
view.keyHandler.handleKeyEvent(KeyPress{event: event, mode: Cmd})
})
notificationLine := tview.NewTextView().
SetDynamicColors(true).
SetText("")
notificationLine.SetBackgroundColor(tcell.ColorDefault)
notificationLine.SetWordWrap(true)
commandLine.SetBackgroundColor(tcell.ColorDefault)
commandLine.SetFieldBackgroundColor(tcell.ColorDefault)
suggestionStyle := tcell.StyleDefault.
Foreground(iToColorTcell(gConfig.Colors.MenuFg)).
Background(iToColorTcell(gConfig.Colors.MenuBg))
suggestionStyleSelected := tcell.StyleDefault.
Foreground(iToColorTcell(gConfig.Colors.MenuSelectedFg)).
Background(iToColorTcell(gConfig.Colors.MenuSelectedBg)).
Attributes(tcell.AttrBold)
commandLine.SetAutocompleteStyles(iToColorTcell(gConfig.Colors.MenuBg), suggestionStyle, suggestionStyleSelected)
indicatorText := tview.NewTextView()
indicatorText.SetBackgroundColor(tcell.ColorDefault)
indicatorText.SetTextAlign(tview.AlignRight)
indicatorText.SetText(fmt.Sprintf("%s ", gConfig.Icons.IndStopped))
bottomRow := tview.NewFlex().
AddItem(commandLine, 0, 1, false).
AddItem(indicatorText, 5, 1, false)
flex.AddItem(view.pageView.GetWidget(), 0, 1, false)
flex.AddItem(notificationLine, 0, 0, false)
flex.AddItem(bottomRow, 1, 1, false)
app.SetRoot(flex, true).SetFocus(flex)
view.masterView = flex
view.cmdLine = commandLine
view.indicatorText = indicatorText
view.notificationLine = notificationLine
go view.uiEventLoop()
go func() {
rpcClient.SetReturnValuesLoadConfig(&defaultConfig)
}()
go view.cmdHandler.RunCommand(&GetBreakpoints{})
return view
}