-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui.go
83 lines (71 loc) · 2.21 KB
/
ui.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
package main
import (
"github.com/gdamore/tcell/v2"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/nustiueudinastea/doltswarm"
"github.com/rivo/tview"
)
func uiUpdate(app *tview.Application, peerListView *tview.List, commitTreeRoot *tview.TreeNode, textView *tview.TextView, peerListChan chan peer.IDSlice, commitListChan chan []doltswarm.Commit, eventChan chan []byte) func() error {
stopSignal := make(chan struct{})
go func() {
log.Info("Starting UI updater")
for {
select {
case peerList := <-peerListChan:
peerListView.Clear()
for _, peer := range peerList {
peerListView.AddItem(peer.ShortString(), "", 0, nil)
}
app.Draw()
case event := <-eventChan:
_, err := textView.Write(event)
if err != nil {
panic(err)
}
app.Draw()
case commitList := <-commitListChan:
commitTreeRoot.ClearChildren()
for _, commit := range commitList {
node := tview.NewTreeNode(commit.Hash + " - " + commit.Message)
commitTreeRoot.AddChild(node)
}
app.Draw()
case <-stopSignal:
log.Info("Stopping ui updater")
return
}
}
}()
stopper := func() error {
stopSignal <- struct{}{}
return nil
}
return stopper
}
func createUI(peerListChan chan peer.IDSlice, commitListChan chan []doltswarm.Commit, eventChan chan []byte) *tview.Application {
var app = tview.NewApplication()
var flex = tview.NewFlex()
flexRow := tview.NewFlex().SetDirection(tview.FlexRow)
commitTreeRoot := tview.NewTreeNode(".").SetColor(tcell.ColorRed)
commitTree := tview.NewTreeView().SetRoot(commitTreeRoot)
commitTree.SetBorder(true).SetTitle("Commits")
logView := tview.NewTextView()
logView.SetBorder(true).SetTitle("Events")
peerList := tview.NewList()
peerList.SetBorder(true).SetTitle("Peers")
flexRow.AddItem(peerList, 0, 1, false).
AddItem(commitTree, 0, 5, false)
flex.AddItem(flexRow, 0, 1, false).
AddItem(logView, 0, 1, false)
uiUpdateStopper := uiUpdate(app, peerList, commitTreeRoot, logView, peerListChan, commitListChan, eventChan)
app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
if event.Key() == tcell.KeyCtrlC {
uiUpdateStopper()
app.Stop()
}
return event
})
app.SetRoot(flex, true)
app.EnableMouse(true)
return app
}