| @@ -4,11 +4,11 @@ import ( |
| 4 | "bufio" |
4 | "bufio" |
| 5 | "fmt" |
5 | "fmt" |
| 6 | "os" |
6 | "os" |
| |
7 | "os/exec" |
| 7 | "path/filepath" |
8 | "path/filepath" |
| 8 | "reflect" |
9 | "reflect" |
| 9 | "strconv" |
| |
| 10 | "os/exec" |
| |
| 11 | "runtime" |
10 | "runtime" |
| |
11 | "strconv" |
| 12 | "strings" |
12 | "strings" |
| 13 | ) |
13 | ) |
| 14 | |
14 | |
| @@ -20,6 +20,7 @@ type CurdConfig struct { |
| 20 | SubOrDub string `config:"SubOrDub"` |
20 | SubOrDub string `config:"SubOrDub"` |
| 21 | StoragePath string `config:"StoragePath"` |
21 | StoragePath string `config:"StoragePath"` |
| 22 | AnimeNameLanguage string `config:"AnimeNameLanguage"` |
22 | AnimeNameLanguage string `config:"AnimeNameLanguage"` |
| |
23 | MenuOrder string `config:"MenuOrder"` |
| 23 | PercentageToMarkComplete int `config:"PercentageToMarkComplete"` |
24 | PercentageToMarkComplete int `config:"PercentageToMarkComplete"` |
| 24 | NextEpisodePrompt bool `config:"NextEpisodePrompt"` |
25 | NextEpisodePrompt bool `config:"NextEpisodePrompt"` |
| 25 | SkipOp bool `config:"SkipOp"` |
26 | SkipOp bool `config:"SkipOp"` |
| @@ -44,6 +45,7 @@ func defaultConfigMap() map[string]string { |
| 44 | "StoragePath": "$HOME/.local/share/curd", |
45 | "StoragePath": "$HOME/.local/share/curd", |
| 45 | "AnimeNameLanguage": "english", |
46 | "AnimeNameLanguage": "english", |
| 46 | "SubsLanguage": "english", |
47 | "SubsLanguage": "english", |
| |
48 | "MenuOrder": "CURRENT,ALL,UNTRACKED,UPDATE,CONTINUE_LAST", |
| 47 | "SubOrDub": "sub", |
49 | "SubOrDub": "sub", |
| 48 | "PercentageToMarkComplete": "85", |
50 | "PercentageToMarkComplete": "85", |
| 49 | "NextEpisodePrompt": "false", |
51 | "NextEpisodePrompt": "false", |
| @@ -178,73 +180,73 @@ func createDefaultConfig(path string) error { |
| 178 | } |
180 | } |
| 179 | |
181 | |
| 180 | func getTokenFromTempFile(isWindowsPlatform bool) (string, error) { |
182 | func getTokenFromTempFile(isWindowsPlatform bool) (string, error) { |
| 181 | // Create a temporary file for the token |
183 | // Create a temporary file for the token |
| 182 | tempFile, err := os.CreateTemp("", "curd-token-*.txt") |
184 | tempFile, err := os.CreateTemp("", "curd-token-*.txt") |
| 183 | if err != nil { |
185 | if err != nil { |
| 184 | return "", fmt.Errorf("error creating temp file: %v", err) |
186 | return "", fmt.Errorf("error creating temp file: %v", err) |
| 185 | } |
187 | } |
| 186 | tempPath := tempFile.Name() |
188 | tempPath := tempFile.Name() |
| 187 | tempFile.Close() |
189 | tempFile.Close() |
| 188 | |
190 | |
| 189 | // Write instructions to the temp file |
191 | // 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" + |
192 | 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" |
193 | "Replace this text with your token and save the file.\n" |
| 192 | if err := os.WriteFile(tempPath, []byte(instructions), 0644); err != nil { |
194 | if err := os.WriteFile(tempPath, []byte(instructions), 0644); err != nil { |
| 193 | return "", fmt.Errorf("error writing instructions: %v", err) |
195 | return "", fmt.Errorf("error writing instructions: %v", err) |
| 194 | } |
196 | } |
| 195 | |
197 | |
| 196 | // Open the file with appropriate editor |
198 | // Open the file with appropriate editor |
| 197 | var cmd *exec.Cmd |
199 | var cmd *exec.Cmd |
| 198 | if isWindowsPlatform { |
200 | if isWindowsPlatform { |
| 199 | cmd = exec.Command("notepad.exe", tempPath) |
201 | cmd = exec.Command("notepad.exe", tempPath) |
| 200 | } else { |
202 | } else { |
| 201 | editor := os.Getenv("EDITOR") |
203 | editor := os.Getenv("EDITOR") |
| 202 | if editor == "" { |
204 | if editor == "" { |
| 203 | editor = "nano" // Default to nano if $EDITOR is not set |
205 | editor = "nano" // Default to nano if $EDITOR is not set |
| 204 | } |
206 | } |
| 205 | cmd = exec.Command(editor, tempPath) |
207 | cmd = exec.Command(editor, tempPath) |
| 206 | cmd.Stdin = os.Stdin |
208 | cmd.Stdin = os.Stdin |
| 207 | cmd.Stdout = os.Stdout |
209 | cmd.Stdout = os.Stdout |
| 208 | cmd.Stderr = os.Stderr |
210 | cmd.Stderr = os.Stderr |
| 209 | } |
211 | } |
| 210 | |
212 | |
| 211 | if err := cmd.Run(); err != nil { |
213 | if err := cmd.Run(); err != nil { |
| 212 | return "", fmt.Errorf("error opening editor: %v", err) |
214 | return "", fmt.Errorf("error opening editor: %v", err) |
| 213 | } |
215 | } |
| 214 | |
216 | |
| 215 | // Read the token from the file |
217 | // Read the token from the file |
| 216 | content, err := os.ReadFile(tempPath) |
218 | content, err := os.ReadFile(tempPath) |
| 217 | if err != nil { |
219 | if err != nil { |
| 218 | return "", fmt.Errorf("error reading token: %v", err) |
220 | return "", fmt.Errorf("error reading token: %v", err) |
| 219 | } |
221 | } |
| 220 | |
222 | |
| 221 | // Clean up the temp file |
223 | // Clean up the temp file |
| 222 | os.Remove(tempPath) |
224 | os.Remove(tempPath) |
| 223 | |
225 | |
| 224 | // Extract token (remove instructions and whitespace) |
226 | // Extract token (remove instructions and whitespace) |
| 225 | return strings.TrimSpace(string(content)), nil |
227 | return strings.TrimSpace(string(content)), nil |
| 226 | } |
228 | } |
| 227 | |
229 | |
| 228 | func ChangeToken(config *CurdConfig, user *User) { |
230 | func ChangeToken(config *CurdConfig, user *User) { |
| 229 | var err error |
231 | var err error |
| 230 | tokenPath := filepath.Join(os.ExpandEnv(config.StoragePath), "token") |
232 | tokenPath := filepath.Join(os.ExpandEnv(config.StoragePath), "token") |
| 231 | |
233 | |
| 232 | switch { |
234 | switch { |
| 233 | case runtime.GOOS == "darwin" || runtime.GOOS == "windows": |
235 | case runtime.GOOS == "darwin" || runtime.GOOS == "windows": |
| 234 | user.Token, err = getTokenFromTempFile(runtime.GOOS == "windows") |
236 | user.Token, err = getTokenFromTempFile(runtime.GOOS == "windows") |
| 235 | case config.RofiSelection: |
237 | case config.RofiSelection: |
| 236 | user.Token, err = GetTokenFromRofi() |
238 | user.Token, err = GetTokenFromRofi() |
| 237 | default: |
239 | default: |
| 238 | fmt.Println("Please generate a token from https://anilist.co/api/v2/oauth/authorize?client_id=20686&response_type=token") |
240 | 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) |
241 | fmt.Scanln(&user.Token) |
| 240 | } |
242 | } |
| 241 | |
243 | |
| 242 | if err != nil { |
244 | if err != nil { |
| 243 | Log("Error getting user input: "+err.Error()) |
245 | Log("Error getting user input: " + err.Error()) |
| 244 | ExitCurd(err) |
246 | ExitCurd(err) |
| 245 | } |
247 | } |
| 246 | |
248 | |
| 247 | WriteTokenToFile(user.Token, tokenPath) |
249 | WriteTokenToFile(user.Token, tokenPath) |
| 248 | } |
250 | } |
| 249 | |
251 | |
| 250 | // Load config file from disk into a map (key=value format) |
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 | return config |
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 | } |