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

View File

@@ -9,25 +9,25 @@ var (
)
type Flag struct {
ConfigPath string
Version bool
HideImage bool
RoundedCorners bool
UseIcons bool
ConfigPath string
Version bool
Image string
Corners string
UseIcons bool
}
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,
"Do not display the cover art image.")
flag.BoolVar(&Flags.Version, "version", false,
"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,
"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()
}

View File

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

View File

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

View File

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

View File

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