| @@ -14,35 +14,37 @@ import ( |
| 14 | 14 | |
| 15 | 15 | // CurdConfig struct with field names that match the config keys |
| 16 | 16 | type CurdConfig struct { |
| 17 | | Player string `config:"Player"` |
| 18 | | SubsLanguage string `config:"SubsLanguage"` |
| 19 | | SubOrDub string `config:"SubOrDub"` |
| 20 | | StoragePath string `config:"StoragePath"` |
| 21 | | AnimeNameLanguage string `config:"AnimeNameLanguage"` |
| 22 | | PercentageToMarkComplete int `config:"PercentageToMarkComplete"` |
| 23 | | NextEpisodePrompt bool `config:"NextEpisodePrompt"` |
| 24 | | SkipOp bool `config:"SkipOp"` |
| 25 | | SkipEd bool `config:"SkipEd"` |
| 26 | | SkipFiller bool `config:"SkipFiller"` |
| 27 | | ImagePreview bool `config:"ImagePreview"` |
| 28 | | SkipRecap bool `config:"SkipRecap"` |
| 29 | | RofiSelection bool `config:"RofiSelection"` |
| 30 | | CurrentCategory bool `config:"CurrentCategory"` |
| 31 | | ScoreOnCompletion bool `config:"ScoreOnCompletion"` |
| 32 | | SaveMpvSpeed bool `config:"SaveMpvSpeed"` |
| 33 | | AddMissingOptions bool `config:"AddMissingOptions"` |
| 34 | | AlternateScreen bool `config:"AlternateScreen"` |
| 35 | | DiscordPresence bool `config:"DiscordPresence"` |
| 17 | Player string `config:"Player"` |
| 18 | MpvArgs []string `config:MpvArgs` |
| 19 | SubsLanguage string `config:"SubsLanguage"` |
| 20 | SubOrDub string `config:"SubOrDub"` |
| 21 | StoragePath string `config:"StoragePath"` |
| 22 | AnimeNameLanguage string `config:"AnimeNameLanguage"` |
| 23 | PercentageToMarkComplete int `config:"PercentageToMarkComplete"` |
| 24 | NextEpisodePrompt bool `config:"NextEpisodePrompt"` |
| 25 | SkipOp bool `config:"SkipOp"` |
| 26 | SkipEd bool `config:"SkipEd"` |
| 27 | SkipFiller bool `config:"SkipFiller"` |
| 28 | ImagePreview bool `config:"ImagePreview"` |
| 29 | SkipRecap bool `config:"SkipRecap"` |
| 30 | RofiSelection bool `config:"RofiSelection"` |
| 31 | CurrentCategory bool `config:"CurrentCategory"` |
| 32 | ScoreOnCompletion bool `config:"ScoreOnCompletion"` |
| 33 | SaveMpvSpeed bool `config:"SaveMpvSpeed"` |
| 34 | AddMissingOptions bool `config:"AddMissingOptions"` |
| 35 | AlternateScreen bool `config:"AlternateScreen"` |
| 36 | DiscordPresence bool `config:"DiscordPresence"` |
| 36 | 37 | } |
| 37 | 38 | |
| 38 | 39 | // Default configuration values as a map |
| 39 | 40 | func defaultConfigMap() map[string]string { |
| 40 | 41 | return map[string]string{ |
| 41 | | "Player": "mpv", |
| 42 | | "StoragePath": "$HOME/.local/share/curd", |
| 43 | | "AnimeNameLanguage": "english", |
| 44 | | "SubsLanguage": "english", |
| 45 | | "SubOrDub": "sub", |
| 42 | "Player": "mpv", |
| 43 | "MpvArgs": "[]", |
| 44 | "StoragePath": "$HOME/.local/share/curd", |
| 45 | "AnimeNameLanguage": "english", |
| 46 | "SubsLanguage": "english", |
| 47 | "SubOrDub": "sub", |
| 46 | 48 | "PercentageToMarkComplete": "85", |
| 47 | 49 | "NextEpisodePrompt": "false", |
| 48 | 50 | "SkipOp": "true", |
| @@ -69,6 +71,29 @@ func GetGlobalConfig() *CurdConfig { |
| 69 | 71 | return globalConfig |
| 70 | 72 | } |
| 71 | 73 | |
| 74 | // Helper function to parse string array from config |
| 75 | func parseStringArray(value string) []string { |
| 76 | // Remove brackets and split by comma |
| 77 | value = strings.TrimPrefix(value, "[") |
| 78 | value = strings.TrimSuffix(value, "]") |
| 79 | if value == "" { |
| 80 | return nil |
| 81 | } |
| 82 | |
| 83 | // Split by comma and trim spaces and quotes from each element |
| 84 | parts := strings.Split(value, ",") |
| 85 | result := make([]string, 0, len(parts)) |
| 86 | for _, part := range parts { |
| 87 | // Trim spaces and quotes |
| 88 | part = strings.TrimSpace(part) |
| 89 | part = strings.Trim(part, "\"") |
| 90 | if part != "" { |
| 91 | result = append(result, part) |
| 92 | } |
| 93 | } |
| 94 | return result |
| 95 | } |
| 96 | |
| 72 | 97 | // LoadConfig reads or creates the config file, adds missing fields, and returns the populated CurdConfig struct |
| 73 | 98 | func LoadConfig(configPath string) (CurdConfig, error) { |
| 74 | 99 | configPath = os.ExpandEnv(configPath) // Substitute environment variables like $HOME |
| @@ -111,6 +136,11 @@ func LoadConfig(configPath string) (CurdConfig, error) { |
| 111 | 136 | } |
| 112 | 137 | } |
| 113 | 138 | |
| 139 | // Parse string arrays |
| 140 | if mpvArgs, exists := configMap["MpvArgs"]; exists { |
| 141 | configMap["MpvArgs"] = mpvArgs |
| 142 | } |
| 143 | |
| 114 | 144 | // Populate the CurdConfig struct from the config map |
| 115 | 145 | config := populateConfig(configMap) |
| 116 | 146 | |
| @@ -279,5 +309,10 @@ func populateConfig(configMap map[string]string) CurdConfig { |
| 279 | 309 | } |
| 280 | 310 | } |
| 281 | 311 | |
| 312 | // Handle MpvArgs specially |
| 313 | if mpvArgs, exists := configMap["MpvArgs"]; exists { |
| 314 | config.MpvArgs = parseStringArray(mpvArgs) |
| 315 | } |
| 316 | |
| 282 | 317 | return config |
| 283 | 318 | } |