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) {
218218 {Key: "SCORE", Label: "Add/Change Score"},
219219 }
220220
221 // Select update option
222 updateSelection, err := DynamicSelect(updateOptions)
223 if err != nil {
224 Log(fmt.Sprintf("Failed to select update option: %v", err))
225 ExitCurd(fmt.Errorf("Failed to select update option"))
226 }
221 // Navigation loop for update option selection
222updateOptionLoop:
223 for {
224 // Select update option
225 updateSelection, err := DynamicSelect(updateOptions)
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 }
227230
228 if updateSelection.Key == "-1" {
229 ExitCurd(nil)
230 }
231 if updateSelection.Key == "-1" {
232 ExitCurd(nil)
233 }
231234
232 // Get user's anime list
233 var animeListOptions []SelectionOption
234 var animeListMapPreview map[string]RofiSelectPreview
235 // Back from update selection returns to home menu
236 if updateSelection.Key == "-2" {
237 return // Return to caller (home menu)
238 }
235239
236 if userCurdConfig.RofiSelection && userCurdConfig.ImagePreview {
237 animeListMapPreview = make(map[string]RofiSelectPreview)
238 // Include anime from all categories
239 for _, entry := range user.AnimeList.Watching {
240 title := entry.Media.Title.English
241 if title == "" || userCurdConfig.AnimeNameLanguage == "romaji" {
242 title = entry.Media.Title.Romaji
243 }
244 animeListMapPreview[strconv.Itoa(entry.Media.ID)] = RofiSelectPreview{
245 Title: title,
246 CoverImage: entry.CoverImage,
240 // Get user's anime list
241 var animeListOptions []SelectionOption
242 var animeListMapPreview map[string]RofiSelectPreview
243
244 if userCurdConfig.RofiSelection && userCurdConfig.ImagePreview {
245 animeListMapPreview = make(map[string]RofiSelectPreview)
246 // Include anime from all categories
247 for _, entry := range user.AnimeList.Watching {
248 title := entry.Media.Title.English
249 if title == "" || userCurdConfig.AnimeNameLanguage == "romaji" {
250 title = entry.Media.Title.Romaji
251 }
252 animeListMapPreview[strconv.Itoa(entry.Media.ID)] = RofiSelectPreview{
253 Title: title,
254 CoverImage: entry.CoverImage,
255 }
247256 }
248 }
249 for _, entry := range user.AnimeList.Completed {
250 title := entry.Media.Title.English
251 if title == "" || userCurdConfig.AnimeNameLanguage == "romaji" {
252 title = entry.Media.Title.Romaji
257 for _, entry := range user.AnimeList.Completed {
258 title := entry.Media.Title.English
259 if title == "" || userCurdConfig.AnimeNameLanguage == "romaji" {
260 title = entry.Media.Title.Romaji
261 }
262 animeListMapPreview[strconv.Itoa(entry.Media.ID)] = RofiSelectPreview{
263 Title: title,
264 CoverImage: entry.CoverImage,
265 }
253266 }
254 animeListMapPreview[strconv.Itoa(entry.Media.ID)] = RofiSelectPreview{
255 Title: title,
256 CoverImage: entry.CoverImage,
267 for _, entry := range user.AnimeList.Paused {
268 title := entry.Media.Title.English
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 }
257276 }
258 }
259 for _, entry := range user.AnimeList.Paused {
260 title := entry.Media.Title.English
261 if title == "" || userCurdConfig.AnimeNameLanguage == "romaji" {
262 title = entry.Media.Title.Romaji
277 for _, entry := range user.AnimeList.Dropped {
278 title := entry.Media.Title.English
279 if title == "" || userCurdConfig.AnimeNameLanguage == "romaji" {
280 title = entry.Media.Title.Romaji
281 }
282 animeListMapPreview[strconv.Itoa(entry.Media.ID)] = RofiSelectPreview{
283 Title: title,
284 CoverImage: entry.CoverImage,
285 }
263286 }
264 animeListMapPreview[strconv.Itoa(entry.Media.ID)] = RofiSelectPreview{
265 Title: title,
266 CoverImage: entry.CoverImage,
287 for _, entry := range user.AnimeList.Planning {
288 title := entry.Media.Title.English
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 }
267296 }
268 }
269 for _, entry := range user.AnimeList.Dropped {
270 title := entry.Media.Title.English
271 if title == "" || userCurdConfig.AnimeNameLanguage == "romaji" {
272 title = entry.Media.Title.Romaji
297 for _, entry := range user.AnimeList.Rewatching {
298 title := entry.Media.Title.English
299 if title == "" || userCurdConfig.AnimeNameLanguage == "romaji" {
300 title = entry.Media.Title.Romaji
301 }
302 animeListMapPreview[strconv.Itoa(entry.Media.ID)] = RofiSelectPreview{
303 Title: title,
304 CoverImage: entry.CoverImage,
305 }
273306 }
274 animeListMapPreview[strconv.Itoa(entry.Media.ID)] = RofiSelectPreview{
275 Title: title,
276 CoverImage: entry.CoverImage,
307 } else {
308 animeListOptions = make([]SelectionOption, 0)
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 })
277319 }
278 }
279 for _, entry := range user.AnimeList.Planning {
280 title := entry.Media.Title.English
281 if title == "" || userCurdConfig.AnimeNameLanguage == "romaji" {
282 title = entry.Media.Title.Romaji
320 for _, entry := range user.AnimeList.Completed {
321 title := entry.Media.Title.English
322 if title == "" || userCurdConfig.AnimeNameLanguage == "romaji" {
323 title = entry.Media.Title.Romaji
324 }
325 animeListOptions = append(animeListOptions, SelectionOption{
326 Key: strconv.Itoa(entry.Media.ID),
327 Label: title,
328 })
283329 }
284 animeListMapPreview[strconv.Itoa(entry.Media.ID)] = RofiSelectPreview{
285 Title: title,
286 CoverImage: entry.CoverImage,
330 for _, entry := range user.AnimeList.Paused {
331 title := entry.Media.Title.English
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 })
287339 }
288 }
289 for _, entry := range user.AnimeList.Rewatching {
290 title := entry.Media.Title.English
291 if title == "" || userCurdConfig.AnimeNameLanguage == "romaji" {
292 title = entry.Media.Title.Romaji
340 for _, entry := range user.AnimeList.Dropped {
341 title := entry.Media.Title.English
342 if title == "" || userCurdConfig.AnimeNameLanguage == "romaji" {
343 title = entry.Media.Title.Romaji
344 }
345 animeListOptions = append(animeListOptions, SelectionOption{
346 Key: strconv.Itoa(entry.Media.ID),
347 Label: title,
348 })
293349 }
294 animeListMapPreview[strconv.Itoa(entry.Media.ID)] = RofiSelectPreview{
295 Title: title,
296 CoverImage: entry.CoverImage,
350 for _, entry := range user.AnimeList.Planning {
351 title := entry.Media.Title.English
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 })
297359 }
298 }
299 } else {
300 animeListOptions = make([]SelectionOption, 0)
301 // Include anime from all categories
302 for _, entry := range user.AnimeList.Watching {
303 title := entry.Media.Title.English
304 if title == "" || userCurdConfig.AnimeNameLanguage == "romaji" {
305 title = entry.Media.Title.Romaji
360 for _, entry := range user.AnimeList.Rewatching {
361 title := entry.Media.Title.English
362 if title == "" || userCurdConfig.AnimeNameLanguage == "romaji" {
363 title = entry.Media.Title.Romaji
364 }
365 animeListOptions = append(animeListOptions, SelectionOption{
366 Key: strconv.Itoa(entry.Media.ID),
367 Label: title,
368 })
306369 }
307 animeListOptions = append(animeListOptions, SelectionOption{
308 Key: strconv.Itoa(entry.Media.ID),
309 Label: title,
310 })
311370 }
312 for _, entry := range user.AnimeList.Completed {
313 title := entry.Media.Title.English
314 if title == "" || userCurdConfig.AnimeNameLanguage == "romaji" {
315 title = entry.Media.Title.Romaji
371
372 // Anime selection loop
373 animeSelectLoop:
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)
316381 }
317 animeListOptions = append(animeListOptions, SelectionOption{
318 Key: strconv.Itoa(entry.Media.ID),
319 Label: title,
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
382 if err != nil {
383 Log(fmt.Sprintf("Failed to select anime: %v", err))
384 ExitCurd(fmt.Errorf("Failed to select anime"))
326385 }
327 animeListOptions = append(animeListOptions, SelectionOption{
328 Key: strconv.Itoa(entry.Media.ID),
329 Label: title,
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
386
387 if selectedAnime.Key == "-1" {
388 ExitCurd(nil)
336389 }
337 animeListOptions = append(animeListOptions, SelectionOption{
338 Key: strconv.Itoa(entry.Media.ID),
339 Label: title,
340 })
341 }
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
390
391 // Back from anime selection goes to update option selection
392 if selectedAnime.Key == "-2" {
393 ClearScreen()
394 continue updateOptionLoop
346395 }
347 animeListOptions = append(animeListOptions, SelectionOption{
348 Key: strconv.Itoa(entry.Media.ID),
349 Label: title,
350 })
351 }
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
396
397 animeID, err := strconv.Atoi(selectedAnime.Key)
398 if err != nil {
399 Log(fmt.Sprintf("Failed to convert anime ID: %v", err))
400 ExitCurd(fmt.Errorf("Failed to convert anime ID"))
356401 }
357 animeListOptions = append(animeListOptions, SelectionOption{
358 Key: strconv.Itoa(entry.Media.ID),
359 Label: title,
360 })
361 }
362 }
363402
364 // Select anime to update
365 var selectedAnime SelectionOption
366 if userCurdConfig.RofiSelection && userCurdConfig.ImagePreview {
367 selectedAnime, err = DynamicSelectPreview(animeListMapPreview, false)
368 } else {
369 selectedAnime, err = DynamicSelect(animeListOptions)
370 }
371 if err != nil {
372 Log(fmt.Sprintf("Failed to select anime: %v", err))
373 ExitCurd(fmt.Errorf("Failed to select anime"))
374 }
403 // After getting animeID, get the current anime entry
404 selectedAnilistAnime, err := FindAnimeByAnilistID(user.AnimeList, selectedAnime.Key)
405 if err != nil {
406 Log(fmt.Sprintf("Can not find the anime in anilist animelist: %v", err))
407 ExitCurd(fmt.Errorf("Can not find the anime in anilist animelist"))
408 }
409 ClearScreen()
375410
376 if selectedAnime.Key == "-1" {
377 ExitCurd(nil)
378 }
411 // Final selection loop (for category/progress/score)
412 for {
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 }
379423
380 animeID, err := strconv.Atoi(selectedAnime.Key)
381 if err != nil {
382 Log(fmt.Sprintf("Failed to convert anime ID: %v", err))
383 ExitCurd(fmt.Errorf("Failed to convert anime ID"))
384 }
424 currentStatus := "None"
425 if selectedAnilistAnime.Status != "" {
426 // Find the label for the current status
427 for _, cat := range categories {
428 if cat.Key == selectedAnilistAnime.Status {
429 currentStatus = cat.Label
430 break
431 }
432 }
433 }
434 CurdOut(fmt.Sprintf("Current category: %s", currentStatus))
385435
386 // After getting animeID, get the current anime entry
387 selectedAnilistAnime, err := FindAnimeByAnilistID(user.AnimeList, selectedAnime.Key)
388 if err != nil {
389 Log(fmt.Sprintf("Can not find the anime in anilist animelist: %v", err))
390 ExitCurd(fmt.Errorf("Can not find the anime in anilist animelist"))
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 }
436 categorySelection, err := DynamicSelect(categories)
437 if err != nil {
438 Log(fmt.Sprintf("Failed to select category: %v", err))
439 ExitCurd(fmt.Errorf("Failed to select category"))
440 }
403441
404 currentStatus := "None"
405 if selectedAnilistAnime.Status != "" {
406 // Find the label for the current status
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))
442 if categorySelection.Key == "-1" {
443 ExitCurd(nil)
444 }
415445
416 categorySelection, err := DynamicSelect(categories)
417 if err != nil {
418 Log(fmt.Sprintf("Failed to select category: %v", err))
419 ExitCurd(fmt.Errorf("Failed to select category"))
420 }
446 // Back from category selection goes to anime selection
447 if categorySelection.Key == "-2" {
448 ClearScreen()
449 continue animeSelectLoop
450 }
421451
422 if categorySelection.Key == "-1" {
423 ExitCurd(nil)
424 }
452 err = UpdateAnimeStatus(user.Token, animeID, categorySelection.Key)
453 if err != nil {
454 Log(fmt.Sprintf("Failed to update anime status: %v", err))
455 ExitCurd(fmt.Errorf("Failed to update anime status"))
456 }
425457
426 err = UpdateAnimeStatus(user.Token, animeID, categorySelection.Key)
427 if err != nil {
428 Log(fmt.Sprintf("Failed to update anime status: %v", err))
429 ExitCurd(fmt.Errorf("Failed to update anime status"))
430 }
458 case "PROGRESS":
459 currentProgress := "None"
460 if selectedAnilistAnime.Progress > 0 {
461 currentProgress = strconv.Itoa(selectedAnilistAnime.Progress)
462 }
431463
432 case "PROGRESS":
433 currentProgress := "None"
434 if selectedAnilistAnime.Progress > 0 {
435 currentProgress = strconv.Itoa(selectedAnilistAnime.Progress)
436 }
464 var progress string
465 if userCurdConfig.RofiSelection {
466 progress, err = GetUserInputFromRofi(fmt.Sprintf("Current progress: %s\nEnter new progress (episode number)", currentProgress))
467 if err != nil {
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 }
437476
438 var progress string
439 if userCurdConfig.RofiSelection {
440 progress, err = GetUserInputFromRofi(fmt.Sprintf("Current progress: %s\nEnter new progress (episode number)", currentProgress))
441 if err != nil {
442 Log(fmt.Sprintf("Failed to get progress input: %v", err))
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 }
477 progressNum, err := strconv.Atoi(progress)
478 if err != nil {
479 Log(fmt.Sprintf("Failed to convert progress to number: %v", err))
480 ExitCurd(fmt.Errorf("Failed to convert progress to number"))
481 }
450482
451 progressNum, err := strconv.Atoi(progress)
452 if err != nil {
453 Log(fmt.Sprintf("Failed to convert progress to number: %v", err))
454 ExitCurd(fmt.Errorf("Failed to convert progress to number"))
455 }
483 err = UpdateAnimeProgress(user.Token, animeID, progressNum)
484 if err != nil {
485 Log(fmt.Sprintf("Failed to update anime progress: %v", err))
486 ExitCurd(fmt.Errorf("Failed to update anime progress"))
487 }
456488
457 err = UpdateAnimeProgress(user.Token, animeID, progressNum)
458 if err != nil {
459 Log(fmt.Sprintf("Failed to update anime progress: %v", err))
460 ExitCurd(fmt.Errorf("Failed to update anime progress"))
461 }
489 case "SCORE":
490 currentScore := "None"
491 if selectedAnilistAnime.Score > 0 {
492 currentScore = strconv.Itoa(int(selectedAnilistAnime.Score))
493 }
494 CurdOut(fmt.Sprintf("Current score: %s", currentScore))
462495
463 case "SCORE":
464 currentScore := "None"
465 if selectedAnilistAnime.Score > 0 {
466 currentScore = strconv.Itoa(int(selectedAnilistAnime.Score))
467 }
468 CurdOut(fmt.Sprintf("Current score: %s", currentScore))
496 err = RateAnime(user.Token, animeID)
497 if err != nil {
498 Log(fmt.Sprintf("Failed to update anime score: %v", err))
499 ExitCurd(fmt.Errorf("Failed to update anime score"))
500 }
501 }
469502
470 err = RateAnime(user.Token, animeID)
471 if err != nil {
472 Log(fmt.Sprintf("Failed to update anime score: %v", err))
473 ExitCurd(fmt.Errorf("Failed to update anime score"))
503 CurdOut("Anime updated successfully!")
504 return
505 }
474506 }
475507 }
476
477 CurdOut("Anime updated successfully!")
478508}
479509
480510func UpdateCurd(repo, fileName string) error {
@@ -714,6 +744,12 @@ func SetupCurd(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAni
714744 user.AnimeList = ParseAnimeList(anilistUserData)
715745 }
716746
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
717753 // If continueLast flag is set, directly get the last watched anime
718754 if anime.Ep.ContinueLast {
719755 // Get the last anime ID from the curd_id file
@@ -738,6 +774,7 @@ func SetupCurd(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAni
738774
739775 // Set the anime details
740776 anime.AnilistId = animePointer.AnilistId
777 anilistSelectedOption.Key = strconv.Itoa(animePointer.AnilistId)
741778 // anime.AllanimeId = animePointer.AllanimeId
742779 // anime.Title = animePointer.Title
743780 // anime.Ep.Number = animePointer.Ep.Number
@@ -745,126 +782,157 @@ func SetupCurd(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAni
745782 // anime.Ep.Resume = true
746783
747784 } else {
748 // Skip category selection if Current flag is set
749 var categorySelection SelectionOption
750 if userCurdConfig.CurrentCategory {
751 categorySelection = SelectionOption{
752 Key: "CURRENT",
753 Label: "Currently Watching",
754 }
755 } else {
756 // Create category selection map
757 // Get ordered categories
758 orderedCategories := getOrderedCategories(userCurdConfig)
785 // Navigation loop for category and anime selection
786 categorySelectionLoop:
787 for {
788 // Skip category selection if Current flag is set
789 var categorySelection SelectionOption
790 if userCurdConfig.CurrentCategory {
791 categorySelection = SelectionOption{
792 Key: "CURRENT",
793 Label: "Currently Watching",
794 }
795 } else {
796 // Create category selection map
797 // Get ordered categories
798 orderedCategories := getOrderedCategories(userCurdConfig)
759799
760 // Use DynamicSelect with ordered categories directly
761 categorySelection, err = DynamicSelect(orderedCategories)
800 // Use DynamicSelect with ordered categories directly
801 categorySelection, err = DynamicSelect(orderedCategories)
762802
763 if err != nil {
764 Log(fmt.Sprintf("Failed to select category: %v", err))
765 ExitCurd(fmt.Errorf("Failed to select category"))
766 }
803 if err != nil {
804 Log(fmt.Sprintf("Failed to select category: %v", err))
805 ExitCurd(fmt.Errorf("Failed to select category"))
806 }
767807
768 if categorySelection.Key == "-1" {
769 ExitCurd(nil)
770 }
808 if categorySelection.Key == "-1" {
809 ExitCurd(nil)
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 }
771828
772 // Handle options
773 if categorySelection.Key == "UPDATE" {
774 ClearScreen()
775 UpdateAnimeEntry(userCurdConfig, user)
776 ExitCurd(nil)
777 } else if categorySelection.Key == "UNTRACKED" {
778829 ClearScreen()
779 WatchUntracked(userCurdConfig)
780 } else if categorySelection.Key == "CONTINUE_LAST" {
781 anime.Ep.ContinueLast = true
782830 }
783831
784 ClearScreen()
785 }
786
787 if userCurdConfig.RofiSelection && userCurdConfig.ImagePreview {
788 animeListMapPreview = make(map[string]RofiSelectPreview)
789 for _, entry := range getEntriesByCategory(user.AnimeList, categorySelection.Key) {
790 title := entry.Media.Title.English
791 if title == "" || userCurdConfig.AnimeNameLanguage == "romaji" {
792 title = entry.Media.Title.Romaji
793 }
794 animeListMapPreview[strconv.Itoa(entry.Media.ID)] = RofiSelectPreview{
795 Title: title,
796 CoverImage: entry.CoverImage,
832 if userCurdConfig.RofiSelection && userCurdConfig.ImagePreview {
833 animeListMapPreview = make(map[string]RofiSelectPreview)
834 for _, entry := range getEntriesByCategory(user.AnimeList, categorySelection.Key) {
835 title := entry.Media.Title.English
836 if title == "" || userCurdConfig.AnimeNameLanguage == "romaji" {
837 title = entry.Media.Title.Romaji
838 }
839 animeListMapPreview[strconv.Itoa(entry.Media.ID)] = RofiSelectPreview{
840 Title: title,
841 CoverImage: entry.CoverImage,
842 }
797843 }
798 }
799 } else {
800 animeListOptions = make([]SelectionOption, 0)
801 for _, entry := range getEntriesByCategory(user.AnimeList, categorySelection.Key) {
802 title := entry.Media.Title.English
803 if title == "" || userCurdConfig.AnimeNameLanguage == "romaji" {
804 title = entry.Media.Title.Romaji
844 } else {
845 animeListOptions = make([]SelectionOption, 0)
846 for _, entry := range getEntriesByCategory(user.AnimeList, categorySelection.Key) {
847 title := entry.Media.Title.English
848 if title == "" || userCurdConfig.AnimeNameLanguage == "romaji" {
849 title = entry.Media.Title.Romaji
850 }
851 animeListOptions = append(animeListOptions, SelectionOption{
852 Key: strconv.Itoa(entry.Media.ID),
853 Label: title,
854 })
805855 }
806 animeListOptions = append(animeListOptions, SelectionOption{
807 Key: strconv.Itoa(entry.Media.ID),
808 Label: title,
809 })
810856 }
811 }
812 }
813857
814 var anilistSelectedOption SelectionOption
815 var selectedAllanimeAnime SelectionOption
816 var userQuery string
858 // Anime selection loop (for back navigation)
859 animeSelectionLoop:
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 }
817869
818 if anime.Ep.ContinueLast {
819 // Get the last watched anime ID from the curd_id file
820 curdIDPath := filepath.Join(os.ExpandEnv(userCurdConfig.StoragePath), "curd_id")
821 curdIDBytes, err := os.ReadFile(curdIDPath)
822 if err != nil {
823 Log(fmt.Sprintf("Error reading curd_id file: %v", err))
824 ExitCurd(fmt.Errorf("Error reading curd_id file"))
825 }
870 lastWatchedID, err := strconv.Atoi(strings.TrimSpace(string(curdIDBytes)))
871 if err != nil {
872 Log(fmt.Sprintf("Error converting curd_id to integer: %v", err))
873 ExitCurd(fmt.Errorf("Error converting curd_id to integer"))
874 }
826875
827 lastWatchedID, err := strconv.Atoi(strings.TrimSpace(string(curdIDBytes)))
828 if err != nil {
829 Log(fmt.Sprintf("Error converting curd_id to integer: %v", err))
830 ExitCurd(fmt.Errorf("Error converting curd_id to integer"))
831 }
876 anime.AnilistId = lastWatchedID
877 anilistSelectedOption.Key = strconv.Itoa(lastWatchedID)
878 break categorySelectionLoop
879 }
832880
833 anime.AnilistId = lastWatchedID
834 anilistSelectedOption.Key = strconv.Itoa(lastWatchedID)
835 } else {
836 // Select anime to watch (Anilist)
837 var err error
838 if userCurdConfig.RofiSelection && userCurdConfig.ImagePreview {
839 anilistSelectedOption, err = DynamicSelectPreview(animeListMapPreview, true)
840 } else {
841 // Add "Add new anime" option to the slice
842 animeListOptions = append(animeListOptions, SelectionOption{
843 Key: "add_new",
844 Label: "Add new anime",
845 })
881 // Select anime to watch (Anilist)
882 var err error
883 if userCurdConfig.RofiSelection && userCurdConfig.ImagePreview {
884 anilistSelectedOption, err = DynamicSelectPreview(animeListMapPreview, true)
885 } else {
886 // Add "Add new anime" option to the slice
887 tempOptions := make([]SelectionOption, len(animeListOptions))
888 copy(tempOptions, animeListOptions)
889 tempOptions = append(tempOptions, SelectionOption{
890 Key: "add_new",
891 Label: "Add new anime",
892 })
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 }
846900
847 anilistSelectedOption, err = DynamicSelect(animeListOptions)
848 }
849 if err != nil {
850 Log(fmt.Sprintf("Error selecting anime: %v", err))
851 ExitCurd(fmt.Errorf("Error selecting anime"))
852 }
901 Log(anilistSelectedOption)
853902
854 Log(anilistSelectedOption)
903 if anilistSelectedOption.Key == "-1" {
904 ExitCurd(nil)
905 }
855906
856 if anilistSelectedOption.Key == "-1" {
857 ExitCurd(nil)
858 }
907 // Handle back navigation - go back to category selection
908 if anilistSelectedOption.Key == "-2" {
909 if userCurdConfig.CurrentCategory {
910 // If CurrentCategory is forced, back means quit
911 ExitCurd(nil)
912 }
913 ClearScreen()
914 continue categorySelectionLoop
915 }
859916
860 if anilistSelectedOption.Label == "add_new" || anilistSelectedOption.Key == "add_new" {
861 anilistSelectedOption = AddNewAnime(userCurdConfig, anime, user, databaseAnimes)
862 }
917 if anilistSelectedOption.Label == "add_new" || anilistSelectedOption.Key == "add_new" {
918 addResult := AddNewAnime(userCurdConfig, anime, user, databaseAnimes)
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 }
863926
864 anime.AnilistId, err = strconv.Atoi(anilistSelectedOption.Key)
865 if err != nil {
866 Log(fmt.Sprintf("Error converting Anilist ID: %v", err))
867 ExitCurd(fmt.Errorf("Error converting Anilist ID"))
927 anime.AnilistId, err = strconv.Atoi(anilistSelectedOption.Key)
928 if err != nil {
929 Log(fmt.Sprintf("Error converting Anilist ID: %v", err))
930 ExitCurd(fmt.Errorf("Error converting Anilist ID"))
931 }
932
933 // Successfully selected anime, break out of both loops
934 break categorySelectionLoop
935 }
868936 }
869937 }
870938 // Find anime in Local history
@@ -1579,3 +1647,4 @@ func HandleLastEpisodeCompletion(userCurdConfig *CurdConfig, anime *Anime, userT
15791647 }
15801648 }
15811649}
1650
internal/localTracking.go +40 −30
@@ -254,43 +254,52 @@ func WatchUntracked(userCurdConfig *CurdConfig) {
254254 var err error
255255 var anime Anime
256256
257 // Get anime name from user
258 if userCurdConfig.RofiSelection {
259 userInput, err := GetUserInputFromRofi("Enter the anime name")
257 // Anime search and selection loop
258 for {
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)
260274 if err != nil {
261 Log("Error getting user input: " + err.Error())
262 ExitCurd(fmt.Errorf("Error getting user input: " + err.Error()))
275 Log(fmt.Sprintf("Failed to search anime: %v", err))
276 ExitCurd(fmt.Errorf("Failed to search anime"))
263277 }
264 query = userInput
265 } else {
266 CurdOut("Enter the anime name:")
267 fmt.Scanln(&query)
268 }
269278
270 // Search for the anime
271 animeList, err = SearchAnime(query, userCurdConfig.SubOrDub)
272 if err != nil {
273 Log(fmt.Sprintf("Failed to search anime: %v", err))
274 ExitCurd(fmt.Errorf("Failed to search anime"))
275 }
279 if len(animeList) == 0 {
280 ExitCurd(fmt.Errorf("No results found."))
281 }
276282
277 if len(animeList) == 0 {
278 ExitCurd(fmt.Errorf("No results found."))
279 }
283 // Select anime from search results
284 selectedAnime, err := DynamicSelect(animeList)
285 if err != nil {
286 Log(fmt.Sprintf("Failed to select anime: %v", err))
287 ExitCurd(fmt.Errorf("Failed to select anime"))
288 }
280289
281 // Select anime from search results
282 selectedAnime, err := DynamicSelect(animeList)
283 if err != nil {
284 Log(fmt.Sprintf("Failed to select anime: %v", err))
285 ExitCurd(fmt.Errorf("Failed to select anime"))
286 }
290 if selectedAnime.Key == "-1" {
291 ExitCurd(nil)
292 }
287293
288 if selectedAnime.Key == "-1" {
289 ExitCurd(nil)
290 }
294 // Back goes to home menu
295 if selectedAnime.Key == "-2" {
296 return // Return to caller (home menu)
297 }
291298
292 anime.AllanimeId = selectedAnime.Key
293 anime.Title.English = selectedAnime.Label
299 anime.AllanimeId = selectedAnime.Key
300 anime.Title.English = selectedAnime.Label
301 break
302 }
294303
295304 // Get episode number
296305 var episodeNumber int
@@ -413,3 +422,4 @@ func WatchUntracked(userCurdConfig *CurdConfig) {
413422 }
414423
415424}
425
internal/selection_menu.go +76 −21
@@ -27,6 +27,7 @@ type Model struct {
2727 terminalHeight int
2828 scrollOffset int
2929 addNewOption bool
30 isHomeMenu bool // If true, ESC quits; if false, ESC goes back
3031}
3132
3233var (
@@ -83,6 +84,18 @@ func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
8384 case "ctrl+c":
8485 // Return quit selection option directly
8586 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
8699 case "backspace":
87100 if len(m.filter) > 0 {
88101 m.filter = m.filter[:len(m.filter)-1]
@@ -200,6 +213,10 @@ func (m *Model) filterOptions() {
200213 if m.addNewOption {
201214 m.filteredKeys = append(m.filteredKeys, SelectionOption{Label: "Add new anime", Key: "add_new"})
202215 }
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 }
203220 m.filteredKeys = append(m.filteredKeys, SelectionOption{Label: "Quit", Key: "-1"})
204221}
205222
@@ -231,6 +248,8 @@ func DynamicSelectPreview(options map[string]RofiSelectPreview, addnewoption boo
231248 if addnewoption {
232249 rofiInput.WriteString("Add new anime\n")
233250 }
251 // Add Back option before Quit (this is always a sub-menu when using preview)
252 rofiInput.WriteString("Back\n")
234253 rofiInput.WriteString("Quit\n")
235254
236255 // Run rofi
@@ -244,7 +263,8 @@ func DynamicSelectPreview(options map[string]RofiSelectPreview, addnewoption boo
244263 if err := cmd.Run(); err != nil {
245264 Log(fmt.Sprintf("Rofi stderr: %s", stderr.String()))
246265 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
248268 }
249269
250270 selected := strings.TrimSpace(stdout.String())
@@ -252,9 +272,12 @@ func DynamicSelectPreview(options map[string]RofiSelectPreview, addnewoption boo
252272 // Handle special cases
253273 switch selected {
254274 case "":
255 return SelectionOption{}, fmt.Errorf("no selection made")
275 // Empty selection - go back
276 return SelectionOption{Key: "-2", Label: "Back"}, nil
256277 case "Add new anime":
257278 return SelectionOption{Label: "Add new anime", Key: "add_new"}, nil
279 case "Back":
280 return SelectionOption{Label: "Back", Key: "-2"}, nil
258281 case "Quit":
259282 return SelectionOption{Label: "Quit", Key: "-1"}, nil
260283 }
@@ -333,7 +356,7 @@ func showCachedImagePreview(imageURL string) error {
333356 return nil
334357}
335358
336func RofiSelect(options []SelectionOption) (SelectionOption, error) {
359func RofiSelect(options []SelectionOption, isHomeMenu bool) (SelectionOption, error) {
337360 userCurdConfig := GetGlobalConfig()
338361 if userCurdConfig.StoragePath == "" {
339362 userCurdConfig.StoragePath = os.ExpandEnv("${HOME}/.local/share/curd")
@@ -345,6 +368,10 @@ func RofiSelect(options []SelectionOption) (SelectionOption, error) {
345368 optionsList = append(optionsList, opt.Label)
346369 }
347370
371 // Add "Back" option before "Quit" (only for non-home menus)
372 if !isHomeMenu {
373 optionsList = append(optionsList, "Back")
374 }
348375 // Add "Quit" option at the end
349376 optionsList = append(optionsList, "Quit")
350377 optionsString := strings.Join(optionsList, "\n")
@@ -360,7 +387,11 @@ func RofiSelect(options []SelectionOption) (SelectionOption, error) {
360387 err := cmd.Run()
361388 if err != nil {
362389 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
364395 }
365396 return SelectionOption{}, fmt.Errorf("failed to run Rofi: %v", err)
366397 }
@@ -370,7 +401,13 @@ func RofiSelect(options []SelectionOption) (SelectionOption, error) {
370401 // Handle special cases
371402 switch selected {
372403 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
374411 case "Quit":
375412 return SelectionOption{Label: "Quit", Key: "-1"}, nil
376413 }
@@ -386,12 +423,22 @@ func RofiSelect(options []SelectionOption) (SelectionOption, error) {
386423}
387424
388425func 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
389435 if GetGlobalConfig().RofiSelection {
390 return RofiSelect(options)
436 return RofiSelect(options, isHomeMenu)
391437 }
392438
393439 model := &Model{
394440 allOptions: options,
441 isHomeMenu: isHomeMenu,
395442 }
396443 model.filterOptions() // Initialize filtered options
397444
@@ -419,32 +466,39 @@ func DynamicSelectFromSlice(options []SelectionOption) (SelectionOption, error)
419466
420467// DynamicSelect displays a simple selection prompt without extra features
421468func 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
422478 if GetGlobalConfig().RofiSelection {
423 return RofiSelect(options)
479 return RofiSelect(options, isHomeMenu)
424480 }
425481
426482 // Sort options if this contains menu categories
427 for _, opt := range options {
428 if opt.Key == "ALL" || opt.Key == "CURRENT" {
429 menuOrder := strings.Split(GetGlobalConfig().MenuOrder, ",")
430 optMap := make(map[string]SelectionOption)
431 for _, opt := range options {
432 optMap[opt.Key] = opt
433 }
483 if isHomeMenu {
484 menuOrder := strings.Split(GetGlobalConfig().MenuOrder, ",")
485 optMap := make(map[string]SelectionOption)
486 for _, opt := range options {
487 optMap[opt.Key] = opt
488 }
434489
435 sorted := make([]SelectionOption, 0, len(options))
436 for _, key := range menuOrder {
437 if opt, exists := optMap[key]; exists {
438 sorted = append(sorted, opt)
439 }
490 sorted := make([]SelectionOption, 0, len(options))
491 for _, key := range menuOrder {
492 if opt, exists := optMap[key]; exists {
493 sorted = append(sorted, opt)
440494 }
441 options = sorted
442 break
443495 }
496 options = sorted
444497 }
445498
446499 model := &Model{
447500 allOptions: options,
501 isHomeMenu: isHomeMenu,
448502 }
449503 model.filterOptions() // Initialize filtered options
450504
@@ -472,3 +526,4 @@ func DynamicSelect(options []SelectionOption) (SelectionOption, error) {
472526 }
473527 return SelectionOption{}, nil
474528}
529