add config options

This commit is contained in:
raziman 2020-07-28 11:24:58 +08:00
parent 138f534d4d
commit 7e61859d1f
6 changed files with 58 additions and 17 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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 {

View File

@ -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()
}

View File

@ -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)
}