-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtui_browser.go
377 lines (316 loc) · 10 KB
/
tui_browser.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
package goidgames
import (
"fmt"
"regexp"
"strings"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
const (
pageMain = "main"
pageDLSure = "dlsure"
)
// IdgamesBrowser holds all fields of the module
type IdgamesBrowser struct {
app *tview.Application
canvas *tview.Pages
layout *tview.Grid
list *tview.Table
fileDetails *tview.TextView
reviews *tview.TextView
dlPathPreview *tview.TextView
search *tview.InputField
idgames []Idgame
downloadPath string
confirmCallback func(idgame Idgame)
postDownloadCallback func(archivePath string)
}
// NewIdgamesBrowser is the modules constructor
// Must be initialized with a *tview.Application in which it is drawn
func NewIdgamesBrowser(app *tview.Application) *IdgamesBrowser {
browser := &IdgamesBrowser{app: app}
layout := tview.NewGrid()
browser.layout = layout
layout.SetRows(5, -1, 3)
layout.SetColumns(-1, -1)
canvas := tview.NewPages()
canvas.AddPage(pageMain, layout, true, true)
browser.canvas = canvas
browser.initList()
browser.initDetails()
browser.initSearchForm()
browser.initDlPathPreview()
return browser
}
// SetConfirmCallback sets a callback function that receives the Idgame instance of a row on which "ENTER" is pressed by the user
// This callbak function could, for example, launch a download of given file
func (b *IdgamesBrowser) SetConfirmCallback(f func(idgame Idgame)) {
b.confirmCallback = f
}
// SetDownloadPath sets the path where the browser can download game files to
func (b *IdgamesBrowser) SetDownloadPath(path string) {
b.downloadPath = path
b.populatedlPathPreview()
}
// SetDownloadDoneCallback set a callback function which gets exectuted when the download has been finished
// the callback receives the local file path of the downloaded archive
func (b *IdgamesBrowser) SetPostDownloadCallback(doWhenDownloadDone func(archivePath string)) {
b.postDownloadCallback = doWhenDownloadDone
}
// GetRootLayout returns the root layout
func (b *IdgamesBrowser) GetRootLayout() *tview.Pages {
return b.canvas
}
// GetRootLayout returns the root layout
func (b *IdgamesBrowser) GetSelectedRowNumber() int {
r, _ := b.list.GetSelection()
return r
}
// FocusSearch focuses the search input field
func (b *IdgamesBrowser) FocusSearch() {
b.app.SetFocus(b.search)
}
// FocusList focuses the result list
func (b *IdgamesBrowser) FocusList() {
b.app.SetFocus(b.list)
}
// UpdateSearch triggers an API call with given search query and types and populates the UI with the results
func (browser *IdgamesBrowser) UpdateSearch(query string, types []string) {
go func() {
browser.app.QueueUpdateDraw(func() {
idgames, _ := SearchMultipleTypes(query, types, SEARCH_SORT_RATING, SEARCH_SORT_DESC)
go func() {
updateGameDetails(idgames)
}()
browser.populateList(idgames)
})
}()
}
// UpdateLatest triggers an API call for the latest entries and populates the UI with the results
func (browser *IdgamesBrowser) UpdateLatest() {
go func() {
browser.app.QueueUpdateDraw(func() {
idgames, _ := LatestFiles(50, 0)
go func() {
updateGameDetails(idgames)
}()
browser.populateList(idgames)
})
}()
}
// init search form ui component
func (b *IdgamesBrowser) initSearchForm() {
searchForm := tview.NewForm()
searchForm.SetHorizontal(true).SetBorder(true)
search := tview.NewInputField().SetLabel("Search Idgames (leave empty for latest)").SetText("").SetFieldWidth(25)
searchForm.AddFormItem(search)
searchForm.AddButton("Search", func() {
query := search.GetText()
if len(query) == 0 {
b.UpdateLatest()
} else {
types := []string{
SEARCH_TYPE_TITLE,
SEARCH_TYPE_AUTHOR,
}
b.UpdateSearch(search.GetText(), types)
}
b.app.SetFocus(b.list)
})
b.layout.AddItem(searchForm, 0, 0, 1, 2, 0, 0, true)
b.search = search
}
// init details ui component
func (b *IdgamesBrowser) initDetails() {
details := tview.NewTextView().
SetDynamicColors(true).
SetRegions(true)
details.SetBorder(true).
SetBorderPadding(0, 0, 1, 1)
b.layout.AddItem(details, 1, 1, 1, 1, 0, 0, false)
details.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
k := event.Key()
if k == tcell.KeyTAB {
b.app.SetFocus(b.list)
return nil
}
if k == tcell.KeyBacktab {
b.app.SetFocus(b.search)
return nil
}
return event
})
b.fileDetails = details
}
func (b *IdgamesBrowser) initDlPathPreview() {
dlPathPreview := tview.NewTextView().
SetDynamicColors(true).
SetRegions(true)
dlPathPreview.SetBorder(true).
SetBorderPadding(0, 0, 1, 1)
b.layout.AddItem(dlPathPreview, 2, 0, 1, 2, 0, 0, false)
b.dlPathPreview = dlPathPreview
}
// init list ui component
func (b *IdgamesBrowser) initList() {
list := tview.NewTable().
SetFixed(1, 2).
SetSelectable(true, false).
SetBorders(false).SetSeparator('|')
list.SetBorder(true)
b.layout.AddItem(list, 1, 0, 1, 1, 0, 0, false)
list.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
k := event.Key()
if k == tcell.KeyTAB {
b.app.SetFocus(b.fileDetails)
return nil
}
if k == tcell.KeyBacktab {
b.app.SetFocus(b.search)
return nil
}
return event
})
list.SetSelectedFunc(func(r int, c int) {
if r > 0 {
g := b.idgames[r-1]
// custom callback when enter is hit on a selection
if b.confirmCallback != nil {
b.confirmCallback(g)
} else {
// if there is no custom callback, a download is initiated
b.canvas.AddPage(pageDLSure, sureDownloadBox(fmt.Sprintf("Download %v?", g.Title),
func() {
g.DownloadTo(b.downloadPath)
b.canvas.RemovePage(pageDLSure)
b.app.SetFocus(b.list)
},
func() {
b.canvas.RemovePage(pageDLSure)
b.app.SetFocus(b.list)
},
8,
r+1,
b.list.Box),
true, true)
}
}
})
b.list = list
}
// updateGameDetails iterates the given slice and fetches the detail data from Idgames via the api's get function
func updateGameDetails(idgames []Idgame) {
for i := range idgames {
g, err := Get(idgames[i].Id, "")
if err != nil {
continue
}
idgames[i] = g
}
}
// populateList populates the UIs list
func (browser *IdgamesBrowser) populateList(idgames []Idgame) {
browser.list.Clear()
browser.idgames = idgames
// header
browser.list.SetCell(0, 0, tview.NewTableCell("Rating").SetTextColor(tview.Styles.SecondaryTextColor))
browser.list.SetCell(0, 1, tview.NewTableCell("Title").SetTextColor(tview.Styles.SecondaryTextColor))
browser.list.SetCell(0, 2, tview.NewTableCell("Author").SetTextColor(tview.Styles.SecondaryTextColor))
browser.list.SetCell(0, 3, tview.NewTableCell("Date").SetTextColor(tview.Styles.SecondaryTextColor))
browser.list.SetSelectionChangedFunc(func(r int, c int) {
switch r {
case 0:
return
default:
browser.populateDetails(idgames[r-1])
}
})
fixRows := 1
cols := 4
rows := len(idgames)
for r := 1; r < rows+fixRows; r++ {
var f Idgame
if r > 0 {
f = idgames[r-fixRows]
}
for c := 0; c < cols; c++ {
var cell *tview.TableCell
switch c {
case 0:
cell = tview.NewTableCell(ratingString(f.Rating)).SetTextColor(tview.Styles.PrimaryTextColor)
case 1:
cell = tview.NewTableCell(f.Title).SetTextColor(tview.Styles.PrimaryTextColor)
case 2:
cell = tview.NewTableCell(f.Author).SetTextColor(tview.Styles.PrimaryTextColor)
case 3:
cell = tview.NewTableCell(f.Date).SetTextColor(tview.Styles.PrimaryTextColor)
default:
cell = tview.NewTableCell("").SetTextColor(tview.Styles.PrimaryTextColor)
}
browser.list.SetCell(r, c, cell)
}
}
browser.list.ScrollToBeginning()
}
// populate the detail panelayout
func (browser *IdgamesBrowser) populateDetails(idgame Idgame) {
browser.fileDetails.Clear()
// stylize the text file a bit
re := regexp.MustCompile(`^(\S.*?):(.*)`)
for _, line := range strings.Split(idgame.Textfile, "\n") {
line = re.ReplaceAllString(line, fmt.Sprintf("%s$1:%s$2", hexStringFromColor(tview.Styles.MoreContrastBackgroundColor), hexStringFromColor(tview.Styles.PrimaryTextColor)))
line = strings.Replace(line, "===========================================================================",
hexStringFromColor(tview.Styles.MoreContrastBackgroundColor)+"==========================================================================="+hexStringFromColor(tview.Styles.PrimaryTextColor),
1)
fmt.Fprintf(browser.fileDetails, "%s\n", line)
}
browser.fileDetails.ScrollToBeginning()
}
// populate the detail panelayout
func (browser *IdgamesBrowser) populatedlPathPreview() {
browser.dlPathPreview.Clear()
fmt.Fprintf(browser.dlPathPreview, "%sDownload to:%s %s", hexStringFromColor(tview.Styles.MoreContrastBackgroundColor), hexStringFromColor(tview.Styles.PrimaryTextColor), browser.downloadPath)
}
// helper to make a string from the games rating
func ratingString(rating float32) string {
return strings.Repeat("*", int(rating)) + strings.Repeat("-", 5-int(rating))
}
// help for navigation
func sureDownloadBox(title string, onOk func(), onCancel func(), xOffset int, yOffset int, container *tview.Box) *tview.Flex {
okbtn := tview.NewButton("foo")
okbtn.SetSelectedFunc(onOk)
youSureForm := tview.NewForm()
youSureForm.
SetBorder(true).
SetTitle(title)
youSureForm.SetFocus(1)
youSureForm.
AddButton("Download", func() {
youSureForm.GetButton(0).SetLabel("Hold on...")
onOk()
}).
AddButton("Cancel", onCancel)
height := 5
width := 75
// surrounding layout
_, _, _, containerHeight := container.GetRect()
helpHeight := 5
// default: right below the selected game
// though, if it flows out of the screen, then on top of the game
if yOffset+height > containerHeight+helpHeight {
yOffset = yOffset - height - 1
}
youSureLayout := tview.NewFlex().SetDirection(tview.FlexRow).
AddItem(nil, yOffset, 0, false).
AddItem(tview.NewFlex().SetDirection(tview.FlexColumn).
AddItem(nil, xOffset, 0, false).
AddItem(youSureForm, width, 0, true).
AddItem(nil, 0, 1, false),
height, 1, true).
AddItem(nil, 0, 1, false)
return youSureLayout
}
func hexStringFromColor(c tcell.Color) string {
r, g, b := c.RGB()
return fmt.Sprintf("[#%02x%02x%02x]", r, g, b)
}