| @@ -4,11 +4,11 @@ import ( |
| 4 | 4 | "bufio" |
| 5 | 5 | "fmt" |
| 6 | 6 | "os" |
| 7 | "os/exec" |
| 7 | 8 | "path/filepath" |
| 8 | 9 | "reflect" |
| 9 | | "strconv" |
| 10 | | "os/exec" |
| 11 | 10 | "runtime" |
| 11 | "strconv" |
| 12 | 12 | "strings" |
| 13 | 13 | ) |
| 14 | 14 | |
| @@ -20,6 +20,7 @@ type CurdConfig struct { |
| 20 | 20 | SubOrDub string `config:"SubOrDub"` |
| 21 | 21 | StoragePath string `config:"StoragePath"` |
| 22 | 22 | AnimeNameLanguage string `config:"AnimeNameLanguage"` |
| 23 | MenuOrder string `config:"MenuOrder"` |
| 23 | 24 | PercentageToMarkComplete int `config:"PercentageToMarkComplete"` |
| 24 | 25 | NextEpisodePrompt bool `config:"NextEpisodePrompt"` |
| 25 | 26 | SkipOp bool `config:"SkipOp"` |
| @@ -44,6 +45,7 @@ func defaultConfigMap() map[string]string { |
| 44 | 45 | "StoragePath": "$HOME/.local/share/curd", |
| 45 | 46 | "AnimeNameLanguage": "english", |
| 46 | 47 | "SubsLanguage": "english", |
| 48 | "MenuOrder": "CURRENT,ALL,UNTRACKED,UPDATE,CONTINUE_LAST", |
| 47 | 49 | "SubOrDub": "sub", |
| 48 | 50 | "PercentageToMarkComplete": "85", |
| 49 | 51 | "NextEpisodePrompt": "false", |
| @@ -178,73 +180,73 @@ func createDefaultConfig(path string) error { |
| 178 | 180 | } |
| 179 | 181 | |
| 180 | 182 | func getTokenFromTempFile(isWindowsPlatform bool) (string, error) { |
| 181 | | // Create a temporary file for the token |
| 182 | | tempFile, err := os.CreateTemp("", "curd-token-*.txt") |
| 183 | | if err != nil { |
| 184 | | return "", fmt.Errorf("error creating temp file: %v", err) |
| 185 | | } |
| 186 | | tempPath := tempFile.Name() |
| 187 | | tempFile.Close() |
| 188 | | |
| 189 | | // Write instructions to the temp file |
| 190 | | instructions := "Please generate a token from https://anilist.co/api/v2/oauth/authorize?client_id=20686&response_type=token\n" + |
| 191 | | "Replace this text with your token and save the file.\n" |
| 192 | | if err := os.WriteFile(tempPath, []byte(instructions), 0644); err != nil { |
| 193 | | return "", fmt.Errorf("error writing instructions: %v", err) |
| 194 | | } |
| 195 | | |
| 196 | | // Open the file with appropriate editor |
| 197 | | var cmd *exec.Cmd |
| 198 | | if isWindowsPlatform { |
| 199 | | cmd = exec.Command("notepad.exe", tempPath) |
| 200 | | } else { |
| 201 | | editor := os.Getenv("EDITOR") |
| 202 | | if editor == "" { |
| 203 | | editor = "nano" // Default to nano if $EDITOR is not set |
| 204 | | } |
| 205 | | cmd = exec.Command(editor, tempPath) |
| 206 | | cmd.Stdin = os.Stdin |
| 207 | | cmd.Stdout = os.Stdout |
| 208 | | cmd.Stderr = os.Stderr |
| 209 | | } |
| 210 | | |
| 211 | | if err := cmd.Run(); err != nil { |
| 212 | | return "", fmt.Errorf("error opening editor: %v", err) |
| 213 | | } |
| 214 | | |
| 215 | | // Read the token from the file |
| 216 | | content, err := os.ReadFile(tempPath) |
| 217 | | if err != nil { |
| 218 | | return "", fmt.Errorf("error reading token: %v", err) |
| 219 | | } |
| 220 | | |
| 221 | | // Clean up the temp file |
| 222 | | os.Remove(tempPath) |
| 223 | | |
| 224 | | // Extract token (remove instructions and whitespace) |
| 225 | | return strings.TrimSpace(string(content)), nil |
| 183 | // Create a temporary file for the token |
| 184 | tempFile, err := os.CreateTemp("", "curd-token-*.txt") |
| 185 | if err != nil { |
| 186 | return "", fmt.Errorf("error creating temp file: %v", err) |
| 187 | } |
| 188 | tempPath := tempFile.Name() |
| 189 | tempFile.Close() |
| 190 | |
| 191 | // Write instructions to the temp file |
| 192 | instructions := "Please generate a token from https://anilist.co/api/v2/oauth/authorize?client_id=20686&response_type=token\n" + |
| 193 | "Replace this text with your token and save the file.\n" |
| 194 | if err := os.WriteFile(tempPath, []byte(instructions), 0644); err != nil { |
| 195 | return "", fmt.Errorf("error writing instructions: %v", err) |
| 196 | } |
| 197 | |
| 198 | // Open the file with appropriate editor |
| 199 | var cmd *exec.Cmd |
| 200 | if isWindowsPlatform { |
| 201 | cmd = exec.Command("notepad.exe", tempPath) |
| 202 | } else { |
| 203 | editor := os.Getenv("EDITOR") |
| 204 | if editor == "" { |
| 205 | editor = "nano" // Default to nano if $EDITOR is not set |
| 206 | } |
| 207 | cmd = exec.Command(editor, tempPath) |
| 208 | cmd.Stdin = os.Stdin |
| 209 | cmd.Stdout = os.Stdout |
| 210 | cmd.Stderr = os.Stderr |
| 211 | } |
| 212 | |
| 213 | if err := cmd.Run(); err != nil { |
| 214 | return "", fmt.Errorf("error opening editor: %v", err) |
| 215 | } |
| 216 | |
| 217 | // Read the token from the file |
| 218 | content, err := os.ReadFile(tempPath) |
| 219 | if err != nil { |
| 220 | return "", fmt.Errorf("error reading token: %v", err) |
| 221 | } |
| 222 | |
| 223 | // Clean up the temp file |
| 224 | os.Remove(tempPath) |
| 225 | |
| 226 | // Extract token (remove instructions and whitespace) |
| 227 | return strings.TrimSpace(string(content)), nil |
| 226 | 228 | } |
| 227 | 229 | |
| 228 | 230 | func ChangeToken(config *CurdConfig, user *User) { |
| 229 | | var err error |
| 230 | | tokenPath := filepath.Join(os.ExpandEnv(config.StoragePath), "token") |
| 231 | | |
| 232 | | switch { |
| 233 | | case runtime.GOOS == "darwin" || runtime.GOOS == "windows": |
| 234 | | user.Token, err = getTokenFromTempFile(runtime.GOOS == "windows") |
| 235 | | case config.RofiSelection: |
| 236 | | user.Token, err = GetTokenFromRofi() |
| 237 | | default: |
| 238 | | fmt.Println("Please generate a token from https://anilist.co/api/v2/oauth/authorize?client_id=20686&response_type=token") |
| 239 | | fmt.Scanln(&user.Token) |
| 240 | | } |
| 241 | | |
| 242 | | if err != nil { |
| 243 | | Log("Error getting user input: "+err.Error()) |
| 244 | | ExitCurd(err) |
| 245 | | } |
| 246 | | |
| 247 | | WriteTokenToFile(user.Token, tokenPath) |
| 231 | var err error |
| 232 | tokenPath := filepath.Join(os.ExpandEnv(config.StoragePath), "token") |
| 233 | |
| 234 | switch { |
| 235 | case runtime.GOOS == "darwin" || runtime.GOOS == "windows": |
| 236 | user.Token, err = getTokenFromTempFile(runtime.GOOS == "windows") |
| 237 | case config.RofiSelection: |
| 238 | user.Token, err = GetTokenFromRofi() |
| 239 | default: |
| 240 | fmt.Println("Please generate a token from https://anilist.co/api/v2/oauth/authorize?client_id=20686&response_type=token") |
| 241 | fmt.Scanln(&user.Token) |
| 242 | } |
| 243 | |
| 244 | if err != nil { |
| 245 | Log("Error getting user input: " + err.Error()) |
| 246 | ExitCurd(err) |
| 247 | } |
| 248 | |
| 249 | WriteTokenToFile(user.Token, tokenPath) |
| 248 | 250 | } |
| 249 | 251 | |
| 250 | 252 | // Load config file from disk into a map (key=value format) |
| @@ -331,3 +333,50 @@ func populateConfig(configMap map[string]string) CurdConfig { |
| 331 | 333 | |
| 332 | 334 | return config |
| 333 | 335 | } |
| 336 | |
| 337 | func getOrderedCategories(userCurdConfig *CurdConfig) map[string]string { |
| 338 | // Define the default categories and their labels |
| 339 | defaultOrder := []string{"CURRENT", "ALL", "UNTRACKED", "UPDATE", "CONTINUE_LAST"} |
| 340 | defaultLabels := map[string]string{ |
| 341 | "CURRENT": "Currently Watching", |
| 342 | "ALL": "Show All", |
| 343 | "UNTRACKED": "Untracked Watching", |
| 344 | "UPDATE": "Update (Episode, Status, Score)", |
| 345 | "CONTINUE_LAST": "Continue Last Session", |
| 346 | } |
| 347 | |
| 348 | // Create ordered map to store final result |
| 349 | finalOrder := make([]string, 0) |
| 350 | seen := make(map[string]bool) |
| 351 | |
| 352 | // If no menu order specified, use default order |
| 353 | if userCurdConfig.MenuOrder == "" { |
| 354 | finalOrder = defaultOrder |
| 355 | } else { |
| 356 | // First, process user-specified order |
| 357 | menuItems := strings.Split(userCurdConfig.MenuOrder, ",") |
| 358 | for _, key := range menuItems { |
| 359 | key = strings.TrimSpace(key) |
| 360 | if _, exists := defaultLabels[key]; exists && !seen[key] { |
| 361 | finalOrder = append(finalOrder, key) |
| 362 | seen[key] = true |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | // Add remaining default items at the end |
| 367 | for _, key := range defaultOrder { |
| 368 | if !seen[key] { |
| 369 | finalOrder = append(finalOrder, key) |
| 370 | seen[key] = true |
| 371 | } |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | // Create the final ordered map |
| 376 | orderedMap := make(map[string]string) |
| 377 | for _, key := range finalOrder { |
| 378 | orderedMap[key] = defaultLabels[key] |
| 379 | } |
| 380 | |
| 381 | return orderedMap |
| 382 | } |