Commit ef9a1dde4e

ef9a1dde4eda9724c82963c18faef5fbc6dd096a

parent: d4f3045d74

Unsigned

Wraient <wraient.911@gmail.com> · 2026-01-01 21:41 UTC

Add: Back button for CLI and Rofi

Layout: unified · split

internal/curd.go +390 −321
@@ -218,263 +218,293 @@ func UpdateAnimeEntry(userCurdConfig *CurdConfig, user *User) {
218 {Key: "SCORE", Label: "Add/Change Score"}, 218 {Key: "SCORE", Label: "Add/Change Score"},
219 } 219 }
220 220
221 // Select update option 221 // Navigation loop for update option selection
222 updateSelection, err := DynamicSelect(updateOptions) 222updateOptionLoop:
223 if err != nil { 223 for {
224 Log(fmt.Sprintf("Failed to select update option: %v", err)) 224 // Select update option
225 ExitCurd(fmt.Errorf("Failed to select update option")) 225 updateSelection, err := DynamicSelect(updateOptions)
226 } 226 if err != nil {
227 Log(fmt.Sprintf("Failed to select update option: %v", err))
228 ExitCurd(fmt.Errorf("Failed to select update option"))
229 }
227 230
228 if updateSelection.Key == "-1" { 231 if updateSelection.Key == "-1" {
229 ExitCurd(nil) 232 ExitCurd(nil)
230 } 233 }
231 234
232 // Get user's anime list 235 // Back from update selection returns to home menu
233 var animeListOptions []SelectionOption 236 if updateSelection.Key == "-2" {
234 var animeListMapPreview map[string]RofiSelectPreview 237 return // Return to caller (home menu)
238 }
235 239
236 if userCurdConfig.RofiSelection && userCurdConfig.ImagePreview { 240 // Get user's anime list
237 animeListMapPreview = make(map[string]RofiSelectPreview) 241 var animeListOptions []SelectionOption
238 // Include anime from all categories 242 var animeListMapPreview map[string]RofiSelectPreview
239 for _, entry := range user.AnimeList.Watching { 243
240 title := entry.Media.Title.English 244 if userCurdConfig.RofiSelection && userCurdConfig.ImagePreview {
241 if title == "" || userCurdConfig.AnimeNameLanguage == "romaji" { 245 animeListMapPreview = make(map[string]RofiSelectPreview)
242 title = entry.Media.Title.Romaji 246 // Include anime from all categories
243 } 247 for _, entry := range user.AnimeList.Watching {
244 animeListMapPreview[strconv.Itoa(entry.Media.ID)] = RofiSelectPreview{ 248 title := entry.Media.Title.English
245 Title: title, 249 if title == "" || userCurdConfig.AnimeNameLanguage == "romaji" {
246 CoverImage: entry.CoverImage, 250 title = entry.Media.Title.Romaji
251 }
252 animeListMapPreview[strconv.Itoa(entry.Media.ID)] = RofiSelectPreview{
253 Title: title,
254 CoverImage: entry.CoverImage,
255 }
247 } 256 }
248 } 257 for _, entry := range user.AnimeList.Completed {
249 for _, entry := range user.AnimeList.Completed { 258 title := entry.Media.Title.English
250 title := entry.Media.Title.English 259 if title == "" || userCurdConfig.AnimeNameLanguage == "romaji" {
251 if title == "" || userCurdConfig.AnimeNameLanguage == "romaji" { 260 title = entry.Media.Title.Romaji
252 title = entry.Media.Title.Romaji 261 }
262 animeListMapPreview[strconv.Itoa(entry.Media.ID)] = RofiSelectPreview{
263 Title: title,
264 CoverImage: entry.CoverImage,
265 }
253 } 266 }
254 animeListMapPreview[strconv.Itoa(entry.Media.ID)] = RofiSelectPreview{ 267 for _, entry := range user.AnimeList.Paused {
255 Title: title, 268 title := entry.Media.Title.English
256 CoverImage: entry.CoverImage, 269 if title == "" || userCurdConfig.AnimeNameLanguage == "romaji" {
270 title = entry.Media.Title.Romaji
271 }
272 animeListMapPreview[strconv.Itoa(entry.Media.ID)] = RofiSelectPreview{
273 Title: title,
274 CoverImage: entry.CoverImage,
275 }
257 } 276 }
258 } 277 for _, entry := range user.AnimeList.Dropped {
259 for _, entry := range user.AnimeList.Paused { 278 title := entry.Media.Title.English
260 title := entry.Media.Title.English 279 if title == "" || userCurdConfig.AnimeNameLanguage == "romaji" {
261 if title == "" || userCurdConfig.AnimeNameLanguage == "romaji" { 280 title = entry.Media.Title.Romaji
262 title = entry.Media.Title.Romaji 281 }
282 animeListMapPreview[strconv.Itoa(entry.Media.ID)] = RofiSelectPreview{
283 Title: title,
284 CoverImage: entry.CoverImage,
285 }
263 } 286 }
264 animeListMapPreview[strconv.Itoa(entry.Media.ID)] = RofiSelectPreview{ 287 for _, entry := range user.AnimeList.Planning {
265 Title: title, 288 title := entry.Media.Title.English
266 CoverImage: entry.CoverImage, 289 if title == "" || userCurdConfig.AnimeNameLanguage == "romaji" {
290 title = entry.Media.Title.Romaji
291 }
292 animeListMapPreview[strconv.Itoa(entry.Media.ID)] = RofiSelectPreview{
293 Title: title,
294 CoverImage: entry.CoverImage,
295 }
267 } 296 }
268 } 297 for _, entry := range user.AnimeList.Rewatching {
269 for _, entry := range user.AnimeList.Dropped { 298 title := entry.Media.Title.English
270 title := entry.Media.Title.English 299 if title == "" || userCurdConfig.AnimeNameLanguage == "romaji" {
271 if title == "" || userCurdConfig.AnimeNameLanguage == "romaji" { 300 title = entry.Media.Title.Romaji
272 title = entry.Media.Title.Romaji 301 }
302 animeListMapPreview[strconv.Itoa(entry.Media.ID)] = RofiSelectPreview{
303 Title: title,
304 CoverImage: entry.CoverImage,
305 }
273 } 306 }
274 animeListMapPreview[strconv.Itoa(entry.Media.ID)] = RofiSelectPreview{ 307 } else {
275 Title: title, 308 animeListOptions = make([]SelectionOption, 0)
276 CoverImage: entry.CoverImage, 309 // Include anime from all categories
310 for _, entry := range user.AnimeList.Watching {
311 title := entry.Media.Title.English
312 if title == "" || userCurdConfig.AnimeNameLanguage == "romaji" {
313 title = entry.Media.Title.Romaji
314 }
315 animeListOptions = append(animeListOptions, SelectionOption{
316 Key: strconv.Itoa(entry.Media.ID),
317 Label: title,
318 })
277 } 319 }
278 } 320 for _, entry := range user.AnimeList.Completed {
279 for _, entry := range user.AnimeList.Planning { 321 title := entry.Media.Title.English
280 title := entry.Media.Title.English 322 if title == "" || userCurdConfig.AnimeNameLanguage == "romaji" {
281 if title == "" || userCurdConfig.AnimeNameLanguage == "romaji" { 323 title = entry.Media.Title.Romaji
282 title = entry.Media.Title.Romaji 324 }
325 animeListOptions = append(animeListOptions, SelectionOption{
326 Key: strconv.Itoa(entry.Media.ID),
327 Label: title,
328 })
283 } 329 }
284 animeListMapPreview[strconv.Itoa(entry.Media.ID)] = RofiSelectPreview{ 330 for _, entry := range user.AnimeList.Paused {
285 Title: title, 331 title := entry.Media.Title.English
286 CoverImage: entry.CoverImage, 332 if title == "" || userCurdConfig.AnimeNameLanguage == "romaji" {
333 title = entry.Media.Title.Romaji
334 }
335 animeListOptions = append(animeListOptions, SelectionOption{
336 Key: strconv.Itoa(entry.Media.ID),
337 Label: title,
338 })
287 } 339 }
288 } 340 for _, entry := range user.AnimeList.Dropped {
289 for _, entry := range user.AnimeList.Rewatching { 341 title := entry.Media.Title.English
290 title := entry.Media.Title.English 342 if title == "" || userCurdConfig.AnimeNameLanguage == "romaji" {
291 if title == "" || userCurdConfig.AnimeNameLanguage == "romaji" { 343 title = entry.Media.Title.Romaji
292 title = entry.Media.Title.Romaji 344 }
345 animeListOptions = append(animeListOptions, SelectionOption{
346 Key: strconv.Itoa(entry.Media.ID),
347 Label: title,
348 })
293 } 349 }
294 animeListMapPreview[strconv.Itoa(entry.Media.ID)] = RofiSelectPreview{ 350 for _, entry := range user.AnimeList.Planning {
295 Title: title, 351 title := entry.Media.Title.English
296 CoverImage: entry.CoverImage, 352 if title == "" || userCurdConfig.AnimeNameLanguage == "romaji" {
353 title = entry.Media.Title.Romaji
354 }
355 animeListOptions = append(animeListOptions, SelectionOption{
356 Key: strconv.Itoa(entry.Media.ID),
357 Label: title,
358 })
297 } 359 }
298 } 360 for _, entry := range user.AnimeList.Rewatching {
299 } else { 361 title := entry.Media.Title.English
300 animeListOptions = make([]SelectionOption, 0) 362 if title == "" || userCurdConfig.AnimeNameLanguage == "romaji" {
301 // Include anime from all categories 363 title = entry.Media.Title.Romaji
302 for _, entry := range user.AnimeList.Watching { 364 }
303 title := entry.Media.Title.English 365 animeListOptions = append(animeListOptions, SelectionOption{
304 if title == "" || userCurdConfig.AnimeNameLanguage == "romaji" { 366 Key: strconv.Itoa(entry.Media.ID),
305 title = entry.Media.Title.Romaji 367 Label: title,
368 })
306 } 369 }
307 animeListOptions = append(animeListOptions, SelectionOption{
308 Key: strconv.Itoa(entry.Media.ID),
309 Label: title,
310 })
311 } 370 }
312 for _, entry := range user.AnimeList.Completed { 371
313 title := entry.Media.Title.English 372 // Anime selection loop
314 if title == "" || userCurdConfig.AnimeNameLanguage == "romaji" { 373 animeSelectLoop:
315 title = entry.Media.Title.Romaji 374 for {
375 // Select anime to update
376 var selectedAnime SelectionOption
377 if userCurdConfig.RofiSelection && userCurdConfig.ImagePreview {
378 selectedAnime, err = DynamicSelectPreview(animeListMapPreview, false)
379 } else {
380 selectedAnime, err = DynamicSelect(animeListOptions)
316 } 381 }
317 animeListOptions = append(animeListOptions, SelectionOption{ 382 if err != nil {
318 Key: strconv.Itoa(entry.Media.ID), 383 Log(fmt.Sprintf("Failed to select anime: %v", err))
319 Label: title, 384 ExitCurd(fmt.Errorf("Failed to select anime"))
320 })
321 }
322 for _, entry := range user.AnimeList.Paused {
323 title := entry.Media.Title.English
324 if title == "" || userCurdConfig.AnimeNameLanguage == "romaji" {
325 title = entry.Media.Title.Romaji
326 } 385 }
327 animeListOptions = append(animeListOptions, SelectionOption{ 386
328 Key: strconv.Itoa(entry.Media.ID), 387 if selectedAnime.Key == "-1" {
329 Label: title, 388 ExitCurd(nil)
330 })
331 }
332 for _, entry := range user.AnimeList.Dropped {
333 title := entry.Media.Title.English
334 if title == "" || userCurdConfig.AnimeNameLanguage == "romaji" {
335 title = entry.Media.Title.Romaji
336 } 389 }
337 animeListOptions = append(animeListOptions, SelectionOption{ 390
338 Key: strconv.Itoa(entry.Media.ID), 391 // Back from anime selection goes to update option selection
339 Label: title, 392 if selectedAnime.Key == "-2" {
340 }) 393 ClearScreen()
341 } 394 continue updateOptionLoop
342 for _, entry := range user.AnimeList.Planning {
343 title := entry.Media.Title.English
344 if title == "" || userCurdConfig.AnimeNameLanguage == "romaji" {
345 title = entry.Media.Title.Romaji
346 } 395 }
347 animeListOptions = append(animeListOptions, SelectionOption{ 396
348 Key: strconv.Itoa(entry.Media.ID), 397 animeID, err := strconv.Atoi(selectedAnime.Key)
349 Label: title, 398 if err != nil {
350 }) 399 Log(fmt.Sprintf("Failed to convert anime ID: %v", err))
351 } 400 ExitCurd(fmt.Errorf("Failed to convert anime ID"))
352 for _, entry := range user.AnimeList.Rewatching {
353 title := entry.Media.Title.English
354 if title == "" || userCurdConfig.AnimeNameLanguage == "romaji" {
355 title = entry.Media.Title.Romaji
356 } 401 }
357 animeListOptions = append(animeListOptions, SelectionOption{
358 Key: strconv.Itoa(entry.Media.ID),
359 Label: title,
360 })
361 }
362 }
363 402
364 // Select anime to update 403 // After getting animeID, get the current anime entry
365 var selectedAnime SelectionOption 404 selectedAnilistAnime, err := FindAnimeByAnilistID(user.AnimeList, selectedAnime.Key)
366 if userCurdConfig.RofiSelection && userCurdConfig.ImagePreview { 405 if err != nil {
367 selectedAnime, err = DynamicSelectPreview(animeListMapPreview, false) 406 Log(fmt.Sprintf("Can not find the anime in anilist animelist: %v", err))
368 } else { 407 ExitCurd(fmt.Errorf("Can not find the anime in anilist animelist"))
369 selectedAnime, err = DynamicSelect(animeListOptions) 408 }
370 } 409 ClearScreen()
371 if err != nil {
372 Log(fmt.Sprintf("Failed to select anime: %v", err))
373 ExitCurd(fmt.Errorf("Failed to select anime"))
374 }
375 410
376 if selectedAnime.Key == "-1" { 411 // Final selection loop (for category/progress/score)
377 ExitCurd(nil) 412 for {
378 } 413 switch updateSelection.Key {
414 case "CATEGORY":
415 categories := []SelectionOption{
416 {Key: "CURRENT", Label: "Currently Watching"},
417 {Key: "COMPLETED", Label: "Completed"},
418 {Key: "PAUSED", Label: "On Hold"},
419 {Key: "DROPPED", Label: "Dropped"},
420 {Key: "PLANNING", Label: "Plan to Watch"},
421 {Key: "REPEATING", Label: "Rewatching"}, // Anilist uses REPEATING for rewatching
422 }
379 423
380 animeID, err := strconv.Atoi(selectedAnime.Key) 424 currentStatus := "None"
381 if err != nil { 425 if selectedAnilistAnime.Status != "" {
382 Log(fmt.Sprintf("Failed to convert anime ID: %v", err)) 426 // Find the label for the current status
383 ExitCurd(fmt.Errorf("Failed to convert anime ID")) 427 for _, cat := range categories {
384 } 428 if cat.Key == selectedAnilistAnime.Status {
429 currentStatus = cat.Label
430 break
431 }
432 }
433 }
434 CurdOut(fmt.Sprintf("Current category: %s", currentStatus))
385 435
386 // After getting animeID, get the current anime entry 436 categorySelection, err := DynamicSelect(categories)
387 selectedAnilistAnime, err := FindAnimeByAnilistID(user.AnimeList, selectedAnime.Key) 437 if err != nil {
388 if err != nil { 438 Log(fmt.Sprintf("Failed to select category: %v", err))
389 Log(fmt.Sprintf("Can not find the anime in anilist animelist: %v", err)) 439 ExitCurd(fmt.Errorf("Failed to select category"))
390 ExitCurd(fmt.Errorf("Can not find the anime in anilist animelist")) 440 }
391 }
392 ClearScreen()
393 switch updateSelection.Key {
394 case "CATEGORY":
395 categories := []SelectionOption{
396 {Key: "CURRENT", Label: "Currently Watching"},
397 {Key: "COMPLETED", Label: "Completed"},
398 {Key: "PAUSED", Label: "On Hold"},
399 {Key: "DROPPED", Label: "Dropped"},
400 {Key: "PLANNING", Label: "Plan to Watch"},
401 {Key: "REPEATING", Label: "Rewatching"}, // Anilist uses REPEATING for rewatching
402 }
403 441
404 currentStatus := "None" 442 if categorySelection.Key == "-1" {
405 if selectedAnilistAnime.Status != "" { 443 ExitCurd(nil)
406 // Find the label for the current status 444 }
407 for _, cat := range categories {
408 if cat.Key == selectedAnilistAnime.Status {
409 currentStatus = cat.Label
410 break
411 }
412 }
413 }
414 CurdOut(fmt.Sprintf("Current category: %s", currentStatus))
415 445
416 categorySelection, err := DynamicSelect(categories) 446 // Back from category selection goes to anime selection
417 if err != nil { 447 if categorySelection.Key == "-2" {
418 Log(fmt.Sprintf("Failed to select category: %v", err)) 448 ClearScreen()
419 ExitCurd(fmt.Errorf("Failed to select category")) 449 continue animeSelectLoop
420 } 450 }
421 451
422 if categorySelection.Key == "-1" { 452 err = UpdateAnimeStatus(user.Token, animeID, categorySelection.Key)
423 ExitCurd(nil) 453 if err != nil {
424 } 454 Log(fmt.Sprintf("Failed to update anime status: %v", err))
455 ExitCurd(fmt.Errorf("Failed to update anime status"))
456 }
425 457
426 err = UpdateAnimeStatus(user.Token, animeID, categorySelection.Key) 458 case "PROGRESS":
427 if err != nil { 459 currentProgress := "None"
428 Log(fmt.Sprintf("Failed to update anime status: %v", err)) 460 if selectedAnilistAnime.Progress > 0 {
429 ExitCurd(fmt.Errorf("Failed to update anime status")) 461 currentProgress = strconv.Itoa(selectedAnilistAnime.Progress)
430 } 462 }
431 463
432 case "PROGRESS": 464 var progress string
433 currentProgress := "None" 465 if userCurdConfig.RofiSelection {
434 if selectedAnilistAnime.Progress > 0 { 466 progress, err = GetUserInputFromRofi(fmt.Sprintf("Current progress: %s\nEnter new progress (episode number)", currentProgress))
435 currentProgress = strconv.Itoa(selectedAnilistAnime.Progress) 467 if err != nil {
436 } 468 Log(fmt.Sprintf("Failed to get progress input: %v", err))
469 ExitCurd(fmt.Errorf("Failed to get progress input"))
470 }
471 } else {
472 CurdOut(fmt.Sprintf("Current progress: %s", currentProgress))
473 CurdOut("Enter new progress (episode number):")
474 fmt.Scanln(&progress)
475 }
437 476
438 var progress string 477 progressNum, err := strconv.Atoi(progress)
439 if userCurdConfig.RofiSelection { 478 if err != nil {
440 progress, err = GetUserInputFromRofi(fmt.Sprintf("Current progress: %s\nEnter new progress (episode number)", currentProgress)) 479 Log(fmt.Sprintf("Failed to convert progress to number: %v", err))
441 if err != nil { 480 ExitCurd(fmt.Errorf("Failed to convert progress to number"))
442 Log(fmt.Sprintf("Failed to get progress input: %v", err)) 481 }
443 ExitCurd(fmt.Errorf("Failed to get progress input"))
444 }
445 } else {
446 CurdOut(fmt.Sprintf("Current progress: %s", currentProgress))
447 CurdOut("Enter new progress (episode number):")
448 fmt.Scanln(&progress)
449 }
450 482
451 progressNum, err := strconv.Atoi(progress) 483 err = UpdateAnimeProgress(user.Token, animeID, progressNum)
452 if err != nil { 484 if err != nil {
453 Log(fmt.Sprintf("Failed to convert progress to number: %v", err)) 485 Log(fmt.Sprintf("Failed to update anime progress: %v", err))
454 ExitCurd(fmt.Errorf("Failed to convert progress to number")) 486 ExitCurd(fmt.Errorf("Failed to update anime progress"))
455 } 487 }
456 488
457 err = UpdateAnimeProgress(user.Token, animeID, progressNum) 489 case "SCORE":
458 if err != nil { 490 currentScore := "None"
459 Log(fmt.Sprintf("Failed to update anime progress: %v", err)) 491 if selectedAnilistAnime.Score > 0 {
460 ExitCurd(fmt.Errorf("Failed to update anime progress")) 492 currentScore = strconv.Itoa(int(selectedAnilistAnime.Score))
461 } 493 }
494 CurdOut(fmt.Sprintf("Current score: %s", currentScore))
462 495
463 case "SCORE": 496 err = RateAnime(user.Token, animeID)
464 currentScore := "None" 497 if err != nil {
465 if selectedAnilistAnime.Score > 0 { 498 Log(fmt.Sprintf("Failed to update anime score: %v", err))
466 currentScore = strconv.Itoa(int(selectedAnilistAnime.Score)) 499 ExitCurd(fmt.Errorf("Failed to update anime score"))
467 } 500 }
468 CurdOut(fmt.Sprintf("Current score: %s", currentScore)) 501 }
469 502
470 err = RateAnime(user.Token, animeID) 503 CurdOut("Anime updated successfully!")
471 if err != nil { 504 return
472 Log(fmt.Sprintf("Failed to update anime score: %v", err)) 505 }
473 ExitCurd(fmt.Errorf("Failed to update anime score"))
474 } 506 }
475 } 507 }
476
477 CurdOut("Anime updated successfully!")
478} 508}
479 509
480func UpdateCurd(repo, fileName string) error { 510func UpdateCurd(repo, fileName string) error {
@@ -714,6 +744,12 @@ func SetupCurd(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAni
714 user.AnimeList = ParseAnimeList(anilistUserData) 744 user.AnimeList = ParseAnimeList(anilistUserData)
715 } 745 }
716 746
747 // Variables for selection results (used in both branches and after)
748 var anilistSelectedOption SelectionOption
749 var selectedAllanimeAnime SelectionOption
750 var userQuery string
751 _ = selectedAllanimeAnime // Used later in the function
752
717 // If continueLast flag is set, directly get the last watched anime 753 // If continueLast flag is set, directly get the last watched anime
718 if anime.Ep.ContinueLast { 754 if anime.Ep.ContinueLast {
719 // Get the last anime ID from the curd_id file 755 // Get the last anime ID from the curd_id file
@@ -738,6 +774,7 @@ func SetupCurd(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAni
738 774
739 // Set the anime details 775 // Set the anime details
740 anime.AnilistId = animePointer.AnilistId 776 anime.AnilistId = animePointer.AnilistId
777 anilistSelectedOption.Key = strconv.Itoa(animePointer.AnilistId)
741 // anime.AllanimeId = animePointer.AllanimeId 778 // anime.AllanimeId = animePointer.AllanimeId
742 // anime.Title = animePointer.Title 779 // anime.Title = animePointer.Title
743 // anime.Ep.Number = animePointer.Ep.Number 780 // anime.Ep.Number = animePointer.Ep.Number
@@ -745,126 +782,157 @@ func SetupCurd(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAni
745 // anime.Ep.Resume = true 782 // anime.Ep.Resume = true
746 783
747 } else { 784 } else {
748 // Skip category selection if Current flag is set 785 // Navigation loop for category and anime selection
749 var categorySelection SelectionOption 786 categorySelectionLoop:
750 if userCurdConfig.CurrentCategory { 787 for {
751 categorySelection = SelectionOption{ 788 // Skip category selection if Current flag is set
752 Key: "CURRENT", 789 var categorySelection SelectionOption
753 Label: "Currently Watching", 790 if userCurdConfig.CurrentCategory {
754 } 791 categorySelection = SelectionOption{
755 } else { 792 Key: "CURRENT",
756 // Create category selection map 793 Label: "Currently Watching",
757 // Get ordered categories 794 }
758 orderedCategories := getOrderedCategories(userCurdConfig) 795 } else {
796 // Create category selection map
797 // Get ordered categories
798 orderedCategories := getOrderedCategories(userCurdConfig)
759 799
760 // Use DynamicSelect with ordered categories directly 800 // Use DynamicSelect with ordered categories directly
761 categorySelection, err = DynamicSelect(orderedCategories) 801 categorySelection, err = DynamicSelect(orderedCategories)
762 802
763 if err != nil { 803 if err != nil {
764 Log(fmt.Sprintf("Failed to select category: %v", err)) 804 Log(fmt.Sprintf("Failed to select category: %v", err))
765 ExitCurd(fmt.Errorf("Failed to select category")) 805 ExitCurd(fmt.Errorf("Failed to select category"))
766 } 806 }
767 807
768 if categorySelection.Key == "-1" { 808 if categorySelection.Key == "-1" {
769 ExitCurd(nil) 809 ExitCurd(nil)
770 } 810 }
811
812 // Handle options
813 if categorySelection.Key == "UPDATE" {
814 ClearScreen()
815 UpdateAnimeEntry(userCurdConfig, user)
816 // If UpdateAnimeEntry returns, user pressed back - continue to category selection
817 ClearScreen()
818 continue categorySelectionLoop
819 } else if categorySelection.Key == "UNTRACKED" {
820 ClearScreen()
821 WatchUntracked(userCurdConfig)
822 // If WatchUntracked returns, user pressed back OR watched is done- continue to category selection
823 ClearScreen()
824 continue categorySelectionLoop
825 } else if categorySelection.Key == "CONTINUE_LAST" {
826 anime.Ep.ContinueLast = true
827 }
771 828
772 // Handle options
773 if categorySelection.Key == "UPDATE" {
774 ClearScreen()
775 UpdateAnimeEntry(userCurdConfig, user)
776 ExitCurd(nil)
777 } else if categorySelection.Key == "UNTRACKED" {
778 ClearScreen() 829 ClearScreen()
779 WatchUntracked(userCurdConfig)
780 } else if categorySelection.Key == "CONTINUE_LAST" {
781 anime.Ep.ContinueLast = true
782 } 830 }
783 831
784 ClearScreen() 832 if userCurdConfig.RofiSelection && userCurdConfig.ImagePreview {
785 } 833 animeListMapPreview = make(map[string]RofiSelectPreview)
786 834 for _, entry := range getEntriesByCategory(user.AnimeList, categorySelection.Key) {
787 if userCurdConfig.RofiSelection && userCurdConfig.ImagePreview { 835 title := entry.Media.Title.English
788 animeListMapPreview = make(map[string]RofiSelectPreview) 836 if title == "" || userCurdConfig.AnimeNameLanguage == "romaji" {
789 for _, entry := range getEntriesByCategory(user.AnimeList, categorySelection.Key) { 837 title = entry.Media.Title.Romaji
790 title := entry.Media.Title.English 838 }
791 if title == "" || userCurdConfig.AnimeNameLanguage == "romaji" { 839 animeListMapPreview[strconv.Itoa(entry.Media.ID)] = RofiSelectPreview{
792 title = entry.Media.Title.Romaji 840 Title: title,
793 } 841 CoverImage: entry.CoverImage,
794 animeListMapPreview[strconv.Itoa(entry.Media.ID)] = RofiSelectPreview{ 842 }
795 Title: title,
796 CoverImage: entry.CoverImage,
797 } 843 }
798 } 844 } else {
799 } else { 845 animeListOptions = make([]SelectionOption, 0)
800 animeListOptions = make([]SelectionOption, 0) 846 for _, entry := range getEntriesByCategory(user.AnimeList, categorySelection.Key) {
801 for _, entry := range getEntriesByCategory(user.AnimeList, categorySelection.Key) { 847 title := entry.Media.Title.English
802 title := entry.Media.Title.English 848 if title == "" || userCurdConfig.AnimeNameLanguage == "romaji" {
803 if title == "" || userCurdConfig.AnimeNameLanguage == "romaji" { 849 title = entry.Media.Title.Romaji
804 title = entry.Media.Title.Romaji 850 }
851 animeListOptions = append(animeListOptions, SelectionOption{
852 Key: strconv.Itoa(entry.Media.ID),
853 Label: title,
854 })
805 } 855 }
806 animeListOptions = append(animeListOptions, SelectionOption{
807 Key: strconv.Itoa(entry.Media.ID),
808 Label: title,
809 })
810 } 856 }
811 }
812 }
813 857
814 var anilistSelectedOption SelectionOption 858 // Anime selection loop (for back navigation)
815 var selectedAllanimeAnime SelectionOption 859 animeSelectionLoop:
816 var userQuery string 860 for {
861 if anime.Ep.ContinueLast {
862 // Get the last watched anime ID from the curd_id file
863 curdIDPath := filepath.Join(os.ExpandEnv(userCurdConfig.StoragePath), "curd_id")
864 curdIDBytes, err := os.ReadFile(curdIDPath)
865 if err != nil {
866 Log(fmt.Sprintf("Error reading curd_id file: %v", err))
867 ExitCurd(fmt.Errorf("Error reading curd_id file"))
868 }
817 869
818 if anime.Ep.ContinueLast { 870 lastWatchedID, err := strconv.Atoi(strings.TrimSpace(string(curdIDBytes)))
819 // Get the last watched anime ID from the curd_id file 871 if err != nil {
820 curdIDPath := filepath.Join(os.ExpandEnv(userCurdConfig.StoragePath), "curd_id") 872 Log(fmt.Sprintf("Error converting curd_id to integer: %v", err))
821 curdIDBytes, err := os.ReadFile(curdIDPath) 873 ExitCurd(fmt.Errorf("Error converting curd_id to integer"))
822 if err != nil { 874 }
823 Log(fmt.Sprintf("Error reading curd_id file: %v", err))
824 ExitCurd(fmt.Errorf("Error reading curd_id file"))
825 }
826 875
827 lastWatchedID, err := strconv.Atoi(strings.TrimSpace(string(curdIDBytes))) 876 anime.AnilistId = lastWatchedID
828 if err != nil { 877 anilistSelectedOption.Key = strconv.Itoa(lastWatchedID)
829 Log(fmt.Sprintf("Error converting curd_id to integer: %v", err)) 878 break categorySelectionLoop
830 ExitCurd(fmt.Errorf("Error converting curd_id to integer")) 879 }
831 }
832 880
833 anime.AnilistId = lastWatchedID 881 // Select anime to watch (Anilist)
834 anilistSelectedOption.Key = strconv.Itoa(lastWatchedID) 882 var err error
835 } else { 883 if userCurdConfig.RofiSelection && userCurdConfig.ImagePreview {
836 // Select anime to watch (Anilist) 884 anilistSelectedOption, err = DynamicSelectPreview(animeListMapPreview, true)
837 var err error 885 } else {
838 if userCurdConfig.RofiSelection && userCurdConfig.ImagePreview { 886 // Add "Add new anime" option to the slice
839 anilistSelectedOption, err = DynamicSelectPreview(animeListMapPreview, true) 887 tempOptions := make([]SelectionOption, len(animeListOptions))
840 } else { 888 copy(tempOptions, animeListOptions)
841 // Add "Add new anime" option to the slice 889 tempOptions = append(tempOptions, SelectionOption{
842 animeListOptions = append(animeListOptions, SelectionOption{ 890 Key: "add_new",
843 Key: "add_new", 891 Label: "Add new anime",
844 Label: "Add new anime", 892 })
845 }) 893
894 anilistSelectedOption, err = DynamicSelect(tempOptions)
895 }
896 if err != nil {
897 Log(fmt.Sprintf("Error selecting anime: %v", err))
898 ExitCurd(fmt.Errorf("Error selecting anime"))
899 }
846 900
847 anilistSelectedOption, err = DynamicSelect(animeListOptions) 901 Log(anilistSelectedOption)
848 }
849 if err != nil {
850 Log(fmt.Sprintf("Error selecting anime: %v", err))
851 ExitCurd(fmt.Errorf("Error selecting anime"))
852 }
853 902
854 Log(anilistSelectedOption) 903 if anilistSelectedOption.Key == "-1" {
904 ExitCurd(nil)
905 }
855 906
856 if anilistSelectedOption.Key == "-1" { 907 // Handle back navigation - go back to category selection
857 ExitCurd(nil) 908 if anilistSelectedOption.Key == "-2" {
858 } 909 if userCurdConfig.CurrentCategory {
910 // If CurrentCategory is forced, back means quit
911 ExitCurd(nil)
912 }
913 ClearScreen()
914 continue categorySelectionLoop
915 }
859 916
860 if anilistSelectedOption.Label == "add_new" || anilistSelectedOption.Key == "add_new" { 917 if anilistSelectedOption.Label == "add_new" || anilistSelectedOption.Key == "add_new" {
861 anilistSelectedOption = AddNewAnime(userCurdConfig, anime, user, databaseAnimes) 918 addResult := AddNewAnime(userCurdConfig, anime, user, databaseAnimes)
862 } 919 if addResult.Key == "-2" {
920 // Back from add new anime goes to anime selection
921 ClearScreen()
922 continue animeSelectionLoop
923 }
924 anilistSelectedOption = addResult
925 }
863 926
864 anime.AnilistId, err = strconv.Atoi(anilistSelectedOption.Key) 927 anime.AnilistId, err = strconv.Atoi(anilistSelectedOption.Key)
865 if err != nil { 928 if err != nil {
866 Log(fmt.Sprintf("Error converting Anilist ID: %v", err)) 929 Log(fmt.Sprintf("Error converting Anilist ID: %v", err))
867 ExitCurd(fmt.Errorf("Error converting Anilist ID")) 930 ExitCurd(fmt.Errorf("Error converting Anilist ID"))
931 }
932
933 // Successfully selected anime, break out of both loops
934 break categorySelectionLoop
935 }
868 } 936 }
869 } 937 }
870 // Find anime in Local history 938 // Find anime in Local history
@@ -1579,3 +1647,4 @@ func HandleLastEpisodeCompletion(userCurdConfig *CurdConfig, anime *Anime, userT
1579 } 1647 }
1580 } 1648 }
1581} 1649}
1650
internal/localTracking.go +40 −30
@@ -254,43 +254,52 @@ func WatchUntracked(userCurdConfig *CurdConfig) {
254 var err error 254 var err error
255 var anime Anime 255 var anime Anime
256 256
257 // Get anime name from user 257 // Anime search and selection loop
258 if userCurdConfig.RofiSelection { 258 for {
259 userInput, err := GetUserInputFromRofi("Enter the anime name") 259 // Get anime name from user
260 if userCurdConfig.RofiSelection {
261 userInput, err := GetUserInputFromRofi("Enter the anime name")
262 if err != nil {
263 Log("Error getting user input: " + err.Error())
264 ExitCurd(fmt.Errorf("Error getting user input: " + err.Error()))
265 }
266 query = userInput
267 } else {
268 CurdOut("Enter the anime name:")
269 fmt.Scanln(&query)
270 }
271
272 // Search for the anime
273 animeList, err = SearchAnime(query, userCurdConfig.SubOrDub)
260 if err != nil { 274 if err != nil {
261 Log("Error getting user input: " + err.Error()) 275 Log(fmt.Sprintf("Failed to search anime: %v", err))
262 ExitCurd(fmt.Errorf("Error getting user input: " + err.Error())) 276 ExitCurd(fmt.Errorf("Failed to search anime"))
263 } 277 }
264 query = userInput
265 } else {
266 CurdOut("Enter the anime name:")
267 fmt.Scanln(&query)
268 }
269 278
270 // Search for the anime 279 if len(animeList) == 0 {
271 animeList, err = SearchAnime(query, userCurdConfig.SubOrDub) 280 ExitCurd(fmt.Errorf("No results found."))
272 if err != nil { 281 }
273 Log(fmt.Sprintf("Failed to search anime: %v", err))
274 ExitCurd(fmt.Errorf("Failed to search anime"))
275 }
276 282
277 if len(animeList) == 0 { 283 // Select anime from search results
278 ExitCurd(fmt.Errorf("No results found.")) 284 selectedAnime, err := DynamicSelect(animeList)
279 } 285 if err != nil {
286 Log(fmt.Sprintf("Failed to select anime: %v", err))
287 ExitCurd(fmt.Errorf("Failed to select anime"))
288 }
280 289
281 // Select anime from search results 290 if selectedAnime.Key == "-1" {
282 selectedAnime, err := DynamicSelect(animeList) 291 ExitCurd(nil)
283 if err != nil { 292 }
284 Log(fmt.Sprintf("Failed to select anime: %v", err))
285 ExitCurd(fmt.Errorf("Failed to select anime"))
286 }
287 293
288 if selectedAnime.Key == "-1" { 294 // Back goes to home menu
289 ExitCurd(nil) 295 if selectedAnime.Key == "-2" {
290 } 296 return // Return to caller (home menu)
297 }
291 298
292 anime.AllanimeId = selectedAnime.Key 299 anime.AllanimeId = selectedAnime.Key
293 anime.Title.English = selectedAnime.Label 300 anime.Title.English = selectedAnime.Label
301 break
302 }
294 303
295 // Get episode number 304 // Get episode number
296 var episodeNumber int 305 var episodeNumber int
@@ -413,3 +422,4 @@ func WatchUntracked(userCurdConfig *CurdConfig) {
413 } 422 }
414 423
415} 424}
425
internal/selection_menu.go +76 −21
@@ -27,6 +27,7 @@ type Model struct {
27 terminalHeight int 27 terminalHeight int
28 scrollOffset int 28 scrollOffset int
29 addNewOption bool 29 addNewOption bool
30 isHomeMenu bool // If true, ESC quits; if false, ESC goes back
30} 31}
31 32
32var ( 33var (
@@ -83,6 +84,18 @@ func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
83 case "ctrl+c": 84 case "ctrl+c":
84 // Return quit selection option directly 85 // Return quit selection option directly
85 return m, tea.Quit 86 return m, tea.Quit
87 case "esc":
88 // ESC: quit on home menu, go back on sub-menus
89 if m.isHomeMenu {
90 // Quit
91 m.filteredKeys = []SelectionOption{{Key: "-1", Label: "Quit"}}
92 m.selected = 0
93 } else {
94 // Go back
95 m.filteredKeys = []SelectionOption{{Key: "-2", Label: "Back"}}
96 m.selected = 0
97 }
98 return m, tea.Quit
86 case "backspace": 99 case "backspace":
87 if len(m.filter) > 0 { 100 if len(m.filter) > 0 {
88 m.filter = m.filter[:len(m.filter)-1] 101 m.filter = m.filter[:len(m.filter)-1]
@@ -200,6 +213,10 @@ func (m *Model) filterOptions() {
200 if m.addNewOption { 213 if m.addNewOption {
201 m.filteredKeys = append(m.filteredKeys, SelectionOption{Label: "Add new anime", Key: "add_new"}) 214 m.filteredKeys = append(m.filteredKeys, SelectionOption{Label: "Add new anime", Key: "add_new"})
202 } 215 }
216 // Add Back option before Quit (only for non-home menus)
217 if !m.isHomeMenu {
218 m.filteredKeys = append(m.filteredKeys, SelectionOption{Label: "Back", Key: "-2"})
219 }
203 m.filteredKeys = append(m.filteredKeys, SelectionOption{Label: "Quit", Key: "-1"}) 220 m.filteredKeys = append(m.filteredKeys, SelectionOption{Label: "Quit", Key: "-1"})
204} 221}
205 222
@@ -231,6 +248,8 @@ func DynamicSelectPreview(options map[string]RofiSelectPreview, addnewoption boo
231 if addnewoption { 248 if addnewoption {
232 rofiInput.WriteString("Add new anime\n") 249 rofiInput.WriteString("Add new anime\n")
233 } 250 }
251 // Add Back option before Quit (this is always a sub-menu when using preview)
252 rofiInput.WriteString("Back\n")
234 rofiInput.WriteString("Quit\n") 253 rofiInput.WriteString("Quit\n")
235 254
236 // Run rofi 255 // Run rofi
@@ -244,7 +263,8 @@ func DynamicSelectPreview(options map[string]RofiSelectPreview, addnewoption boo
244 if err := cmd.Run(); err != nil { 263 if err := cmd.Run(); err != nil {
245 Log(fmt.Sprintf("Rofi stderr: %s", stderr.String())) 264 Log(fmt.Sprintf("Rofi stderr: %s", stderr.String()))
246 Log(fmt.Sprintf("Rofi stdout: %s", stdout.String())) 265 Log(fmt.Sprintf("Rofi stdout: %s", stdout.String()))
247 return SelectionOption{}, fmt.Errorf("failed to execute rofi: %w", err) 266 // ESC pressed or rofi closed - return back
267 return SelectionOption{Key: "-2", Label: "Back"}, nil
248 } 268 }
249 269
250 selected := strings.TrimSpace(stdout.String()) 270 selected := strings.TrimSpace(stdout.String())
@@ -252,9 +272,12 @@ func DynamicSelectPreview(options map[string]RofiSelectPreview, addnewoption boo
252 // Handle special cases 272 // Handle special cases
253 switch selected { 273 switch selected {
254 case "": 274 case "":
255 return SelectionOption{}, fmt.Errorf("no selection made") 275 // Empty selection - go back
276 return SelectionOption{Key: "-2", Label: "Back"}, nil
256 case "Add new anime": 277 case "Add new anime":
257 return SelectionOption{Label: "Add new anime", Key: "add_new"}, nil 278 return SelectionOption{Label: "Add new anime", Key: "add_new"}, nil
279 case "Back":
280 return SelectionOption{Label: "Back", Key: "-2"}, nil
258 case "Quit": 281 case "Quit":
259 return SelectionOption{Label: "Quit", Key: "-1"}, nil 282 return SelectionOption{Label: "Quit", Key: "-1"}, nil
260 } 283 }
@@ -333,7 +356,7 @@ func showCachedImagePreview(imageURL string) error {
333 return nil 356 return nil
334} 357}
335 358
336func RofiSelect(options []SelectionOption) (SelectionOption, error) { 359func RofiSelect(options []SelectionOption, isHomeMenu bool) (SelectionOption, error) {
337 userCurdConfig := GetGlobalConfig() 360 userCurdConfig := GetGlobalConfig()
338 if userCurdConfig.StoragePath == "" { 361 if userCurdConfig.StoragePath == "" {
339 userCurdConfig.StoragePath = os.ExpandEnv("${HOME}/.local/share/curd") 362 userCurdConfig.StoragePath = os.ExpandEnv("${HOME}/.local/share/curd")
@@ -345,6 +368,10 @@ func RofiSelect(options []SelectionOption) (SelectionOption, error) {
345 optionsList = append(optionsList, opt.Label) 368 optionsList = append(optionsList, opt.Label)
346 } 369 }
347 370
371 // Add "Back" option before "Quit" (only for non-home menus)
372 if !isHomeMenu {
373 optionsList = append(optionsList, "Back")
374 }
348 // Add "Quit" option at the end 375 // Add "Quit" option at the end
349 optionsList = append(optionsList, "Quit") 376 optionsList = append(optionsList, "Quit")
350 optionsString := strings.Join(optionsList, "\n") 377 optionsString := strings.Join(optionsList, "\n")
@@ -360,7 +387,11 @@ func RofiSelect(options []SelectionOption) (SelectionOption, error) {
360 err := cmd.Run() 387 err := cmd.Run()
361 if err != nil { 388 if err != nil {
362 if exitError, ok := err.(*exec.ExitError); ok && exitError.ExitCode() == 1 { 389 if exitError, ok := err.(*exec.ExitError); ok && exitError.ExitCode() == 1 {
363 return SelectionOption{Key: "-1", Label: ""}, nil 390 // ESC pressed or rofi closed: go back on sub-menus, quit on home menu
391 if isHomeMenu {
392 return SelectionOption{Key: "-1", Label: "Quit"}, nil
393 }
394 return SelectionOption{Key: "-2", Label: "Back"}, nil
364 } 395 }
365 return SelectionOption{}, fmt.Errorf("failed to run Rofi: %v", err) 396 return SelectionOption{}, fmt.Errorf("failed to run Rofi: %v", err)
366 } 397 }
@@ -370,7 +401,13 @@ func RofiSelect(options []SelectionOption) (SelectionOption, error) {
370 // Handle special cases 401 // Handle special cases
371 switch selected { 402 switch selected {
372 case "": 403 case "":
373 return SelectionOption{Key: "-1", Label: ""}, nil 404 // Empty selection: go back on sub-menus, quit on home menu
405 if isHomeMenu {
406 return SelectionOption{Key: "-1", Label: "Quit"}, nil
407 }
408 return SelectionOption{Key: "-2", Label: "Back"}, nil
409 case "Back":
410 return SelectionOption{Label: "Back", Key: "-2"}, nil
374 case "Quit": 411 case "Quit":
375 return SelectionOption{Label: "Quit", Key: "-1"}, nil 412 return SelectionOption{Label: "Quit", Key: "-1"}, nil
376 } 413 }
@@ -386,12 +423,22 @@ func RofiSelect(options []SelectionOption) (SelectionOption, error) {
386} 423}
387 424
388func DynamicSelectFromSlice(options []SelectionOption) (SelectionOption, error) { 425func DynamicSelectFromSlice(options []SelectionOption) (SelectionOption, error) {
426 // Detect if this is a home menu by checking for category keys
427 isHomeMenu := false
428 for _, opt := range options {
429 if opt.Key == "ALL" || opt.Key == "CURRENT" {
430 isHomeMenu = true
431 break
432 }
433 }
434
389 if GetGlobalConfig().RofiSelection { 435 if GetGlobalConfig().RofiSelection {
390 return RofiSelect(options) 436 return RofiSelect(options, isHomeMenu)
391 } 437 }
392 438
393 model := &Model{ 439 model := &Model{
394 allOptions: options, 440 allOptions: options,
441 isHomeMenu: isHomeMenu,
395 } 442 }
396 model.filterOptions() // Initialize filtered options 443 model.filterOptions() // Initialize filtered options
397 444
@@ -419,32 +466,39 @@ func DynamicSelectFromSlice(options []SelectionOption) (SelectionOption, error)
419 466
420// DynamicSelect displays a simple selection prompt without extra features 467// DynamicSelect displays a simple selection prompt without extra features
421func DynamicSelect(options []SelectionOption) (SelectionOption, error) { 468func DynamicSelect(options []SelectionOption) (SelectionOption, error) {
469 // Detect if this is a home menu by checking for category keys
470 isHomeMenu := false
471 for _, opt := range options {
472 if opt.Key == "ALL" || opt.Key == "CURRENT" {
473 isHomeMenu = true
474 break
475 }
476 }
477
422 if GetGlobalConfig().RofiSelection { 478 if GetGlobalConfig().RofiSelection {
423 return RofiSelect(options) 479 return RofiSelect(options, isHomeMenu)
424 } 480 }
425 481
426 // Sort options if this contains menu categories 482 // Sort options if this contains menu categories
427 for _, opt := range options { 483 if isHomeMenu {
428 if opt.Key == "ALL" || opt.Key == "CURRENT" { 484 menuOrder := strings.Split(GetGlobalConfig().MenuOrder, ",")
429 menuOrder := strings.Split(GetGlobalConfig().MenuOrder, ",") 485 optMap := make(map[string]SelectionOption)
430 optMap := make(map[string]SelectionOption) 486 for _, opt := range options {
431 for _, opt := range options { 487 optMap[opt.Key] = opt
432 optMap[opt.Key] = opt 488 }
433 }
434 489
435 sorted := make([]SelectionOption, 0, len(options)) 490 sorted := make([]SelectionOption, 0, len(options))
436 for _, key := range menuOrder { 491 for _, key := range menuOrder {
437 if opt, exists := optMap[key]; exists { 492 if opt, exists := optMap[key]; exists {
438 sorted = append(sorted, opt) 493 sorted = append(sorted, opt)
439 }
440 } 494 }
441 options = sorted
442 break
443 } 495 }
496 options = sorted
444 } 497 }
445 498
446 model := &Model{ 499 model := &Model{
447 allOptions: options, 500 allOptions: options,
501 isHomeMenu: isHomeMenu,
448 } 502 }
449 model.filterOptions() // Initialize filtered options 503 model.filterOptions() // Initialize filtered options
450 504
@@ -472,3 +526,4 @@ func DynamicSelect(options []SelectionOption) (SelectionOption, error) {
472 } 526 }
473 return SelectionOption{}, nil 527 return SelectionOption{}, nil
474} 528}
529