-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathinteractive_output_controller.go
More file actions
224 lines (195 loc) · 6.33 KB
/
interactive_output_controller.go
File metadata and controls
224 lines (195 loc) · 6.33 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
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
package controller
import (
"fmt"
"strings"
"unicode"
"github.com/gdamore/tcell/v3"
"github.com/rivo/tview"
"github.com/confluentinc/cli/v4/pkg/flink/components"
"github.com/confluentinc/cli/v4/pkg/flink/config"
"github.com/confluentinc/cli/v4/pkg/flink/internal/utils"
"github.com/confluentinc/cli/v4/pkg/flink/types"
"github.com/confluentinc/cli/v4/pkg/log"
"github.com/confluentinc/cli/v4/pkg/output"
)
const errorDuringTableView = "Error: internal error occurred while in table view"
type InteractiveOutputController struct {
app *tview.Application
tableView components.TableViewInterface
resultFetcher types.ResultFetcherInterface
isRowViewOpen bool
userProperties types.UserPropertiesInterface
debug bool
}
func NewInteractiveOutputController(tableView components.TableViewInterface, resultFetcher types.ResultFetcherInterface, userProperties types.UserPropertiesInterface, debug bool) types.OutputControllerInterface {
return &InteractiveOutputController{
app: tview.NewApplication(),
tableView: tableView,
resultFetcher: resultFetcher,
userProperties: userProperties,
debug: debug,
}
}
func (t *InteractiveOutputController) VisualizeResults() {
t.init()
t.startTView() // this is blocking
t.close()
}
func (t *InteractiveOutputController) startTView() {
err := t.app.Run()
if err != nil {
log.CliLogger.Errorf("%s, %v", errorDuringTableView, err)
utils.OutputErr(errorDuringTableView)
}
}
func (t *InteractiveOutputController) close() {
t.resultFetcher.Close()
output.Println(false, "Result retrieval aborted.")
}
func (t *InteractiveOutputController) init() {
t.isRowViewOpen = false
t.resultFetcher.SetRefreshCallback(t.renderTableAsync)
t.resultFetcher.ToggleRefresh()
t.app.SetInputCapture(t.inputCapture)
t.tableView.Init()
t.updateTable()
}
func (t *InteractiveOutputController) updateTable() {
t.tableView.GetFocusableElement().SetBorder(t.withBorder())
t.tableView.RenderTable(t.getTableTitle(), t.resultFetcher.GetMaterializedStatementResults(), t.resultFetcher.GetLastRefreshTimestamp(), t.resultFetcher.GetRefreshState())
t.renderTableView()
}
func (t *InteractiveOutputController) renderTableView() {
t.app.SetRoot(t.tableView.GetRoot(), true).EnableMouse(false)
t.app.SetFocus(t.tableView.GetFocusableElement())
}
func (t *InteractiveOutputController) renderTableAsync() {
t.app.QueueUpdateDraw(t.updateTable)
}
// Function to handle shortcuts and keybindings for TView
func (t *InteractiveOutputController) inputCapture(event *tcell.EventKey) *tcell.EventKey {
if t.isRowViewOpen {
return t.inputHandlerRowView(event)
}
return t.inputHandlerTableView(event)
}
func (t *InteractiveOutputController) inputHandlerRowView(event *tcell.EventKey) *tcell.EventKey {
switch event.Key() {
case tcell.KeyRune:
shortcut := string(unicode.ToUpper(event.Rune()))
switch shortcut {
case components.ExitRowViewShortcut:
t.closeRowView()
}
return nil
case tcell.KeyCtrlQ:
fallthrough
case tcell.KeyEscape:
t.closeRowView()
return nil
}
return event
}
func (t *InteractiveOutputController) closeRowView() {
t.renderTableView()
t.isRowViewOpen = false
}
func (t *InteractiveOutputController) inputHandlerTableView(event *tcell.EventKey) *tcell.EventKey {
switch event.Key() {
case tcell.KeyRune:
char := unicode.ToUpper(event.Rune())
action := t.getActionForShortcut(string(char))
if action != nil {
action()
}
return nil
case tcell.KeyEscape, tcell.KeyCtrlQ:
t.app.Stop()
return nil
case tcell.KeyEnter:
t.renderRowView()
return nil
case tcell.KeyUp, tcell.KeyDown:
return t.handleKeyUpOrDownPress(event)
}
return event
}
func (t *InteractiveOutputController) getActionForShortcut(shortcut string) func() {
switch shortcut {
case components.ExitTableViewShortcut:
return t.app.Stop
case components.ToggleTableModeShortcut:
return t.toggleTableMode
case components.ToggleRefreshShortcut:
return t.toggleRefresh
case components.JumpUpShortcut:
return t.stopRefreshOrScroll(t.tableView.JumpUp)
case components.JumpDownShortcut:
return t.stopRefreshOrScroll(t.tableView.JumpDown)
}
return nil
}
func (t *InteractiveOutputController) toggleTableMode() {
t.resultFetcher.ToggleTableMode()
t.updateTable()
}
func (t *InteractiveOutputController) toggleRefresh() {
if t.resultFetcher.GetRefreshState() != types.Completed {
t.resultFetcher.ToggleRefresh()
t.updateTable()
}
}
func (t *InteractiveOutputController) stopRefreshOrScroll(scroll func()) func() {
if t.resultFetcher.IsRefreshRunning() {
return func() {
t.resultFetcher.ToggleRefresh()
t.updateTable()
}
}
return scroll
}
func (t *InteractiveOutputController) renderRowView() {
if !t.resultFetcher.IsRefreshRunning() {
row := t.tableView.GetSelectedRow()
t.isRowViewOpen = true
headers := t.resultFetcher.GetMaterializedStatementResults().GetHeaders()
sb := strings.Builder{}
for rowIdx, field := range row.GetFields() {
sb.WriteString(fmt.Sprintf("[yellow]%s:\n[white]%s\n\n", tview.Escape(headers[rowIdx]), tview.Escape(field.ToString())))
}
textView := tview.NewTextView().SetText(sb.String())
// mouse needs to be disabled, otherwise selecting text with the cursor won't work
rowView := components.CreateRowView(textView, t.withBorder())
t.app.SetRoot(rowView, true).EnableMouse(false)
t.app.SetFocus(textView)
}
}
func (t *InteractiveOutputController) handleKeyUpOrDownPress(event *tcell.EventKey) *tcell.EventKey {
if t.resultFetcher.IsRefreshRunning() {
t.resultFetcher.ToggleRefresh()
t.updateTable()
return nil
}
return event
}
func (t *InteractiveOutputController) withBorder() bool {
return t.userProperties.GetOutputFormat() != config.OutputFormatPlainText
}
func (t *InteractiveOutputController) getTableTitle() string {
mode := "Changelog mode"
if t.resultFetcher.IsTableMode() {
mode = "Table mode"
}
if t.debug {
return fmt.Sprintf(
" %s (%s) | last page size: %d | current cache size: %d/%d | table size: %d ",
mode,
t.resultFetcher.GetStatement().StatementName,
t.resultFetcher.GetStatement().GetPageSize(),
t.resultFetcher.GetMaterializedStatementResults().GetChangelogSize(),
t.resultFetcher.GetMaterializedStatementResults().GetMaxResults(),
t.resultFetcher.GetMaterializedStatementResults().GetTableSize(),
)
}
return fmt.Sprintf(" %s (%s) ", mode, t.resultFetcher.GetStatement().StatementName)
}