Commit 5a8dd93470

5a8dd934702a28cadbdd855822c40e7cbc914139

parent: b08ed0902f

Unsigned

Wraient <wraient.911@gmail.com> · 2025-02-26 21:32 UTC

Chore: Update DynamicSelect function to remove bloated arg

Layout: unified · split

cmd/curd/main.go +1 −1
@@ -596,7 +596,7 @@ func main() {
596 if userCurdConfig.NextEpisodePrompt { 596 if userCurdConfig.NextEpisodePrompt {
597 internal.CurdOut(fmt.Sprintf("Start next episode (%d)?", anime.Ep.Number)) 597 internal.CurdOut(fmt.Sprintf("Start next episode (%d)?", anime.Ep.Number))
598 598
599 selectedOption, err := internal.DynamicSelect(map[string]string{"yes": "Yes"}, false) 599 selectedOption, err := internal.DynamicSelect(map[string]string{"yes": "Yes"})
600 if err != nil { 600 if err != nil {
601 internal.ExitCurd(err) 601 internal.ExitCurd(err)
602 } 602 }
internal/curd.go +12 −9
@@ -230,7 +230,7 @@ func UpdateAnimeEntry(userCurdConfig *CurdConfig, user *User) {
230 } 230 }
231 231
232 // Select update option 232 // Select update option
233 updateSelection, err := DynamicSelect(updateOptions, false) 233 updateSelection, err := DynamicSelect(updateOptions)
234 if err != nil { 234 if err != nil {
235 Log(fmt.Sprintf("Failed to select update option: %v", err)) 235 Log(fmt.Sprintf("Failed to select update option: %v", err))
236 ExitCurd(fmt.Errorf("Failed to select update option")) 236 ExitCurd(fmt.Errorf("Failed to select update option"))
@@ -342,7 +342,7 @@ func UpdateAnimeEntry(userCurdConfig *CurdConfig, user *User) {
342 if userCurdConfig.RofiSelection && userCurdConfig.ImagePreview { 342 if userCurdConfig.RofiSelection && userCurdConfig.ImagePreview {
343 selectedAnime, err = DynamicSelectPreview(animeListMapPreview, false) 343 selectedAnime, err = DynamicSelectPreview(animeListMapPreview, false)
344 } else { 344 } else {
345 selectedAnime, err = DynamicSelect(animeListMap, false) 345 selectedAnime, err = DynamicSelect(animeListMap)
346 } 346 }
347 if err != nil { 347 if err != nil {
348 Log(fmt.Sprintf("Failed to select anime: %v", err)) 348 Log(fmt.Sprintf("Failed to select anime: %v", err))
@@ -382,7 +382,7 @@ func UpdateAnimeEntry(userCurdConfig *CurdConfig, user *User) {
382 } 382 }
383 CurdOut(fmt.Sprintf("Current category: %s", currentStatus)) 383 CurdOut(fmt.Sprintf("Current category: %s", currentStatus))
384 384
385 categorySelection, err := DynamicSelect(categories, false) 385 categorySelection, err := DynamicSelect(categories)
386 if err != nil { 386 if err != nil {
387 Log(fmt.Sprintf("Failed to select category: %v", err)) 387 Log(fmt.Sprintf("Failed to select category: %v", err))
388 ExitCurd(fmt.Errorf("Failed to select category")) 388 ExitCurd(fmt.Errorf("Failed to select category"))
@@ -577,7 +577,7 @@ func AddNewAnime(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseA
577 if userCurdConfig.RofiSelection && userCurdConfig.ImagePreview { 577 if userCurdConfig.RofiSelection && userCurdConfig.ImagePreview {
578 anilistSelectedOption, err = DynamicSelectPreview(animeMapPreview, false) 578 anilistSelectedOption, err = DynamicSelectPreview(animeMapPreview, false)
579 } else { 579 } else {
580 anilistSelectedOption, err = DynamicSelect(animeMap, false) 580 anilistSelectedOption, err = DynamicSelect(animeMap)
581 } 581 }
582 582
583 if anilistSelectedOption.Key == "-1" { 583 if anilistSelectedOption.Key == "-1" {
@@ -606,7 +606,7 @@ func AddNewAnime(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseA
606 ClearScreen() 606 ClearScreen()
607 CurdOut("Select which list to add the anime to:") 607 CurdOut("Select which list to add the anime to:")
608 608
609 categorySelection, err := DynamicSelect(categories, false) 609 categorySelection, err := DynamicSelect(categories)
610 if err != nil { 610 if err != nil {
611 Log(fmt.Sprintf("Failed to select category: %v", err)) 611 Log(fmt.Sprintf("Failed to select category: %v", err))
612 ExitCurd(fmt.Errorf("Failed to select category")) 612 ExitCurd(fmt.Errorf("Failed to select category"))
@@ -719,7 +719,7 @@ func SetupCurd(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAni
719 719
720 // Select category using DynamicSelect 720 // Select category using DynamicSelect
721 var err error 721 var err error
722 categorySelection, err = DynamicSelect(categories, false) 722 categorySelection, err = DynamicSelect(categories)
723 if err != nil { 723 if err != nil {
724 Log(fmt.Sprintf("Failed to select category: %v", err)) 724 Log(fmt.Sprintf("Failed to select category: %v", err))
725 ExitCurd(fmt.Errorf("Failed to select category")) 725 ExitCurd(fmt.Errorf("Failed to select category"))
@@ -795,7 +795,10 @@ func SetupCurd(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAni
795 if userCurdConfig.RofiSelection && userCurdConfig.ImagePreview { 795 if userCurdConfig.RofiSelection && userCurdConfig.ImagePreview {
796 anilistSelectedOption, err = DynamicSelectPreview(animeListMapPreview, true) 796 anilistSelectedOption, err = DynamicSelectPreview(animeListMapPreview, true)
797 } else { 797 } else {
798 anilistSelectedOption, err = DynamicSelect(animeListMap, true) 798 // Add "Add new anime" option to the map
799 animeListMap["add_new"] = "Add new anime"
800
801 anilistSelectedOption, err = DynamicSelect(animeListMap)
799 } 802 }
800 if err != nil { 803 if err != nil {
801 Log(fmt.Sprintf("Error selecting anime: %v", err)) 804 Log(fmt.Sprintf("Error selecting anime: %v", err))
@@ -860,7 +863,7 @@ func SetupCurd(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAni
860 // If unable to get Allanime id automatically get manually 863 // If unable to get Allanime id automatically get manually
861 if anime.AllanimeId == "" { 864 if anime.AllanimeId == "" {
862 CurdOut("Failed to automatically select anime") 865 CurdOut("Failed to automatically select anime")
863 selectedAllanimeAnime, err := DynamicSelect(animeList, false) 866 selectedAllanimeAnime, err := DynamicSelect(animeList)
864 867
865 if selectedAllanimeAnime.Key == "-1" { 868 if selectedAllanimeAnime.Key == "-1" {
866 ExitCurd(nil) 869 ExitCurd(nil)
@@ -905,7 +908,7 @@ func SetupCurd(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAni
905 var err error 908 var err error
906 909
907 // Use rofi for selection 910 // Use rofi for selection
908 selectedOption, err = DynamicSelect(options, false) 911 selectedOption, err = DynamicSelect(options)
909 if err != nil { 912 if err != nil {
910 Log("Error in selection: " + err.Error()) 913 Log("Error in selection: " + err.Error())
911 ExitCurd(err) 914 ExitCurd(err)
internal/localTracking.go +1 −1
@@ -279,7 +279,7 @@ func WatchUntracked(userCurdConfig *CurdConfig) {
279 } 279 }
280 280
281 // Select anime from search results 281 // Select anime from search results
282 selectedAnime, err := DynamicSelect(animeList, false) 282 selectedAnime, err := DynamicSelect(animeList)
283 if err != nil { 283 if err != nil {
284 Log(fmt.Sprintf("Failed to select anime: %v", err)) 284 Log(fmt.Sprintf("Failed to select anime: %v", err))
285 ExitCurd(fmt.Errorf("Failed to select anime")) 285 ExitCurd(fmt.Errorf("Failed to select anime"))
internal/selection_menu.go +6 −19
@@ -363,7 +363,7 @@ func showCachedImagePreview(imageURL string) error {
363 return nil 363 return nil
364} 364}
365 365
366func RofiSelect(options map[string]string, addanimeopt bool) (SelectionOption, error) { 366func RofiSelect(options map[string]string) (SelectionOption, error) {
367 userCurdConfig := GetGlobalConfig() 367 userCurdConfig := GetGlobalConfig()
368 if userCurdConfig.StoragePath == "" { 368 if userCurdConfig.StoragePath == "" {
369 userCurdConfig.StoragePath = os.ExpandEnv("${HOME}/.local/share/curd") 369 userCurdConfig.StoragePath = os.ExpandEnv("${HOME}/.local/share/curd")
@@ -378,13 +378,9 @@ func RofiSelect(options map[string]string, addanimeopt bool) (SelectionOption, e
378 // Sort the options alphabetically 378 // Sort the options alphabetically
379 sort.Strings(optionsList) 379 sort.Strings(optionsList)
380 380
381 // Add "Add new anime" and "Quit" options 381 // Add "Quit" options
382 if addanimeopt { 382 optionsList = append(optionsList, "Quit")
383 optionsList = append(optionsList, "Add new anime", "Quit") 383
384 } else {
385 optionsList = append(optionsList, "Quit")
386 }
387
388 // Join all options into a single string, separated by newlines 384 // Join all options into a single string, separated by newlines
389 optionsString := strings.Join(optionsList, "\n") 385 optionsString := strings.Join(optionsList, "\n")
390 386
@@ -409,8 +405,6 @@ func RofiSelect(options map[string]string, addanimeopt bool) (SelectionOption, e
409 switch selected { 405 switch selected {
410 case "": 406 case "":
411 return SelectionOption{}, fmt.Errorf("no selection made") 407 return SelectionOption{}, fmt.Errorf("no selection made")
412 case "Add new anime":
413 return SelectionOption{Label: "Add new anime", Key: "add_new"}, nil
414 case "Quit": 408 case "Quit":
415 return SelectionOption{Label: "Quit", Key: "-1"}, nil 409 return SelectionOption{Label: "Quit", Key: "-1"}, nil
416 } 410 }
@@ -427,9 +421,9 @@ func RofiSelect(options map[string]string, addanimeopt bool) (SelectionOption, e
427} 421}
428 422
429// DynamicSelect displays a simple selection prompt without extra features 423// DynamicSelect displays a simple selection prompt without extra features
430func DynamicSelect(options map[string]string, addnewoption bool) (SelectionOption, error) { 424func DynamicSelect(options map[string]string) (SelectionOption, error) {
431 if GetGlobalConfig().RofiSelection { 425 if GetGlobalConfig().RofiSelection {
432 return RofiSelect(options, addnewoption) 426 return RofiSelect(options)
433 } 427 }
434 428
435 // Create a slice to maintain order 429 // Create a slice to maintain order
@@ -462,15 +456,8 @@ func DynamicSelect(options map[string]string, addnewoption bool) (SelectionOptio
462 model := &Model{ 456 model := &Model{
463 options: options, 457 options: options,
464 filteredKeys: orderedOptions, 458 filteredKeys: orderedOptions,
465 addNewOption: addnewoption,
466 } 459 }
467 460
468 if addnewoption {
469 model.filteredKeys = append(model.filteredKeys, SelectionOption{
470 Label: "Add new anime",
471 Key: "add_new",
472 })
473 }
474 461
475 model.filteredKeys = append(model.filteredKeys, SelectionOption{ 462 model.filteredKeys = append(model.filteredKeys, SelectionOption{
476 Label: "Quit", 463 Label: "Quit",