Commit 6bee23b811

6bee23b811e65d6a4a8f10f3e4ae7beca80ebee9

parent: d9152af709

Unsigned

Wraient <wraient.911@gmail.com> · 2026-07-22 20:54 UTC

Harden selection Quit/Back handling across menus

Centralize Quit/Back detection, normalize selection keys on return, treat
empty picks as cancel, drop numbered recovery labels, and route recovery
messages through Rofi -mesg to avoid notify spam.

Layout: unified · split

internal/curd.go +13 −4
@@ -677,11 +677,16 @@ func SetupCurd(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAni
677 ExitCurd(fmt.Errorf("Failed to select category")) 677 ExitCurd(fmt.Errorf("Failed to select category"))
678 } 678 }
679 679
680 if categorySelection.Key == "-1" || strings.EqualFold(categorySelection.Label, "Quit") { 680 categorySelection = NormalizeSelectionKey(categorySelection)
681 if SelectionMeansQuit(categorySelection) {
681 ExitCurd(nil) 682 ExitCurd(nil)
682 } 683 }
683 684
684 if categorySelection.Key == "-2" || strings.EqualFold(categorySelection.Label, "Back") { 685 if SelectionMeansBack(categorySelection) {
686 continue
687 }
688 if categorySelection.Key == "" {
689 // Empty/cancelled selection — re-show category menu.
685 continue 690 continue
686 } 691 }
687 692
@@ -792,12 +797,13 @@ func SetupCurd(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAni
792 797
793 Log(anilistSelectedOption) 798 Log(anilistSelectedOption)
794 799
795 if anilistSelectedOption.Key == "-1" { 800 anilistSelectedOption = NormalizeSelectionKey(anilistSelectedOption)
801 if SelectionMeansQuit(anilistSelectedOption) {
796 ExitCurd(nil) 802 ExitCurd(nil)
797 } 803 }
798 804
799 // Handle back navigation - go back to category selection 805 // Handle back navigation - go back to category selection
800 if anilistSelectedOption.Key == "-2" { 806 if SelectionMeansBack(anilistSelectedOption) {
801 if userCurdConfig.CurrentCategory { 807 if userCurdConfig.CurrentCategory {
802 // If CurrentCategory is forced, back means quit 808 // If CurrentCategory is forced, back means quit
803 ExitCurd(nil) 809 ExitCurd(nil)
@@ -805,6 +811,9 @@ func SetupCurd(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAni
805 ClearScreen() 811 ClearScreen()
806 continue categorySelectionLoop 812 continue categorySelectionLoop
807 } 813 }
814 if anilistSelectedOption.Key == "" {
815 continue animeSelectionLoop
816 }
808 817
809 if anilistSelectedOption.Label == "add_new" || anilistSelectedOption.Key == "add_new" { 818 if anilistSelectedOption.Label == "add_new" || anilistSelectedOption.Key == "add_new" {
810 addResult := AddNewAnime(userCurdConfig, anime, user, databaseAnimes) 819 addResult := AddNewAnime(userCurdConfig, anime, user, databaseAnimes)
internal/episode_link_recovery_test.go +5 −16
@@ -11,7 +11,7 @@ func TestEpisodeLinkFailureRecoveryOptionsOrder(t *testing.T) {
11 if len(withAudio) != 3 { 11 if len(withAudio) != 3 {
12 t.Fatalf("expected 3 options with audio, got %#v", withAudio) 12 t.Fatalf("expected 3 options with audio, got %#v", withAudio)
13 } 13 }
14 if withAudio[0].Key != "remap" || !strings.HasPrefix(withAudio[0].Label, "1.") { 14 if withAudio[0].Key != "remap" {
15 t.Fatalf("remap should be first: %#v", withAudio[0]) 15 t.Fatalf("remap should be first: %#v", withAudio[0])
16 } 16 }
17 if withAudio[1].Key != "audio" || !strings.Contains(withAudio[1].Label, "dub") { 17 if withAudio[1].Key != "audio" || !strings.Contains(withAudio[1].Label, "dub") {
@@ -20,11 +20,10 @@ func TestEpisodeLinkFailureRecoveryOptionsOrder(t *testing.T) {
20 if withAudio[2].Key != "episode" || !strings.Contains(strings.ToLower(withAudio[2].Label), "wrong") { 20 if withAudio[2].Key != "episode" || !strings.Contains(strings.ToLower(withAudio[2].Label), "wrong") {
21 t.Fatalf("episode correction should be last ranked action: %#v", withAudio[2]) 21 t.Fatalf("episode correction should be last ranked action: %#v", withAudio[2])
22 } 22 }
23 23 for _, opt := range withAudio {
24 // Numbered labels must sort in the same priority order as DynamicSelect's alpha sort. 24 if strings.HasPrefix(opt.Label, "1.") || strings.HasPrefix(opt.Label, "2.") {
25 labels := []string{withAudio[0].Label, withAudio[1].Label, withAudio[2].Label} 25 t.Fatalf("recovery options should not be numbered: %#v", opt)
26 if !(labels[0] < labels[1] && labels[1] < labels[2]) { 26 }
27 t.Fatalf("numbered labels should sort by priority, got %v", labels)
28 } 27 }
29 28
30 withoutAudio := episodeLinkFailureRecoveryOptions("dub", false) 29 withoutAudio := episodeLinkFailureRecoveryOptions("dub", false)
@@ -34,16 +33,6 @@ func TestEpisodeLinkFailureRecoveryOptionsOrder(t *testing.T) {
34 if withoutAudio[0].Key != "remap" || withoutAudio[1].Key != "episode" { 33 if withoutAudio[0].Key != "remap" || withoutAudio[1].Key != "episode" {
35 t.Fatalf("unexpected options without audio: %#v", withoutAudio) 34 t.Fatalf("unexpected options without audio: %#v", withoutAudio)
36 } 35 }
37 if !strings.HasPrefix(withoutAudio[1].Label, "2.") {
38 t.Fatalf("episode option should renumber when audio omitted: %#v", withoutAudio[1])
39 }
40 // No cancel/quit/back in our option list — DynamicSelect injects a single exit pair.
41 for _, opt := range withAudio {
42 switch opt.Key {
43 case "quit", "back", "-1", "-2", "cancel":
44 t.Fatalf("recovery options should not include exit keys: %#v", opt)
45 }
46 }
47} 36}
48 37
49func TestEpisodeLinkFailureDiagnosisIncludesEpisodeModeProviders(t *testing.T) { 38func TestEpisodeLinkFailureDiagnosisIncludesEpisodeModeProviders(t *testing.T) {
internal/logic_regression_test.go +19
@@ -200,6 +200,25 @@ func TestFilterQuitOnlyShowsQuitNotBack(t *testing.T) {
200 } 200 }
201} 201}
202 202
203func TestSelectionMeansQuitAndBack(t *testing.T) {
204 if !SelectionMeansQuit(SelectionOption{Key: "-1"}) {
205 t.Fatal("key -1 should mean quit")
206 }
207 if !SelectionMeansQuit(SelectionOption{Label: "Quit"}) {
208 t.Fatal("label Quit should mean quit")
209 }
210 if !SelectionMeansBack(SelectionOption{Key: "-2"}) {
211 t.Fatal("key -2 should mean back")
212 }
213 if !SelectionMeansBack(SelectionOption{Label: "Back to menu"}) {
214 t.Fatal("Back to menu should mean back")
215 }
216 got := NormalizeSelectionKey(SelectionOption{Label: "quit"})
217 if got.Key != "-1" {
218 t.Fatalf("NormalizeSelectionKey quit -> %#v", got)
219 }
220}
221
203func TestLegacyModeStillTypeToFilter(t *testing.T) { 222func TestLegacyModeStillTypeToFilter(t *testing.T) {
204 prev := GetGlobalConfig() 223 prev := GetGlobalConfig()
205 SetGlobalConfig(&CurdConfig{VimKeys: false}) 224 SetGlobalConfig(&CurdConfig{VimKeys: false})
internal/provider_mapping.go +32 −16
@@ -370,16 +370,16 @@ func promptProviderSearchRecovery(config *CurdConfig, state *providerMappingSear
370 if message == "" { 370 if message == "" {
371 message = fmt.Sprintf("No results found for '%s' on %s.", state.query, state.currentProviderLabel()) 371 message = fmt.Sprintf("No results found for '%s' on %s.", state.query, state.currentProviderLabel())
372 } 372 }
373 CurdOut(message)
374 373
375 selected, err := promptSelect(options) 374 selected, err := selectWithOptionalMessage(config, options, "Provider search", message)
376 if err != nil { 375 if err != nil {
377 return "", err 376 return "", err
378 } 377 }
379 if selected.Key == "-1" { 378 selected = NormalizeSelectionKey(selected)
379 if SelectionMeansQuit(selected) {
380 return "quit", nil 380 return "quit", nil
381 } 381 }
382 if selected.Key == "-2" { 382 if SelectionMeansBack(selected) || selected.Key == "back" {
383 return "back", nil 383 return "back", nil
384 } 384 }
385 return selected.Key, nil 385 return selected.Key, nil
@@ -398,20 +398,31 @@ func promptProviderMatchRecovery(config *CurdConfig, state *providerMappingSearc
398 } 398 }
399 options = append(options, SelectionOption{Key: "back", Label: "Back to menu"}) 399 options = append(options, SelectionOption{Key: "back", Label: "Back to menu"})
400 400
401 CurdOut("We didn't find an automatic provider match.") 401 selected, err := selectWithOptionalMessage(config, options, "Provider match", "We didn't find an automatic provider match.")
402 selected, err := promptSelect(options)
403 if err != nil { 402 if err != nil {
404 return "", err 403 return "", err
405 } 404 }
406 if selected.Key == "-1" { 405 selected = NormalizeSelectionKey(selected)
406 if SelectionMeansQuit(selected) {
407 return "quit", nil 407 return "quit", nil
408 } 408 }
409 if selected.Key == "-2" { 409 if SelectionMeansBack(selected) || selected.Key == "back" {
410 return "back", nil 410 return "back", nil
411 } 411 }
412 return selected.Key, nil 412 return selected.Key, nil
413} 413}
414 414
415// selectWithOptionalMessage uses Rofi -mesg when Rofi is on (no notify spam), else prints + ordered select.
416func selectWithOptionalMessage(config *CurdConfig, options []SelectionOption, prompt, message string) (SelectionOption, error) {
417 if config != nil && config.RofiSelection {
418 return RofiSelectWithMessage(options, false, prompt, message)
419 }
420 if strings.TrimSpace(message) != "" {
421 fmt.Println(message)
422 }
423 return promptSelectOrdered(options)
424}
425
415func activeProviderName(config *CurdConfig, state *providerMappingSearchState) string { 426func activeProviderName(config *CurdConfig, state *providerMappingSearchState) string {
416 if state != nil && state.sequential { 427 if state != nil && state.sequential {
417 if name := state.currentProviderLabel(); name != "" { 428 if name := state.currentProviderLabel(); name != "" {
@@ -800,20 +811,19 @@ func episodeLinkFailureRecoveryOptions(preferredMode string, includeAudio bool)
800 preferredMode = normalizeTranslationType(preferredMode) 811 preferredMode = normalizeTranslationType(preferredMode)
801 alternateMode := alternateTranslationType(preferredMode) 812 alternateMode := alternateTranslationType(preferredMode)
802 813
814 // No numeric indices — callers must use DynamicSelectPreserveOrder / Rofi (keeps order).
803 options := []SelectionOption{ 815 options := []SelectionOption{
804 {Key: "remap", Label: "1. Search for this anime again"}, 816 {Key: "remap", Label: "Search for this anime again"},
805 } 817 }
806 next := 2
807 if includeAudio { 818 if includeAudio {
808 options = append(options, SelectionOption{ 819 options = append(options, SelectionOption{
809 Key: "audio", 820 Key: "audio",
810 Label: fmt.Sprintf("%d. Try other audio (%s)", next, alternateMode), 821 Label: fmt.Sprintf("Try other audio (%s)", alternateMode),
811 }) 822 })
812 next++
813 } 823 }
814 options = append(options, SelectionOption{ 824 options = append(options, SelectionOption{
815 Key: "episode", 825 Key: "episode",
816 Label: fmt.Sprintf("%d. Change episode number (if this one is wrong)", next), 826 Label: "Change episode number (if this one is wrong)",
817 }) 827 })
818 return options 828 return options
819} 829}
@@ -876,16 +886,22 @@ func promptEpisodeLinkFailureRecovery(config *CurdConfig, anime *Anime, lastErr
876 selected, err = RofiSelectWithMessage(options, false, "Playback recovery", diagnosis) 886 selected, err = RofiSelectWithMessage(options, false, "Playback recovery", diagnosis)
877 } else { 887 } else {
878 fmt.Println(diagnosis) 888 fmt.Println(diagnosis)
879 selected, err = promptSelect(options) 889 selected, err = promptSelectOrdered(options)
880 } 890 }
881 if err != nil { 891 if err != nil {
882 return "back" 892 return "back"
883 } 893 }
894 selected = NormalizeSelectionKey(selected)
895 if SelectionMeansQuit(selected) {
896 ExitCurd(nil)
897 return "back"
898 }
899 if SelectionMeansBack(selected) || selected.Key == "" {
900 return "back"
901 }
884 switch selected.Key { 902 switch selected.Key {
885 case "remap", "audio", "episode": 903 case "remap", "audio", "episode":
886 return selected.Key 904 return selected.Key
887 case "-1", "-2", "quit", "back", "":
888 return "back"
889 default: 905 default:
890 return "back" 906 return "back"
891 } 907 }
internal/selection_menu.go +52 −9
@@ -407,6 +407,35 @@ func detectHomeMenu(options []SelectionOption) bool {
407 return false 407 return false
408} 408}
409 409
410// SelectionMeansQuit reports whether the user chose Quit (by key or label).
411func SelectionMeansQuit(opt SelectionOption) bool {
412 if opt.Key == "-1" {
413 return true
414 }
415 label := strings.TrimSpace(opt.Label)
416 return strings.EqualFold(label, "Quit") || strings.EqualFold(label, "quit")
417}
418
419// SelectionMeansBack reports whether the user chose Back / dismiss (by key or label).
420func SelectionMeansBack(opt SelectionOption) bool {
421 if opt.Key == "-2" || strings.EqualFold(opt.Key, "back") {
422 return true
423 }
424 label := strings.ToLower(strings.TrimSpace(opt.Label))
425 return label == "back" || label == "back to menu" || label == "back to list"
426}
427
428// NormalizeSelectionKey forces Quit/Back labels onto the canonical keys used by callers.
429func NormalizeSelectionKey(opt SelectionOption) SelectionOption {
430 if SelectionMeansQuit(opt) {
431 return SelectionOption{Key: "-1", Label: "Quit"}
432 }
433 if SelectionMeansBack(opt) {
434 return SelectionOption{Key: "-2", Label: "Back"}
435 }
436 return opt
437}
438
410func findSelectionIndex(options []SelectionOption, previousKey string, previousLabel string, fallbackIndex int) int { 439func findSelectionIndex(options []SelectionOption, previousKey string, previousLabel string, fallbackIndex int) int {
411 if previousKey != "" { 440 if previousKey != "" {
412 for idx, option := range options { 441 for idx, option := range options {
@@ -770,6 +799,10 @@ func DynamicSelectPreserveOrder(options []SelectionOption) (SelectionOption, err
770 799
771var promptSelect = DynamicSelect 800var promptSelect = DynamicSelect
772 801
802// promptSelectOrdered is used for action menus where option order matters.
803// Tests may replace this the same way as promptSelect.
804var promptSelectOrdered = DynamicSelectPreserveOrder
805
773func DynamicSelectWithRefresh(options []SelectionOption, refreshConfig *SelectionRefreshConfig) (SelectionOption, error) { 806func DynamicSelectWithRefresh(options []SelectionOption, refreshConfig *SelectionRefreshConfig) (SelectionOption, error) {
774 return dynamicSelectInternal(options, refreshConfig, false) 807 return dynamicSelectInternal(options, refreshConfig, false)
775} 808}
@@ -848,10 +881,14 @@ func dynamicSelectInternal(options []SelectionOption, refreshConfig *SelectionRe
848 return SelectionOption{}, fmt.Errorf("unexpected model type") 881 return SelectionOption{}, fmt.Errorf("unexpected model type")
849 } 882 }
850 883
851 if finalSelectionModel.selected < len(finalSelectionModel.filteredKeys) { 884 if finalSelectionModel.selected >= 0 && finalSelectionModel.selected < len(finalSelectionModel.filteredKeys) {
852 return finalSelectionModel.filteredKeys[finalSelectionModel.selected], nil 885 return NormalizeSelectionKey(finalSelectionModel.filteredKeys[finalSelectionModel.selected]), nil
886 }
887 // Empty list / out of range — treat as cancel (Back for submenus, Quit on home).
888 if finalSelectionModel.isHomeMenu {
889 return SelectionOption{Key: "-1", Label: "Quit"}, nil
853 } 890 }
854 return SelectionOption{}, nil 891 return SelectionOption{Key: "-2", Label: "Back"}, nil
855} 892}
856 893
857func buildRofiOptionsString(options []SelectionOption, isHomeMenu bool) string { 894func buildRofiOptionsString(options []SelectionOption, isHomeMenu bool) string {
@@ -880,21 +917,27 @@ func parseRofiSelection(err error, rawSelection string, options []SelectionOptio
880 } 917 }
881 918
882 selected := strings.TrimSpace(rawSelection) 919 selected := strings.TrimSpace(rawSelection)
883 switch selected { 920 // Strip accidental Pango/markup noise if a theme echoes it.
884 case "": 921 selected = strings.TrimSpace(ansiStrip.ReplaceAllString(selected, ""))
922 switch {
923 case selected == "":
885 if isHomeMenu { 924 if isHomeMenu {
886 return SelectionOption{Key: "-1", Label: "Quit"}, nil 925 return SelectionOption{Key: "-1", Label: "Quit"}, nil
887 } 926 }
888 return SelectionOption{Key: "-2", Label: "Back"}, nil 927 return SelectionOption{Key: "-2", Label: "Back"}, nil
889 case "Back": 928 case strings.EqualFold(selected, "Back"), strings.EqualFold(selected, "Back to menu"), strings.EqualFold(selected, "Back to list"):
890 return SelectionOption{Label: "Back", Key: "-2"}, nil 929 return SelectionOption{Label: "Back", Key: "-2"}, nil
891 case "Quit": 930 case strings.EqualFold(selected, "Quit"):
892 return SelectionOption{Label: "Quit", Key: "-1"}, nil 931 return SelectionOption{Label: "Quit", Key: "-1"}, nil
893 } 932 }
894 933
895 for _, opt := range options { 934 for _, opt := range options {
896 if opt.Label == selected { 935 if opt.Label == selected || strings.EqualFold(opt.Label, selected) {
897 return opt, nil 936 return NormalizeSelectionKey(opt), nil
937 }
938 // Match when emoji/spacing differs slightly (e.g. double-space after emoji).
939 if strings.EqualFold(strings.Join(strings.Fields(opt.Label), " "), strings.Join(strings.Fields(selected), " ")) {
940 return NormalizeSelectionKey(opt), nil
898 } 941 }
899 } 942 }
900 943
internal/update.go +4 −3
@@ -553,18 +553,19 @@ func HandlePendingUpdatePrompt(config *CurdConfig, currentVersion string) bool {
553 fmt.Println(header.Render(prompt)) 553 fmt.Println(header.Render(prompt))
554 fmt.Println(message) 554 fmt.Println(message)
555 fmt.Println() 555 fmt.Println()
556 selected, err = DynamicSelectPreserveOrder(options) 556 selected, err = promptSelectOrdered(options)
557 } 557 }
558 if err != nil { 558 if err != nil {
559 return false 559 return false
560 } 560 }
561 selected = NormalizeSelectionKey(selected)
561 // Quit from the pinned menu must exit the whole program (not fall through to curd). 562 // Quit from the pinned menu must exit the whole program (not fall through to curd).
562 if selected.Key == "-1" || strings.EqualFold(selected.Label, "Quit") { 563 if SelectionMeansQuit(selected) {
563 ExitCurd(nil) 564 ExitCurd(nil)
564 return true 565 return true
565 } 566 }
566 // Back / empty = dismiss update prompt and continue the session. 567 // Back / empty = dismiss update prompt and continue the session.
567 if selected.Key == "-2" || selected.Key == "continue" || selected.Key == "" { 568 if SelectionMeansBack(selected) || selected.Key == "continue" || selected.Key == "" {
568 return false 569 return false
569 } 570 }
570 571
internal/user_intent_regression_test.go +3
@@ -57,9 +57,12 @@ func (p *fakePlaybackProvider) GetEpisodeURL(config CurdConfig, id string, epNo
57func withPromptSelect(t *testing.T, fn func([]SelectionOption) (SelectionOption, error)) { 57func withPromptSelect(t *testing.T, fn func([]SelectionOption) (SelectionOption, error)) {
58 t.Helper() 58 t.Helper()
59 previous := promptSelect 59 previous := promptSelect
60 previousOrdered := promptSelectOrdered
60 promptSelect = fn 61 promptSelect = fn
62 promptSelectOrdered = fn
61 t.Cleanup(func() { 63 t.Cleanup(func() {
62 promptSelect = previous 64 promptSelect = previous
65 promptSelectOrdered = previousOrdered
63 }) 66 })
64} 67}
65 68