-
Notifications
You must be signed in to change notification settings - Fork 15
/
tageditor.go
486 lines (430 loc) · 12.4 KB
/
tageditor.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
// Copyright (C) 2020 Raziman
package main
import (
"errors"
"fmt"
"strings"
"sync"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
"github.com/tramhao/id3v2"
"github.com/ztrue/tracerr"
"github.com/issadarkthing/gomu/lyric"
"github.com/issadarkthing/gomu/player"
)
// lyricFlex extend the flex control to modify the Focus item
type lyricFlex struct {
*tview.Flex
FocusedItem tview.Primitive
inputs []tview.Primitive
box *tview.Box
}
// tagPopup is used to edit tag, delete and fetch lyrics
func tagPopup(node *player.AudioFile) (err error) {
popupID := "tag-editor-input-popup"
tag, popupLyricMap, options, err := node.LoadTagMap()
if err != nil {
return tracerr.Wrap(err)
}
var (
artistInputField *tview.InputField = tview.NewInputField()
titleInputField *tview.InputField = tview.NewInputField()
albumInputField *tview.InputField = tview.NewInputField()
getTagButton *tview.Button = tview.NewButton("Get Tag")
saveTagButton *tview.Button = tview.NewButton("Save Tag")
lyricDropDown *tview.DropDown = tview.NewDropDown()
deleteLyricButton *tview.Button = tview.NewButton("Delete Lyric")
getLyricDropDown *tview.DropDown = tview.NewDropDown()
getLyricButton *tview.Button = tview.NewButton("Fetch Lyric")
lyricTextView *tview.TextView = tview.NewTextView()
leftGrid *tview.Grid = tview.NewGrid()
rightFlex *tview.Flex = tview.NewFlex()
)
artistInputField.SetLabel("Artist: ").
SetFieldWidth(20).
SetText(tag.Artist()).
SetFieldBackgroundColor(gomu.colors.popup)
titleInputField.SetLabel("Title: ").
SetFieldWidth(20).
SetText(tag.Title()).
SetFieldBackgroundColor(gomu.colors.popup)
albumInputField.SetLabel("Album: ").
SetFieldWidth(20).
SetText(tag.Album()).
SetFieldBackgroundColor(gomu.colors.popup)
leftBox := tview.NewBox().
SetBorder(true).
SetTitle(node.Name()).
SetBackgroundColor(gomu.colors.popup).
SetBorderColor(gomu.colors.accent).
SetTitleColor(gomu.colors.accent).
SetBorderPadding(1, 1, 2, 2)
getTagButton.SetSelectedFunc(func() {
var titles []string
audioFile := node
go func() {
var lyricFetcher lyric.LyricFetcherCn
results, err := lyricFetcher.LyricOptions(audioFile.Name())
if err != nil {
errorPopup(err)
return
}
for _, v := range results {
titles = append(titles, v.TitleForPopup)
}
go func() {
searchPopup(" Song Tags ", titles, func(selected string) {
if selected == "" {
return
}
var selectedIndex int
for i, v := range results {
if v.TitleForPopup == selected {
selectedIndex = i
break
}
}
newTag := results[selectedIndex]
artistInputField.SetText(newTag.Artist)
titleInputField.SetText(newTag.Title)
albumInputField.SetText(newTag.Album)
tag, err = id3v2.Open(node.Path(), id3v2.Options{Parse: true})
if err != nil {
errorPopup(err)
return
}
defer tag.Close()
tag.SetArtist(newTag.Artist)
tag.SetTitle(newTag.Title)
tag.SetAlbum(newTag.Album)
err = tag.Save()
if err != nil {
errorPopup(err)
return
}
if gomu.anko.GetBool("General.rename_bytag") {
newName := fmt.Sprintf("%s-%s", newTag.Artist, newTag.Title)
err = gomu.playlist.rename(newName)
if err != nil {
errorPopup(err)
return
}
gomu.playlist.refresh()
leftBox.SetTitle(newName)
// update queue
err = gomu.playlist.refreshAfterRename(node, newName)
if err != nil {
errorPopup(err)
return
}
node = gomu.playlist.getCurrentFile()
}
defaultTimedPopup(" Success ", "Tag update successfully")
})
gomu.app.Draw()
}()
}()
}).
SetBackgroundColorActivated(gomu.colors.popup).
SetLabelColorActivated(gomu.colors.accent).
SetBorder(true).
SetBackgroundColor(gomu.colors.popup).
SetTitleColor(gomu.colors.accent)
saveTagButton.SetSelectedFunc(func() {
tag, err = id3v2.Open(node.Path(), id3v2.Options{
Parse: true,
ParseFrames: []string{},
})
if err != nil {
errorPopup(err)
return
}
defer tag.Close()
newArtist := artistInputField.GetText()
newTitle := titleInputField.GetText()
newAlbum := albumInputField.GetText()
tag.SetArtist(newArtist)
tag.SetTitle(newTitle)
tag.SetAlbum(newAlbum)
err = tag.Save()
if err != nil {
errorPopup(err)
return
}
if gomu.anko.GetBool("General.rename_bytag") {
newName := fmt.Sprintf("%s-%s", newArtist, newTitle)
err = gomu.playlist.rename(newName)
if err != nil {
errorPopup(err)
return
}
gomu.playlist.refresh()
leftBox.SetTitle(newName)
// update queue
err = gomu.playlist.refreshAfterRename(node, newName)
if err != nil {
errorPopup(err)
return
}
node = gomu.playlist.getCurrentFile()
}
defaultTimedPopup(" Success ", "Tag update successfully")
}).
SetBackgroundColorActivated(gomu.colors.popup).
SetLabelColorActivated(gomu.colors.accent).
SetBorder(true).
SetBackgroundColor(gomu.colors.popup).
SetTitleColor(gomu.colors.foreground)
lyricDropDown.SetOptions(options, nil).
SetCurrentOption(0).
SetFieldBackgroundColor(gomu.colors.popup).
SetFieldTextColor(gomu.colors.accent).
SetPrefixTextColor(gomu.colors.accent).
SetSelectedFunc(func(text string, _ int) {
lyricTextView.SetText(popupLyricMap[text]).
SetTitle(" " + text + " lyric preview ")
}).
SetLabel("Embeded Lyrics: ")
lyricDropDown.SetBackgroundColor(gomu.colors.popup)
deleteLyricButton.SetSelectedFunc(func() {
_, langExt := lyricDropDown.GetCurrentOption()
lyric := &lyric.Lyric{
LangExt: langExt,
}
if len(options) > 0 {
err := embedLyric(node.Path(), lyric, true)
if err != nil {
errorPopup(err)
return
}
infoPopup(langExt + " lyric deleted successfully.")
// Update map
delete(popupLyricMap, langExt)
// Update dropdown options
var newOptions []string
for _, v := range options {
if v == langExt {
continue
}
newOptions = append(newOptions, v)
}
options = newOptions
lyricDropDown.SetOptions(newOptions, nil).
SetCurrentOption(0).
SetSelectedFunc(func(text string, _ int) {
lyricTextView.SetText(popupLyricMap[text]).
SetTitle(" " + text + " lyric preview ")
})
// Update lyric preview
if len(newOptions) > 0 {
_, langExt = lyricDropDown.GetCurrentOption()
lyricTextView.SetText(popupLyricMap[langExt]).
SetTitle(" " + langExt + " lyric preview ")
} else {
langExt = ""
lyricTextView.SetText("No lyric embeded.").
SetTitle(" " + langExt + " lyric preview ")
}
} else {
infoPopup("No lyric embeded.")
}
}).
SetBackgroundColorActivated(gomu.colors.popup).
SetLabelColorActivated(gomu.colors.accent).
SetBorder(true).
SetBackgroundColor(gomu.colors.popup).
SetTitleColor(gomu.colors.accent)
getLyricDropDownOptions := []string{"en", "zh-CN"}
getLyricDropDown.SetOptions(getLyricDropDownOptions, nil).
SetCurrentOption(0).
SetFieldBackgroundColor(gomu.colors.popup).
SetFieldTextColor(gomu.colors.accent).
SetPrefixTextColor(gomu.colors.accent).
SetLabel("Fetch Lyrics: ").
SetBackgroundColor(gomu.colors.popup)
langLyricFromConfig := gomu.anko.GetString("General.lang_lyric")
if strings.Contains(langLyricFromConfig, "zh-CN") {
getLyricDropDown.SetCurrentOption(1)
}
getLyricButton.SetSelectedFunc(func() {
audioFile := gomu.playlist.getCurrentFile()
_, lang := getLyricDropDown.GetCurrentOption()
if !audioFile.IsAudioFile() {
errorPopup(errors.New("not an audio file"))
return
}
var wg sync.WaitGroup
wg.Add(1)
go func() {
err := lyricPopup(lang, audioFile, &wg)
if err != nil {
errorPopup(err)
return
}
}()
go func() {
// This is to ensure that the above go routine finish.
wg.Wait()
_, popupLyricMap, newOptions, err := audioFile.LoadTagMap()
if err != nil {
errorPopup(err)
gomu.app.Draw()
return
}
options = newOptions
// Update dropdown options
gomu.app.QueueUpdateDraw(func() {
lyricDropDown.SetOptions(newOptions, nil).
SetCurrentOption(0).
SetSelectedFunc(func(text string, _ int) {
lyricTextView.SetText(popupLyricMap[text]).
SetTitle(" " + text + " lyric preview ")
})
// Update lyric preview
if len(newOptions) > 0 {
_, langExt := lyricDropDown.GetCurrentOption()
lyricTextView.SetText(popupLyricMap[langExt]).
SetTitle(" " + langExt + " lyric preview ")
} else {
lyricTextView.SetText("No lyric embeded.").
SetTitle(" lyric preview ")
}
})
}()
}).
SetBackgroundColorActivated(gomu.colors.popup).
SetLabelColorActivated(gomu.colors.accent).
SetBorder(true).
SetTitleColor(gomu.colors.accent).
SetBackgroundColor(gomu.colors.popup)
var lyricText string
_, langExt := lyricDropDown.GetCurrentOption()
lyricText = popupLyricMap[langExt]
if lyricText == "" {
lyricText = "No lyric embeded."
langExt = ""
}
lyricTextView.
SetDynamicColors(true).
SetRegions(true).
SetScrollable(true).
SetTitle(" " + langExt + " lyric preview ").
SetBorder(true)
lyricTextView.SetText(lyricText).
SetScrollable(true).
SetWordWrap(true).
SetWrap(true).
SetBorder(true)
lyricTextView.SetChangedFunc(func() {
gomu.app.QueueUpdate(func() {
lyricTextView.ScrollToBeginning()
})
})
leftGrid.SetRows(3, 1, 2, 2, 2, 3, 0, 3, 3, 1, 3, 3).
SetColumns(30).
AddItem(getTagButton, 0, 0, 1, 3, 1, 10, true).
AddItem(artistInputField, 2, 0, 1, 3, 1, 10, true).
AddItem(titleInputField, 3, 0, 1, 3, 1, 10, true).
AddItem(albumInputField, 4, 0, 1, 3, 1, 10, true).
AddItem(saveTagButton, 5, 0, 1, 3, 1, 10, true).
AddItem(getLyricDropDown, 7, 0, 1, 3, 1, 20, true).
AddItem(getLyricButton, 8, 0, 1, 3, 1, 10, true).
AddItem(lyricDropDown, 10, 0, 1, 3, 1, 10, true).
AddItem(deleteLyricButton, 11, 0, 1, 3, 1, 10, true)
rightFlex.SetDirection(tview.FlexColumn).
AddItem(lyricTextView, 0, 1, true)
lyricFlex := &lyricFlex{
tview.NewFlex().SetDirection(tview.FlexColumn).
AddItem(leftGrid, 0, 2, true).
AddItem(rightFlex, 0, 3, true),
nil,
nil,
leftBox,
}
leftGrid.Box = lyricFlex.box
lyricFlex.inputs = []tview.Primitive{
getTagButton,
artistInputField,
titleInputField,
albumInputField,
saveTagButton,
getLyricDropDown,
getLyricButton,
lyricDropDown,
deleteLyricButton,
lyricTextView,
}
if gomu.playingBar.albumPhoto != nil {
gomu.playingBar.albumPhoto.Clear()
}
gomu.pages.AddPage(popupID, center(lyricFlex, 90, 30), true, true)
gomu.popups.push(lyricFlex)
lyricFlex.SetInputCapture(func(e *tcell.EventKey) *tcell.EventKey {
switch e.Key() {
case tcell.KeyEnter:
case tcell.KeyEsc:
gomu.pages.RemovePage(popupID)
gomu.popups.pop()
case tcell.KeyTab, tcell.KeyCtrlN, tcell.KeyCtrlJ:
lyricFlex.cycleFocus(gomu.app, false)
case tcell.KeyBacktab, tcell.KeyCtrlP, tcell.KeyCtrlK:
lyricFlex.cycleFocus(gomu.app, true)
case tcell.KeyDown:
lyricFlex.cycleFocus(gomu.app, false)
case tcell.KeyUp:
lyricFlex.cycleFocus(gomu.app, true)
}
switch e.Rune() {
case 'q':
if artistInputField.HasFocus() || titleInputField.HasFocus() || albumInputField.HasFocus() {
return e
}
gomu.pages.RemovePage(popupID)
gomu.popups.pop()
}
return e
})
return err
}
// This is a hack to cycle Focus in a flex
func (f *lyricFlex) cycleFocus(app *tview.Application, reverse bool) {
for i, el := range f.inputs {
if !el.HasFocus() {
continue
}
if reverse {
i = i - 1
if i < 0 {
i = len(f.inputs) - 1
}
} else {
i = i + 1
i = i % len(f.inputs)
}
app.SetFocus(f.inputs[i])
f.FocusedItem = f.inputs[i]
// below code is setting the border highlight of left and right flex
if f.inputs[9].HasFocus() {
f.inputs[9].(*tview.TextView).SetBorderColor(gomu.colors.accent).
SetTitleColor(gomu.colors.accent)
f.box.SetBorderColor(gomu.colors.background).
SetTitleColor(gomu.colors.background)
} else {
f.inputs[9].(*tview.TextView).SetBorderColor(gomu.colors.background).
SetTitleColor(gomu.colors.background)
f.box.SetBorderColor(gomu.colors.accent).
SetTitleColor(gomu.colors.accent)
}
return
}
}
// Focus is an override of Focus function in tview.flex.
// This is to ensure that the focus of flex remain unchanged
// when returning from popups or search lists
func (f *lyricFlex) Focus(delegate func(p tview.Primitive)) {
if f.FocusedItem != nil {
gomu.app.SetFocus(f.FocusedItem)
} else {
f.Flex.Focus(delegate)
}
}