From 482b8f247fdf340fc4dead2936e6e84c729b07bf Mon Sep 17 00:00:00 2001 From: raziman Date: Sat, 13 Feb 2021 21:00:50 +0800 Subject: [PATCH] add basic support for scripting language --- command.go | 6 ++++ playlist.go | 2 +- popup.go | 39 ++++++++++++++++++++ start.go | 101 +++++++++++++++++++++++++++++++++++++++++++++++++--- utils.go | 2 +- 5 files changed, 143 insertions(+), 7 deletions(-) diff --git a/command.go b/command.go index d467903..7c62c5a 100644 --- a/command.go +++ b/command.go @@ -466,4 +466,10 @@ func (c Command) defineCommands() { } }) + for name, cmd := range c.commands { + err := gomu.env.DefineGlobal(name, cmd) + if err != nil { + log.Panicln(err) + } + } } diff --git a/playlist.go b/playlist.go index 2132215..9c46dd6 100644 --- a/playlist.go +++ b/playlist.go @@ -179,7 +179,7 @@ func newPlaylist(args Args) *Playlist { } fn, err := gomu.command.getFn(cmd) if err != nil { - logError(err) + errorPopup(err) return e } fn() diff --git a/popup.go b/popup.go index 2fc870d..6c181da 100644 --- a/popup.go +++ b/popup.go @@ -13,6 +13,7 @@ import ( "github.com/gdamore/tcell/v2" "github.com/rivo/tview" "github.com/sahilm/fuzzy" + "github.com/ztrue/tracerr" ) // this is used to make the popup unique @@ -527,3 +528,41 @@ func renamePopup(node *AudioFile) { return e }) } + +func errorPopup(message error) { + defaultTimedPopup(" Error ", tracerr.Unwrap(message).Error()) + logError(message) +} + +func debugPopup(message string) { + defaultTimedPopup(" Debug ", message) + log.Println(message) +} + +func inputPopup(prompt string) string { + + popupID := "general-input-popup" + input := newInputPopup(popupID, "", prompt + ": ", "") + result := make(chan string) + input.SetInputCapture(func(e *tcell.EventKey) *tcell.EventKey { + + switch e.Key() { + case tcell.KeyEnter: + newName := input.GetText() + if newName == "" { + return e + } + result <-newName + gomu.pages.RemovePage(popupID) + gomu.popups.pop() + + case tcell.KeyEsc: + gomu.pages.RemovePage(popupID) + gomu.popups.pop() + } + + return e + }) + + return <-result +} diff --git a/start.go b/start.go index 80ae26a..dc4b2ba 100644 --- a/start.go +++ b/start.go @@ -3,12 +3,14 @@ package main import ( + "context" "errors" "flag" "fmt" "io/ioutil" "os" "os/signal" + "reflect" "strings" "syscall" @@ -30,21 +32,78 @@ type Panel interface { } const ( - configPath = ".config/gomu/config" + configPath = "~/.config/gomu/config" musicPath = "~/music" ) func execConfig() error { - gomu.env.Define("echo", func(text string) { - defaultTimedPopup(" Debug ", text) - }) + const defaultConfig = ` - content, err := ioutil.ReadFile("/home/terra/.config/gomu/config.anko") +// 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" +// 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" + +// default emoji here is using awesome-terminal-fonts +// you can change these to your liking +emoji_playlist = "" +emoji_file = "" +emoji_loop = "ﯩ" +emoji_noloop = "" + +// not all colors can be reproducible in terminal +// changing hex colors may or may not produce expected result +color_accent = "#008B8B" +color_background = "none" +color_foreground = "#FFFFFF" +color_now_playing_title = "#017702" +color_playlist = "#008B8B" +color_popup = "#0A0F14" + +// vim: syntax=go +` + + gomu.env.DefineGlobal("debug_popup", debugPopup) + gomu.env.DefineGlobal("input_popup", inputPopup) + + cfg := expandTilde(configPath) + + _, 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 = vm.Execute(gomu.env, nil, defaultConfig) + if err != nil { + return tracerr.Wrap(err) + } + + // execute user config _, err = vm.Execute(gomu.env, nil, string(content)) if err != nil { return tracerr.Wrap(err) @@ -178,6 +237,38 @@ func start(application *tview.Application, args Args) { gomu.cyclePanels2() } + // check for user defined keybindings + kb, err := gomu.env.Get("keybinds") + if err == nil { + keybinds, ok := kb.(map[interface{}]interface{}) + if !ok { + errorPopup(errors.New("invalid type; require {}")) + return e + } + + cmd, ok := keybinds[string(e.Rune())] + if ok { + + f, ok := cmd.(func(context.Context) (reflect.Value, reflect.Value)) + if !ok { + errorPopup(errors.New("invalid type; require type func()")) + return e + } + + go func() { + _, execErr := f(context.Background()) + if err := execErr.Interface(); !execErr.IsNil() { + if err, ok := err.(error); ok { + errorPopup(err) + } + } + }() + + return e + } + } + + cmds := map[rune]string{ 'q': "quit", ' ': "toggle_pause", diff --git a/utils.go b/utils.go index 258f827..7d6a466 100644 --- a/utils.go +++ b/utils.go @@ -192,7 +192,7 @@ func appendFile(path string, content string) error { // create the neccessary parent directory err = os.MkdirAll(filepath.Dir(expandFilePath(path)), os.ModePerm) if err != nil { - return err + return tracerr.Wrap(err) } } defer f.Close()