Skip to content

Commit

Permalink
Add: Config options
Browse files Browse the repository at this point in the history
1. AddMissingOptions - Do not add missing config options (use default)
2. AlternateScreen - Clear Screen or not
  • Loading branch information
Wraient committed Jan 23, 2025
1 parent fafd9ee commit 2304033
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 28 deletions.
8 changes: 4 additions & 4 deletions cmd/curd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ func main() {

discordClientId := "1287457464148820089"

// Setup
internal.ClearScreen()
defer internal.RestoreScreen()

var anime internal.Anime
var user internal.User

Expand All @@ -40,6 +36,10 @@ func main() {
}
internal.SetGlobalConfig(&userCurdConfig)

// Setup
internal.ClearScreen()
defer internal.RestoreScreen()

logFile := filepath.Join(os.ExpandEnv(userCurdConfig.StoragePath), "debug.log")
internal.ClearLogFile(logFile)

Expand Down
57 changes: 33 additions & 24 deletions internal/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,19 @@ type CurdConfig struct {
StoragePath string `config:"StoragePath"`
AnimeNameLanguage string `config:"AnimeNameLanguage"`
PercentageToMarkComplete int `config:"PercentageToMarkComplete"`
NextEpisodePrompt bool `config:"NextEpisodePrompt"`
SkipOp bool `config:"SkipOp"`
SkipEd bool `config:"SkipEd"`
SkipFiller bool `config:"SkipFiller"`
ImagePreview bool `config:"ImagePreview"`
SkipRecap bool `config:"SkipRecap"`
RofiSelection bool `config:"RofiSelection"`
CurrentCategory bool `config:"CurrentCategory"`
ScoreOnCompletion bool `config:"ScoreOnCompletion"`
SaveMpvSpeed bool `config:"SaveMpvSpeed"`
DiscordPresence bool `config:"DiscordPresence"`
NextEpisodePrompt bool `config:"NextEpisodePrompt"`
SkipOp bool `config:"SkipOp"`
SkipEd bool `config:"SkipEd"`
SkipFiller bool `config:"SkipFiller"`
ImagePreview bool `config:"ImagePreview"`
SkipRecap bool `config:"SkipRecap"`
RofiSelection bool `config:"RofiSelection"`
CurrentCategory bool `config:"CurrentCategory"`
ScoreOnCompletion bool `config:"ScoreOnCompletion"`
SaveMpvSpeed bool `config:"SaveMpvSpeed"`
AddMissingOptions bool `config:"AddMissingOptions"`
AlternateScreen bool `config:"AlternateScreen"`
DiscordPresence bool `config:"DiscordPresence"`
}

// Default configuration values as a map
Expand All @@ -42,16 +44,18 @@ func defaultConfigMap() map[string]string {
"SubsLanguage": "english",
"SubOrDub": "sub",
"PercentageToMarkComplete": "85",
"NextEpisodePrompt": "false",
"SkipOp": "true",
"SkipEd": "true",
"SkipFiller": "true",
"SkipRecap": "true",
"RofiSelection": "false",
"ImagePreview": "false",
"ScoreOnCompletion": "true",
"SaveMpvSpeed": "true",
"DiscordPresence": "true",
"NextEpisodePrompt": "false",
"SkipOp": "true",
"SkipEd": "true",
"SkipFiller": "true",
"SkipRecap": "true",
"RofiSelection": "false",
"ImagePreview": "false",
"ScoreOnCompletion": "true",
"SaveMpvSpeed": "true",
"AddMissingOptions": "true",
"AlternateScreen": "true",
"DiscordPresence": "true",
}
}

Expand All @@ -65,7 +69,6 @@ func GetGlobalConfig() *CurdConfig {
return globalConfig
}


// LoadConfig reads or creates the config file, adds missing fields, and returns the populated CurdConfig struct
func LoadConfig(configPath string) (CurdConfig, error) {
configPath = os.ExpandEnv(configPath) // Substitute environment variables like $HOME
Expand All @@ -85,6 +88,12 @@ func LoadConfig(configPath string) (CurdConfig, error) {
return CurdConfig{}, fmt.Errorf("error loading config file: %v", err)
}

// Check AddMissingOptions setting first
addMissing := true
if val, exists := configMap["AddMissingOptions"]; exists {
addMissing, _ = strconv.ParseBool(val)
}

// Add missing fields to the config map
updated := false
defaultConfigMap := defaultConfigMap()
Expand All @@ -95,8 +104,8 @@ func LoadConfig(configPath string) (CurdConfig, error) {
}
}

// Write updated config back to file if there were any missing fields
if updated {
// Write updated config back to file only if AddMissingOptions is true
if addMissing && updated {
if err := saveConfigToFile(configPath, configMap); err != nil {
return CurdConfig{}, fmt.Errorf("error saving updated config file: %v", err)
}
Expand Down
12 changes: 12 additions & 0 deletions internal/curd.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,25 @@ func Log(data interface{}, logFile string) error {

// ClearScreen clears the terminal screen and saves the state
func ClearScreen() {
userCurdConfig := GetGlobalConfig()

if userCurdConfig.AlternateScreen == false {
return
}

fmt.Print("\033[?1049h") // Switch to alternate screen buffer
fmt.Print("\033[2J") // Clear the entire screen
fmt.Print("\033[H") // Move cursor to the top left
}

// RestoreScreen restores the original terminal state
func RestoreScreen() {
userCurdConfig := GetGlobalConfig()

if userCurdConfig.AlternateScreen == false {
return
}

fmt.Print("\033[?1049l") // Switch back to the main screen buffer
}

Expand Down

0 comments on commit 2304033

Please sign in to comment.