/
tview_tui.go
179 lines (145 loc) · 4.05 KB
/
tview_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
package tview
import (
"fmt"
"time"
"github.com/adrianbrad/privatebtc"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
// TUI is a Terminal User Interface for a bitcoin private network.
// It starts a new TUI app and runs it.
type TUI struct {
app *app
btcPrvNet *privatebtc.PrivateNetwork
}
const bitcoinMaxAddressLength = 44
// NewTUI creates a new Terminal User Interface for the given bitcoin private network.
func NewTUI(btcPrivateNetwork *privatebtc.PrivateNetwork, version string) *TUI {
mempoolTxs := map[string]*mempoolTxDetails{}
nodesDetails := make([]*nodeDetails, len(btcPrivateNetwork.Nodes()))
for i := range btcPrivateNetwork.Nodes() {
nodesDetails[i] = &nodeDetails{
id: btcPrivateNetwork.Nodes()[i].ID(),
connected: true,
}
}
data := &data{
btcpn: btcPrivateNetwork,
nodesDetails: nodesDetails,
mempool: mempoolTxs,
}
// Mempool
mempoolTransactionDetails := newMempoolTransactionDetails(data)
mempoolTransactionsList := newMempoolTransactionsList(mempoolTransactionDetails, data)
// Nodes
nodeDetailsView := newNodeDetailsView(data)
nodesList := newNodesList(btcPrivateNetwork.Nodes(), nodeDetailsView)
// Shared widgets
tviewAppFlex := tview.NewFlex()
tviewAppPages := tview.NewPages()
// Actions
outputView := newOutputView()
actionsHandler := newActionsHandler(data, btcPrivateNetwork)
nodeActionsList := newNodeActionsList(
tviewAppPages,
nodesList,
mempoolTransactionsList,
actionsHandler,
outputView,
nodeDetailsView,
data,
)
actionsFlex := newActionsFlex(outputView, nodeActionsList)
// init child flexes
mempoolFlex := newMempoolFlex(mempoolTransactionsList, mempoolTransactionDetails)
nodesFlex := newNodesFlex(nodesList, nodeDetailsView)
// main app flex
footer := tview.NewTextView().
SetText(fmt.Sprintf(
"Version: %s. Navigate with ←↑↓→(arrows); use ↹(Tab) for modals.",
version,
)).
SetTextAlign(tview.AlignCenter)
appFlex := newAppFlex(tviewAppFlex, mempoolFlex, actionsFlex, nodesFlex, footer)
appPages := newAppPages(tviewAppPages, nodeActionsList, appFlex)
app := newApp(appPages, nodesList, actionsFlex, mempoolTransactionsList, nodeActionsList)
go func() {
for {
const delay = time.Second / 2
<-time.After(delay)
app.QueueUpdateDraw(func() {
if err := data.update(); err != nil {
outputView.AddError(fmt.Sprintf("update error: %s", err))
return
}
mempoolTransactionsList.refresh()
nodeDetailsView.refresh()
})
}
}()
return &TUI{
app: app,
btcPrvNet: btcPrivateNetwork,
}
}
func centeredForm(p tview.Primitive, width, height int) tview.Primitive {
return tview.NewGrid().
SetColumns(0, width, 0).
SetRows(0, height, 0).
AddItem(p, 1, 1, 1, 1, 0, 0, true)
}
// Run starts the Terminal User Interface.
func (t *TUI) Run() error {
if err := t.app.Run(); err != nil {
return fmt.Errorf("run tview app: %w", err)
}
return nil
}
type app struct {
*tview.Application
}
func newApp(
appPages *appPages,
nodes *nodesList,
actionsFlex *actionsFlex,
mempoolTransactions *mempoolTransactionsList,
nodeActions *nodeActionsList,
) *app {
application := tview.NewApplication()
application.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
// nolint: exhaustive
switch event.Key() {
case tcell.KeyLeft:
switch application.GetFocus() {
case nodes:
application.SetFocus(actionsFlex)
case mempoolTransactions:
application.SetFocus(nodes)
case nodeActions:
application.SetFocus(mempoolTransactions)
}
case tcell.KeyRight:
switch application.GetFocus() {
case nodes:
application.SetFocus(mempoolTransactions)
case mempoolTransactions:
application.SetFocus(actionsFlex)
case nodeActions, actionsFlex:
application.SetFocus(nodes)
}
}
return event
})
application.SetRoot(appPages, true)
return &app{
Application: application,
}
}
func inputCaptureIgnoreLeftRight(event *tcell.EventKey) *tcell.EventKey {
// nolint: exhaustive
switch event.Key() {
case tcell.KeyLeft, tcell.KeyRight:
return nil
}
return event
}