-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathstart.go
558 lines (461 loc) · 11.7 KB
/
start.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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
// Copyright (C) 2020 Raziman
package main
import (
"errors"
"flag"
"fmt"
"io/ioutil"
"os"
"os/signal"
"path/filepath"
"strings"
"sync"
"syscall"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
"github.com/ztrue/tracerr"
"github.com/issadarkthing/gomu/anko"
"github.com/issadarkthing/gomu/hook"
"github.com/issadarkthing/gomu/player"
)
// Panel is used to keep track of childrens in slices
type Panel interface {
HasFocus() bool
SetBorderColor(color tcell.Color) *tview.Box
SetTitleColor(color tcell.Color) *tview.Box
SetTitle(s string) *tview.Box
GetTitle() string
help() []string
}
// Args is the args for gomu executable
type Args struct {
config *string
empty *bool
music *string
version *bool
}
func getArgs() Args {
cfd, err := os.UserConfigDir()
if err != nil {
logError(tracerr.Wrap(err))
}
configPath := filepath.Join(cfd, "gomu", "config")
configFlag := flag.String("config", configPath, "Specify config file")
emptyFlag := flag.Bool("empty", false, "Open gomu with empty queue. Does not override previous queue")
home, err := os.UserHomeDir()
if err != nil {
logError(tracerr.Wrap(err))
}
musicPath := filepath.Join(home, "Music")
musicFlag := flag.String("music", musicPath, "Specify music directory")
versionFlag := flag.Bool("version", false, "Print gomu version")
flag.Parse()
return Args{
config: configFlag,
empty: emptyFlag,
music: musicFlag,
version: versionFlag,
}
}
// built-in functions
func defineBuiltins() {
gomu.anko.DefineGlobal("debug_popup", debugPopup)
gomu.anko.DefineGlobal("info_popup", infoPopup)
gomu.anko.DefineGlobal("input_popup", inputPopup)
gomu.anko.DefineGlobal("show_popup", defaultTimedPopup)
gomu.anko.DefineGlobal("search_popup", searchPopup)
gomu.anko.DefineGlobal("shell", shell)
}
func defineInternals() {
playlist, _ := gomu.anko.NewModule("Playlist")
playlist.Define("get_focused", gomu.playlist.getCurrentFile)
playlist.Define("focus", func(filepath string) {
root := gomu.playlist.GetRoot()
root.Walk(func(node, _ *tview.TreeNode) bool {
if node.GetReference().(*player.AudioFile).Path() == filepath {
gomu.playlist.setHighlight(node)
return false
}
return true
})
})
queue, _ := gomu.anko.NewModule("Queue")
queue.Define("get_focused", func() *player.AudioFile {
index := gomu.queue.GetCurrentItem()
if index < 0 || index > len(gomu.queue.items)-1 {
return nil
}
item := gomu.queue.items[index]
return item
})
player, _ := gomu.anko.NewModule("Player")
player.Define("current_audio", gomu.player.GetCurrentSong)
}
func setupHooks(hook *hook.EventHook, anko *anko.Anko) {
events := []string{
"enter",
"new_song",
"skip",
"play",
"pause",
"exit",
}
for _, event := range events {
name := event
hook.AddHook(name, func() {
src := fmt.Sprintf(`Event.run_hooks("%s")`, name)
_, err := anko.Execute(src)
if err != nil {
err = tracerr.Errorf("error execute hook: %w", err)
logError(err)
}
})
}
}
// loadModules executes helper modules and default config that should only be
// executed once
func loadModules(env *anko.Anko) error {
const listModule = `
module List {
func collect(l, f) {
result = []
for x in l {
result += f(x)
}
return result
}
func filter(l, f) {
result = []
for x in l {
if f(x) {
result += x
}
}
return result
}
func reduce(l, f, acc) {
for x in l {
acc = f(acc, x)
}
return acc
}
}
`
const eventModule = `
module Event {
events = {}
func add_hook(name, f) {
hooks = events[name]
if hooks == nil {
events[name] = [f]
return
}
hooks += f
events[name] = hooks
}
func run_hooks(name) {
hooks = events[name]
if hooks == nil {
return
}
for hook in hooks {
hook()
}
}
}
`
const keybindModule = `
module Keybinds {
global = {}
playlist = {}
queue = {}
func def_g(kb, f) {
global[kb] = f
}
func def_p(kb, f) {
playlist[kb] = f
}
func def_q(kb, f) {
queue[kb] = f
}
}
`
_, err := env.Execute(eventModule + listModule + keybindModule)
if err != nil {
return tracerr.Wrap(err)
}
return nil
}
// executes user config with default config is executed first in order to apply
// default values
func execConfig(config string) error {
const defaultConfig = `
module General {
# confirmation popup to add the whole playlist to the queue
confirm_bulk_add = true
confirm_on_exit = true
queue_loop = false
load_prev_queue = true
popup_timeout = "5s"
sort_by_mtime = false
# change this to directory that contains mp3 files
music_dir = "~/Music"
# url history of downloaded audio will be saved here
history_path = "~/.local/share/gomu/urls"
# some of the terminal supports unicode character
# you can set this to true to enable emojis
use_emoji = true
# initial volume when gomu starts up
volume = 80
# if you experiencing error using this invidious instance, you can change it
# to another instance from this list:
# https://github.com/iv-org/documentation/blob/master/Invidious-Instances.md
invidious_instance = "https://vid.puffyan.us"
# Prefered language for lyrics to be displayed, if not available, english version
# will be displayed.
# Available tags: en,el,ko,es,th,vi,zh-Hans,zh-Hant,zh-CN and can be separated with comma.
# find more tags: youtube-dl --skip-download --list-subs "url"
lang_lyric = "en"
# When save tag, could rename the file by tag info: artist-songname-album
rename_bytag = false
}
module Emoji {
# default emoji here is using awesome-terminal-fonts
# you can change these to your liking
playlist = ""
file = ""
loop = "ﯩ"
noloop = ""
}
module Color {
# you may choose colors by pressing 'c'
accent = "darkcyan"
background = "none"
foreground = "white"
popup = "black"
playlist_directory = "darkcyan"
playlist_highlight = "darkcyan"
queue_highlight = "darkcyan"
now_playing_title = "darkgreen"
subtitle = "darkgoldenrod"
}
# you can get the syntax highlighting for this language here:
# https://github.com/mattn/anko/tree/master/misc/vim
# vim: ft=anko
`
cfg := expandTilde(config)
_, err := os.Stat(cfg)
if os.IsNotExist(err) {
err = appendFile(cfg, defaultConfig)
if err != nil {
return tracerr.Wrap(err)
}
}
content, err := ioutil.ReadFile(cfg)
if err != nil {
return tracerr.Wrap(err)
}
// execute default config
_, err = gomu.anko.Execute(defaultConfig)
if err != nil {
return tracerr.Wrap(err)
}
// execute user config
_, err = gomu.anko.Execute(string(content))
if err != nil {
return tracerr.Wrap(err)
}
return nil
}
// Sets the layout of the application
func layout(gomu *Gomu) *tview.Flex {
flex := tview.NewFlex().
AddItem(gomu.playlist, 0, 1, false).
AddItem(tview.NewFlex().SetDirection(tview.FlexRow).
AddItem(gomu.queue, 0, 5, false).
AddItem(gomu.playingBar, 9, 0, false), 0, 2, false)
return flex
}
// Initialize
func start(application *tview.Application, args Args) {
// Print version and exit
if *args.version {
fmt.Printf("Gomu %s\n", VERSION)
return
}
// Assigning to global variable gomu
gomu = newGomu()
gomu.command.defineCommands()
defineBuiltins()
err := loadModules(gomu.anko)
if err != nil {
die(err)
}
err = execConfig(expandFilePath(*args.config))
if err != nil {
die(err)
}
setupHooks(gomu.hook, gomu.anko)
gomu.hook.RunHooks("enter")
gomu.args = args
gomu.colors = newColor()
// override default border
// change double line border to one line border when focused
tview.Borders.HorizontalFocus = tview.Borders.Horizontal
tview.Borders.VerticalFocus = tview.Borders.Vertical
tview.Borders.TopLeftFocus = tview.Borders.TopLeft
tview.Borders.TopRightFocus = tview.Borders.TopRight
tview.Borders.BottomLeftFocus = tview.Borders.BottomLeft
tview.Borders.BottomRightFocus = tview.Borders.BottomRight
tview.Styles.PrimitiveBackgroundColor = gomu.colors.popup
gomu.initPanels(application, args)
defineInternals()
gomu.player.SetSongStart(func(audio player.Audio) {
duration, err := getTagLength(audio.Path())
if err != nil || duration == 0 {
duration, err = player.GetLength(audio.Path())
if err != nil {
logError(err)
return
}
}
audioFile := audio.(*player.AudioFile)
gomu.playingBar.newProgress(audioFile, int(duration.Seconds()))
name := audio.Name()
var description string
if len(gomu.playingBar.subtitles) == 0 {
description = name
} else {
lang := gomu.playingBar.subtitle.LangExt
description = fmt.Sprintf("%s \n\n %s lyric loaded", name, lang)
}
defaultTimedPopup(" Now Playing ", description)
go func() {
err := gomu.playingBar.run()
if err != nil {
logError(err)
}
}()
})
gomu.player.SetSongFinish(func(currAudio player.Audio) {
gomu.playingBar.subtitles = nil
var mu sync.Mutex
mu.Lock()
gomu.playingBar.subtitle = nil
mu.Unlock()
if gomu.queue.isLoop {
_, err = gomu.queue.enqueue(currAudio.(*player.AudioFile))
if err != nil {
logError(err)
}
}
if len(gomu.queue.items) > 0 {
err := gomu.queue.playQueue()
if err != nil {
logError(err)
}
} else {
gomu.playingBar.setDefault()
}
})
flex := layout(gomu)
gomu.pages.AddPage("main", flex, true, true)
// sets the first focused panel
gomu.setFocusPanel(gomu.playlist)
gomu.prevPanel = gomu.playlist
gomu.playingBar.setDefault()
gomu.queue.isLoop = gomu.anko.GetBool("General.queue_loop")
loadQueue := gomu.anko.GetBool("General.load_prev_queue")
if !*args.empty && loadQueue {
// load saved queue from previous session
if err := gomu.queue.loadQueue(); err != nil {
logError(err)
}
}
if len(gomu.queue.items) > 0 {
if err := gomu.queue.playQueue(); err != nil {
logError(err)
}
}
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
go func() {
sig := <-sigs
errMsg := fmt.Sprintf("Received %s. Exiting program", sig.String())
logError(errors.New(errMsg))
err := gomu.quit(args)
if err != nil {
logError(errors.New("unable to quit program"))
}
}()
cmds := map[rune]string{
'q': "quit",
' ': "toggle_pause",
'+': "volume_up",
'=': "volume_up",
'-': "volume_down",
'_': "volume_down",
'n': "skip",
':': "command_search",
'?': "toggle_help",
'f': "forward",
'F': "forward_fast",
'b': "rewind",
'B': "rewind_fast",
'm': "repl",
'T': "switch_lyric",
'c': "show_colors",
}
for key, cmdName := range cmds {
src := fmt.Sprintf(`Keybinds.def_g("%c", %s)`, key, cmdName)
gomu.anko.Execute(src)
}
// global keybindings are handled here
application.SetInputCapture(func(e *tcell.EventKey) *tcell.EventKey {
if gomu.pages.HasPage("repl-input-popup") {
return e
}
if gomu.pages.HasPage("tag-editor-input-popup") {
return e
}
popupName, _ := gomu.pages.GetFrontPage()
// disables keybindings when writing in input fields
if strings.Contains(popupName, "-input-") {
return e
}
switch e.Key() {
// cycle through each section
case tcell.KeyTAB:
if strings.Contains(popupName, "confirmation-") {
return e
}
gomu.cyclePanels2()
}
if gomu.anko.KeybindExists("global", e) {
err := gomu.anko.ExecKeybind("global", e)
if err != nil {
errorPopup(err)
}
return nil
}
return e
})
// fix transparent background issue
gomu.app.SetBeforeDrawFunc(func(screen tcell.Screen) bool {
screen.Clear()
return false
})
init := false
gomu.app.SetAfterDrawFunc(func(_ tcell.Screen) {
if !init && len(gomu.queue.items) == 0 {
gomu.playingBar.setDefault()
init = true
}
})
gomu.app.SetRoot(gomu.pages, true).SetFocus(gomu.playlist)
// main loop
if err := gomu.app.Run(); err != nil {
die(err)
}
gomu.hook.RunHooks("exit")
}