Change some config and flag options

Earlier the flags couldn't override with false values, that has been
changed. Also change how errors are handled while the config is been
read.
This commit is contained in:
aditya-k2
2024-01-22 11:01:03 +05:30
parent 487336ebaa
commit 3cacc62196
6 changed files with 74 additions and 48 deletions

View File

@@ -5,6 +5,8 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"errors"
"github.com/aditya-K2/utils" "github.com/aditya-K2/utils"
"github.com/fsnotify/fsnotify" "github.com/fsnotify/fsnotify"
"github.com/spf13/viper" "github.com/spf13/viper"
@@ -20,6 +22,13 @@ var (
buildDate = "unknown" buildDate = "unknown"
) )
const (
ImageHidden string = "hidden"
ImageShow string = "show"
CornersRounded string = "rounded"
CornersDefault string = "default"
)
type configS struct { type configS struct {
CacheDir string `yaml:"cache_dir" mapstructure:"cache_dir"` CacheDir string `yaml:"cache_dir" mapstructure:"cache_dir"`
RedrawInterval int `yaml:"redraw_interval" mapstructure:"redraw_interval"` RedrawInterval int `yaml:"redraw_interval" mapstructure:"redraw_interval"`
@@ -29,8 +38,8 @@ type configS struct {
AdditionalPaddingY int `yaml:"additional_padding_y" mapstructure:"additional_padding_y"` AdditionalPaddingY int `yaml:"additional_padding_y" mapstructure:"additional_padding_y"`
ImageWidthExtraX int `yaml:"image_width_extra_x" mapstructure:"image_width_extra_x"` ImageWidthExtraX int `yaml:"image_width_extra_x" mapstructure:"image_width_extra_x"`
ImageWidthExtraY int `yaml:"image_width_extra_y" mapstructure:"image_width_extra_y"` ImageWidthExtraY int `yaml:"image_width_extra_y" mapstructure:"image_width_extra_y"`
HideImage bool `yaml:"hide_image" mapstructure:"hide_image"` Image string `yaml:"image" mapstructure:"image"`
RoundedCorners bool `yaml:"rounded_corners" mapstructure:"rounded_corners"` Corners string `yaml:"corners" mapstructure:"corners"`
UseIcons bool `yaml:"use_icons" mapstructure:"use_icons"` UseIcons bool `yaml:"use_icons" mapstructure:"use_icons"`
} }
@@ -40,10 +49,12 @@ func newConfigS() *configS {
RedrawInterval: 500, RedrawInterval: 500,
Colors: NewColors(), Colors: NewColors(),
Icons: NewIcons(), Icons: NewIcons(),
Corners: CornersDefault,
Image: ImageShow,
} }
} }
func ReadConfig() { func ReadConfig() error {
parseFlags() parseFlags()
if Flags.Version { if Flags.Version {
@@ -58,12 +69,12 @@ func ReadConfig() {
if configErr != nil { if configErr != nil {
utils.Print("RED", "Couldn't get $XDG_CONFIG!") utils.Print("RED", "Couldn't get $XDG_CONFIG!")
panic(configErr) return configErr
} }
if cacheErr != nil { if cacheErr != nil {
utils.Print("RED", "Couldn't get CACHE DIR!") utils.Print("RED", "Couldn't get CACHE DIR!")
panic(cacheErr) return cacheErr
} }
viper.SetConfigName("config") viper.SetConfigName("config")
@@ -80,19 +91,29 @@ func ReadConfig() {
Config.CacheDir = utils.ExpandHomeDir(Config.CacheDir) Config.CacheDir = utils.ExpandHomeDir(Config.CacheDir)
} }
// Flags have precedence over config values if Flags.Image != "" {
useFlags := func() { if Flags.Image == ImageHidden || Flags.Image == ImageShow {
if Flags.HideImage != false { fmt.Println(Flags.Image)
Config.HideImage = Flags.HideImage fmt.Println("Here")
} Config.Image = Flags.Image
if Flags.RoundedCorners != false { } else {
Config.RoundedCorners = Flags.RoundedCorners return errors.New(fmt.Sprintf("Undefined value provided to --image flag: '%s' ( accepted: %s | %s )", Flags.Image, ImageHidden, ImageShow))
}
if Flags.UseIcons != false {
Config.UseIcons = Flags.UseIcons
} }
fmt.Println(Config.Image)
}
if Flags.Corners != "" {
if Flags.Corners == CornersRounded || Flags.Corners == CornersDefault {
Config.Corners = Flags.Corners
} else {
return errors.New(fmt.Sprintf("Undefined value provided to --corners flag: '%s' ( accepted: %s | %s )", Flags.Corners, CornersRounded, CornersDefault))
}
Config.Corners = Flags.Corners
}
if Flags.UseIcons != false {
Config.UseIcons = Flags.UseIcons
} }
useFlags()
viper.OnConfigChange(func(e fsnotify.Event) { viper.OnConfigChange(func(e fsnotify.Event) {
viper.Unmarshal(Config) viper.Unmarshal(Config)
@@ -104,6 +125,7 @@ func ReadConfig() {
viper.WatchConfig() viper.WatchConfig()
expandHome() expandHome()
return nil
} }
func GenerateMappings() map[string]map[string]map[Key]string { func GenerateMappings() map[string]map[string]map[Key]string {

View File

@@ -9,25 +9,25 @@ var (
) )
type Flag struct { type Flag struct {
ConfigPath string ConfigPath string
Version bool Version bool
HideImage bool Image string
RoundedCorners bool Corners string
UseIcons bool UseIcons bool
} }
func parseFlags() { func parseFlags() {
flag.StringVar(&Flags.ConfigPath, "c", userConfigPath,
"Specify The Directory to check for config.yml file.")
flag.BoolVar(&Flags.HideImage, "hide-image", Config.HideImage,
"Do not display the cover art image.")
flag.BoolVar(&Flags.Version, "v", false, flag.BoolVar(&Flags.Version, "v", false,
"Do not display the cover art image.") "Do not display the cover art image.")
flag.BoolVar(&Flags.Version, "version", false, flag.BoolVar(&Flags.Version, "version", false,
"Do not display the cover art image.") "Do not display the cover art image.")
flag.BoolVar(&Flags.RoundedCorners, "rounded-corners", Config.RoundedCorners,
"Enable Rounded Corners")
flag.BoolVar(&Flags.UseIcons, "icons", Config.UseIcons, flag.BoolVar(&Flags.UseIcons, "icons", Config.UseIcons,
"Use Icons") "Use Icons")
flag.StringVar(&Flags.ConfigPath, "c", userConfigPath,
"Specify The Directory to check for config.yml file.")
flag.StringVar(&Flags.Image, "image", "",
"Show or Hide Image ( 'show' | 'hidden' )")
flag.StringVar(&Flags.Corners, "corners", "",
"Enable or disable Rounded Corners ( 'rounded' | 'default' )")
flag.Parse() flag.Parse()
} }

View File

@@ -7,7 +7,9 @@ import (
) )
func main() { func main() {
config.ReadConfig() if err := config.ReadConfig(); err != nil {
panic(err)
}
if err := spt.InitClient(); err != nil { if err := spt.InitClient(); err != nil {
panic(err) panic(err)
} }

View File

@@ -44,7 +44,7 @@ func onConfigChange() {
setStyles() setStyles()
setIcons() setIcons()
setBorderRunes() setBorderRunes()
if coverArt != nil && !cfg.HideImage { if coverArt != nil && cfg.Image == config.ImageShow {
coverArt.RefreshState() coverArt.RefreshState()
} }
} }
@@ -272,7 +272,7 @@ func NewApplication() *tview.Application {
}, coverArt), }, coverArt),
} }
if !cfg.HideImage { if cfg.Image == config.ImageShow {
globalActions = utils.MergeMaps(globalActions, imageActions) globalActions = utils.MergeMaps(globalActions, imageActions)
} }
@@ -415,7 +415,7 @@ func NewApplication() *tview.Application {
AddItem(navMenu, 6, 3, false). AddItem(navMenu, 6, 3, false).
AddItem(playlistNav, 0, 6, false) AddItem(playlistNav, 0, 6, false)
if !cfg.HideImage { if cfg.Image == config.ImageShow {
navFlex.AddItem(coverArt, 9, 3, false) navFlex.AddItem(coverArt, 9, 3, false)
} }
@@ -438,7 +438,7 @@ func NewApplication() *tview.Application {
// Start Routines // Start Routines
InitNotifier() InitNotifier()
if !cfg.HideImage { if cfg.Image == config.ImageShow {
go rectWatcher() go rectWatcher()
} else { } else {
// Start Progress Routine directly // Start Progress Routine directly

View File

@@ -8,6 +8,7 @@ import (
"github.com/gdamore/tcell/v2" "github.com/gdamore/tcell/v2"
"github.com/zmb3/spotify/v2" "github.com/zmb3/spotify/v2"
"github.com/aditya-K2/gspt/config"
"github.com/aditya-K2/gspt/spt" "github.com/aditya-K2/gspt/spt"
"github.com/aditya-K2/tview" "github.com/aditya-K2/tview"
"github.com/aditya-K2/utils" "github.com/aditya-K2/utils"
@@ -104,7 +105,7 @@ func RefreshProgress(force bool) {
if state.Item != nil { if state.Item != nil {
ctrackId = state.Item.ID ctrackId = state.Item.ID
} }
if !cfg.HideImage { if cfg.Image == config.ImageShow {
coverArt.RefreshState() coverArt.RefreshState()
} }
} }

View File

@@ -1,12 +1,13 @@
package ui package ui
import ( import (
"github.com/aditya-K2/gspt/config"
"github.com/aditya-K2/tview" "github.com/aditya-K2/tview"
) )
var ( var (
borders = map[bool]map[string]rune{ borders = map[string]map[string]rune{
true: { config.CornersRounded: {
"TopLeft": '╭', "TopLeft": '╭',
"TopRight": '╮', "TopRight": '╮',
"BottomRight": '╯', "BottomRight": '╯',
@@ -20,7 +21,7 @@ var (
"VerticalFocus": '│', "VerticalFocus": '│',
"HorizontalFocus": '─', "HorizontalFocus": '─',
}, },
false: { config.CornersDefault: {
"TopLeft": tview.Borders.TopLeft, "TopLeft": tview.Borders.TopLeft,
"TopRight": tview.Borders.TopRight, "TopRight": tview.Borders.TopRight,
"BottomRight": tview.Borders.BottomRight, "BottomRight": tview.Borders.BottomRight,
@@ -38,18 +39,18 @@ var (
) )
func setBorderRunes() { func setBorderRunes() {
tview.Borders.TopLeft = borders[cfg.RoundedCorners]["TopLeft"] tview.Borders.TopLeft = borders[cfg.Corners]["TopLeft"]
tview.Borders.TopRight = borders[cfg.RoundedCorners]["TopRight"] tview.Borders.TopRight = borders[cfg.Corners]["TopRight"]
tview.Borders.BottomRight = borders[cfg.RoundedCorners]["BottomRight"] tview.Borders.BottomRight = borders[cfg.Corners]["BottomRight"]
tview.Borders.BottomLeft = borders[cfg.RoundedCorners]["BottomLeft"] tview.Borders.BottomLeft = borders[cfg.Corners]["BottomLeft"]
tview.Borders.Vertical = borders[cfg.RoundedCorners]["Vertical"] tview.Borders.Vertical = borders[cfg.Corners]["Vertical"]
tview.Borders.Horizontal = borders[cfg.RoundedCorners]["Horizontal"] tview.Borders.Horizontal = borders[cfg.Corners]["Horizontal"]
tview.Borders.TopLeftFocus = borders[cfg.RoundedCorners]["TopLeftFocus"] tview.Borders.TopLeftFocus = borders[cfg.Corners]["TopLeftFocus"]
tview.Borders.TopRightFocus = borders[cfg.RoundedCorners]["TopRightFocus"] tview.Borders.TopRightFocus = borders[cfg.Corners]["TopRightFocus"]
tview.Borders.BottomRightFocus = borders[cfg.RoundedCorners]["BottomRightFocus"] tview.Borders.BottomRightFocus = borders[cfg.Corners]["BottomRightFocus"]
tview.Borders.BottomLeftFocus = borders[cfg.RoundedCorners]["BottomLeftFocus"] tview.Borders.BottomLeftFocus = borders[cfg.Corners]["BottomLeftFocus"]
tview.Borders.VerticalFocus = borders[cfg.RoundedCorners]["VerticalFocus"] tview.Borders.VerticalFocus = borders[cfg.Corners]["VerticalFocus"]
tview.Borders.HorizontalFocus = borders[cfg.RoundedCorners]["HorizontalFocus"] tview.Borders.HorizontalFocus = borders[cfg.Corners]["HorizontalFocus"]
} }
func setStyles() { func setStyles() {