forked from issadarkthing/gomu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.go
366 lines (285 loc) · 8.05 KB
/
popup.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
// Copyright (C) 2020 Raziman
package main
import (
"fmt"
"regexp"
"time"
"github.com/gdamore/tcell"
"github.com/rivo/tview"
"github.com/spf13/viper"
)
// this is used to make the popup unique
// this mitigates the issue of closing all popups when timeout ends
var (
popupCounter = 0
)
// Simple stack data structure
type Stack struct {
popups []tview.Primitive
}
// Push popup to the stack and focus
func (s *Stack) push(p tview.Primitive) {
s.popups = append(s.popups, p)
gomu.app.SetFocus(p)
}
// Show item on the top of the stack
func (s *Stack) peekTop() tview.Primitive {
if len(s.popups)-1 < 0 {
return nil
}
return s.popups[len(s.popups)-1]
}
// Remove popup from the stack and focus previous popup
func (s *Stack) pop() tview.Primitive {
if len(s.popups) == 0 {
return nil
}
last := s.popups[len(s.popups)-1]
res := s.popups[:len(s.popups)-1]
s.popups = res
// focus previous popup
if len(s.popups) > 0 {
gomu.app.SetFocus(s.popups[len(s.popups)-1])
} else {
// focus the panel if no popup left
gomu.app.SetFocus(gomu.prevPanel.(tview.Primitive))
}
return last
}
// Gets popup timeout from config file
func getPopupTimeout() time.Duration {
dur := viper.GetString("general.popup_timeout")
m, err := time.ParseDuration(dur)
if err != nil {
logError(err)
return time.Second * 5
}
return m
}
// Simple confirmation popup. Accepts callback
func confirmationPopup(
text string,
handler func(buttonIndex int, buttonLabel string),
) {
modal := tview.NewModal().
SetText(text).
SetBackgroundColor(gomu.popupBg).
AddButtons([]string{"no", "yes"}).
SetButtonBackgroundColor(gomu.popupBg).
SetButtonTextColor(gomu.accentColor).
SetDoneFunc(func(indx int, label string) {
handler(indx, label)
gomu.pages.RemovePage("confirmation-popup")
gomu.popups.pop()
})
gomu.pages.
AddPage("confirmation-popup", center(modal, 40, 10), true, true)
gomu.popups.push(modal)
}
func center(p tview.Primitive, width, height int) tview.Primitive {
return tview.NewFlex().
AddItem(nil, 0, 1, false).
AddItem(tview.NewFlex().SetDirection(tview.FlexRow).
AddItem(nil, 0, 1, false).
AddItem(p, height, 1, false).
AddItem(nil, 0, 1, false), width, 1, false).
AddItem(nil, 0, 1, false)
}
func topRight(p tview.Primitive, width, height int) tview.Primitive {
return tview.NewFlex().
AddItem(nil, 0, 23, false).
AddItem(tview.NewFlex().SetDirection(tview.FlexRow).
AddItem(nil, 0, 1, false).
AddItem(p, height, 1, false).
AddItem(nil, 0, 15, false), width, 1, false).
AddItem(nil, 0, 1, false)
}
// Width and height parameter are optional, provide 0 for both to use deault values.
// It defaults to 70 and 7 respectively.
func timedPopup(
title string, desc string, timeout time.Duration, width, height int,
) {
// Wait until app is not suspended
for {
if !gomu.isSuspend {
break
}
}
if width == 0 && height == 0 {
width = 70
height = 7
}
textView := tview.NewTextView().
SetText(desc).
SetTextColor(gomu.accentColor)
// debugLog(fmt.Sprintf("from color: %d", tcell.GetColor("#FF0000")))
// debugLog(fmt.Sprintf("from color: %d", tcell.GetColor("#0A0F14")))
// debugLog(fmt.Sprintf("from config: %d", gomu.popupBg))
textView.SetTextAlign(tview.AlignCenter).SetBackgroundColor(gomu.popupBg)
box := tview.NewFrame(textView).SetBorders(1, 0, 0, 0, 0, 0)
box.SetTitle(title).SetBorder(true).SetBackgroundColor(gomu.popupBg)
popupId := fmt.Sprintf("%s %d", "timeout-popup", popupCounter)
popupCounter++
gomu.pages.AddPage(popupId, topRight(box, width, height), true, true)
gomu.app.SetFocus(gomu.prevPanel.(tview.Primitive))
go func() {
time.Sleep(timeout)
gomu.pages.RemovePage(popupId)
gomu.app.Draw()
// timed popup shouldn't get focused
// this here check if another popup exists and focus that instead of panel
// if none continue focus panel
topPopup := gomu.popups.peekTop()
if topPopup == nil {
gomu.app.SetFocus(gomu.prevPanel.(tview.Primitive))
} else {
gomu.app.SetFocus(topPopup)
}
}()
}
// Shows popup for the current volume
func volumePopup(volume float64) {
currVol := volToHuman(volume)
maxVol := 100
// max progress bar length
maxLength := 50
progressBar := progresStr(currVol, maxVol, maxLength, "█", "-")
progress := fmt.Sprintf("\n%d |%s| %d",
currVol,
progressBar,
maxVol,
)
timedPopup(" Volume ", progress, getPopupTimeout(), 0, 0)
}
// Shows a list of keybind. The upper list is the local keybindings to specific
// panel only. The lower list is the global keybindings
func helpPopup(panel Panel) {
helpText := panel.help()
genHelp := []string{
" ",
"tab change panel",
"space toggle play/pause",
"esc close popup",
"n skip",
"q quit",
"+ volume up",
"- volume down",
"? toggle help",
}
list := tview.NewList().ShowSecondaryText(false)
list.SetBackgroundColor(gomu.popupBg).SetTitle(" Help ").
SetBorder(true)
list.SetSelectedBackgroundColor(gomu.popupBg).
SetSelectedTextColor(gomu.accentColor)
for _, v := range append(helpText, genHelp...) {
list.AddItem(v, "", 0, nil)
}
prev := func() {
currIndex := list.GetCurrentItem()
list.SetCurrentItem(currIndex - 1)
}
next := func() {
currIndex := list.GetCurrentItem()
idx := currIndex + 1
if currIndex == list.GetItemCount()-1 {
idx = 0
}
list.SetCurrentItem(idx)
}
list.SetInputCapture(func(e *tcell.EventKey) *tcell.EventKey {
switch e.Rune() {
case 'j':
next()
case 'k':
prev()
}
switch e.Key() {
case tcell.KeyEsc:
gomu.pages.RemovePage("help-page")
gomu.popups.pop()
}
return nil
})
gomu.pages.AddPage("help-page", center(list, 50, 30), true, true)
gomu.popups.push(list)
}
// Input popup. Takes video url from youtube to be downloaded
func downloadMusicPopup(selPlaylist *tview.TreeNode) {
re := regexp.MustCompile(`^((?:https?:)?\/\/)?((?:www|m)\.)?((?:youtube\.com|youtu.be))(\/(?:[\w\-]+\?v=|embed\/|v\/)?)([\w\-]+)(\S+)?$`)
inputField := tview.NewInputField().
SetLabel("Youtube url: ").
SetFieldWidth(0).
SetAcceptanceFunc(tview.InputFieldMaxLength(50)).
SetFieldBackgroundColor(gomu.accentColor).
SetFieldTextColor(tcell.ColorBlack)
inputField.SetBackgroundColor(gomu.popupBg).
SetBorder(true).SetTitle(" Ytdl ")
inputField.SetDoneFunc(func(key tcell.Key) {
switch key {
case tcell.KeyEnter:
url := inputField.GetText()
// check if valid youtube url was given
if re.MatchString(url) {
go func() {
if err := ytdl(url, selPlaylist); err != nil {
logError(err)
}
}()
} else {
timedPopup("Invalid url", "Invalid youtube url was given",
getPopupTimeout(), 0, 0)
}
gomu.pages.RemovePage("download-input-popup")
gomu.popups.pop()
case tcell.KeyEscape:
gomu.pages.RemovePage("download-input-popup")
gomu.popups.pop()
}
gomu.app.SetFocus(gomu.prevPanel.(tview.Primitive))
})
gomu.pages.
AddPage("download-input-popup", center(inputField, 50, 4), true, true)
gomu.popups.push(inputField)
}
// Input popup that takes the name of directory to be created
func createPlaylistPopup() {
inputField := tview.NewInputField().
SetLabel("Enter a playlist name: ").
SetFieldWidth(0).
SetAcceptanceFunc(tview.InputFieldMaxLength(50)).
SetFieldBackgroundColor(gomu.accentColor).
SetFieldTextColor(tcell.ColorBlack)
inputField.
SetBackgroundColor(gomu.popupBg).
SetBorder(true).
SetTitle(" New Playlist ")
inputField.SetDoneFunc(func(key tcell.Key) {
switch key {
case tcell.KeyEnter:
playListName := inputField.GetText()
err := gomu.playlist.createPlaylist(playListName)
if err != nil {
logError(err)
}
gomu.pages.RemovePage("mkdir-input-popup")
gomu.popups.pop()
case tcell.KeyEsc:
gomu.pages.RemovePage("mkdir-input-popup")
gomu.popups.pop()
}
})
gomu.pages.
AddPage("mkdir-input-popup", center(inputField, 50, 4), true, true)
gomu.popups.push(inputField)
}
func exitConfirmation(args Args) {
confirmationPopup("Are you sure to exit?", func(_ int, label string) {
if label == "no" || label == "" {
return
}
err := gomu.quit(args)
if err != nil {
logError(err)
}
})
}