From 7e61859d1f75c4236817c960878275c720c18d27 Mon Sep 17 00:00:00 2001 From: raziman Date: Tue, 28 Jul 2020 11:24:58 +0800 Subject: [PATCH] add config options --- gomu.go | 7 ++++--- playingbar.go | 4 +++- playlist.go | 6 +++--- popup.go | 2 +- start.go | 50 ++++++++++++++++++++++++++++++++++++++++++-------- utils.go | 6 +++++- 6 files changed, 58 insertions(+), 17 deletions(-) diff --git a/gomu.go b/gomu.go index 49d3e23..fbbada4 100644 --- a/gomu.go +++ b/gomu.go @@ -5,6 +5,7 @@ import ( "github.com/gdamore/tcell" "github.com/rivo/tview" + "github.com/spf13/viper" ) const VERSION = "v1.4.0" @@ -32,9 +33,9 @@ type Gomu struct { func newGomu() *Gomu { gomu := &Gomu{ - popupBg: tcell.GetColor("#0A0F14"), - textColor: tcell.ColorWhite, - accentColor: tcell.ColorDarkCyan, + popupBg: tcell.GetColor(viper.GetString("color.popup")), + textColor: tcell.GetColor(viper.GetString("color.foreground")), + accentColor: tcell.GetColor(viper.GetString("color.accent")), } return gomu diff --git a/playingbar.go b/playingbar.go index c119706..17a45d1 100644 --- a/playingbar.go +++ b/playingbar.go @@ -10,6 +10,7 @@ import ( "github.com/gdamore/tcell" "github.com/rivo/tview" + "github.com/spf13/viper" "github.com/ztrue/tracerr" ) @@ -118,7 +119,8 @@ func (p *PlayingBar) run() error { // Updates song title func (p *PlayingBar) setSongTitle(title string) { p.Clear() - p.AddText(title, true, tview.AlignCenter, tcell.ColorGreen) + titleColor := viper.GetString("color.now_playing_title") + p.AddText(title, true, tview.AlignCenter, tcell.GetColor(titleColor)) } // Resets progress bar, ready for execution diff --git a/playlist.go b/playlist.go index 2546dd7..ca10c4b 100644 --- a/playlist.go +++ b/playlist.go @@ -63,7 +63,7 @@ func (p *Playlist) help() []string { // on root music directory. func newPlaylist() *Playlist { - rootDir, err := filepath.Abs(expandTilde(viper.GetString("music_dir"))) + rootDir, err := filepath.Abs(expandTilde(viper.GetString("general.music_dir"))) if err != nil { logError(err) @@ -190,7 +190,7 @@ func newPlaylist() *Playlist { currNode.Collapse() case 'L': - if !viper.GetBool("confirm_bulk_add") { + if !viper.GetBool("general.confirm_bulk_add") { playlist.addAllToQueue(playlist.GetCurrentNode()) return e } @@ -611,7 +611,7 @@ func ytdl(url string, selPlaylist *tview.TreeNode) error { return tracerr.Wrap(err) } - dir := viper.GetString("music_dir") + dir := viper.GetString("general.music_dir") selAudioFile := selPlaylist.GetReference().(*AudioFile) selPlaylistName := selAudioFile.name diff --git a/popup.go b/popup.go index 2542ab9..9d72e89 100644 --- a/popup.go +++ b/popup.go @@ -63,7 +63,7 @@ func (s *Stack) pop() tview.Primitive { // Gets popup timeout from config file func getPopupTimeout() time.Duration { - dur := viper.GetString("popup_timeout") + dur := viper.GetString("general.popup_timeout") m, err := time.ParseDuration(dur) if err != nil { diff --git a/start.go b/start.go index 9aa337c..513e8a5 100644 --- a/start.go +++ b/start.go @@ -46,13 +46,29 @@ func readConfig(args Args) { viper.AddConfigPath("$HOME/.gomu") viper.AddConfigPath("$HOME/.config/gomu") + colors := map[string]string{ + "color.foreground": "#FFFFFF", + "color.background": "none", + "color.accent": "#008B8B", + "color.popup": "#0A0F14", + "color.now_playing_title": "#017702", + "color.playlist": "#008B8B", + } + if err := viper.ReadInConfig(); err != nil { - viper.SetDefault("music_dir", musicDir) - viper.SetDefault("confirm_on_exit", true) - viper.SetDefault("confirm_bulk_add", true) - viper.SetDefault("popup_timeout", "5s") - viper.SetDefault("volume", "50") + // General config + viper.SetDefault("general.music_dir", musicDir) + viper.SetDefault("general.confirm_on_exit", true) + viper.SetDefault("general.confirm_bulk_add", true) + viper.SetDefault("general.popup_timeout", "5s") + viper.SetDefault("general.volume", 100) + viper.SetDefault("general.load_prev_queue", true) + + // Colors + for k, v := range colors { + viper.SetDefault(k, v) + } // creates gomu config dir if does not exist if _, err := os.Stat(defaultPath); err != nil { @@ -70,6 +86,16 @@ func readConfig(args Args) { } + // Validate hex color + for k, v := range colors { + cfgColor := viper.GetString(k) + if validateHexColor(cfgColor) { + continue + } + // use default value if invalid hex color was given + viper.Set(k, v) + } + } type Args struct { @@ -118,8 +144,16 @@ func start(application *tview.Application, args Args) { tview.Borders.TopRightFocus = tview.Borders.TopRight tview.Borders.BottomLeftFocus = tview.Borders.BottomLeft tview.Borders.BottomRightFocus = tview.Borders.BottomRight - tview.Styles.PrimitiveBackgroundColor = tcell.ColorDefault - tview.Styles.BorderColor = tcell.ColorWhite + + var bgColor tcell.Color + bg := viper.GetString("color.background") + if bg == "none" { + bgColor = tcell.ColorDefault + } else { + bgColor = tcell.GetColor(bg) + } + + tview.Styles.PrimitiveBackgroundColor = bgColor // Assigning to global variable gomu gomu = newGomu() @@ -165,7 +199,7 @@ func start(application *tview.Application, args Args) { switch event.Rune() { case 'q': - if !viper.GetBool("confirm_on_exit") { + if !viper.GetBool("general.confirm_on_exit") { application.Stop() } diff --git a/utils.go b/utils.go index 1fd4b11..91db827 100644 --- a/utils.go +++ b/utils.go @@ -185,5 +185,9 @@ func padHex(r, g, b int32) string { } return result.String() - +} + +func validateHexColor(color string) bool { + reg := regexp.MustCompile(`^#([A-Fa-f0-9]{6})$`) + return reg.MatchString(color) }