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
677677 ExitCurd(fmt.Errorf("Failed to select category"))
678678 }
679679
680 if categorySelection.Key == "-1" || strings.EqualFold(categorySelection.Label, "Quit") {
680 categorySelection = NormalizeSelectionKey(categorySelection)
681 if SelectionMeansQuit(categorySelection) {
681682 ExitCurd(nil)
682683 }
683684
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.
685690 continue
686691 }
687692
@@ -792,12 +797,13 @@ func SetupCurd(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAni
792797
793798 Log(anilistSelectedOption)
794799
795 if anilistSelectedOption.Key == "-1" {
800 anilistSelectedOption = NormalizeSelectionKey(anilistSelectedOption)
801 if SelectionMeansQuit(anilistSelectedOption) {
796802 ExitCurd(nil)
797803 }
798804
799805 // Handle back navigation - go back to category selection
800 if anilistSelectedOption.Key == "-2" {
806 if SelectionMeansBack(anilistSelectedOption) {
801807 if userCurdConfig.CurrentCategory {
802808 // If CurrentCategory is forced, back means quit
803809 ExitCurd(nil)
@@ -805,6 +811,9 @@ func SetupCurd(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAni
805811 ClearScreen()
806812 continue categorySelectionLoop
807813 }
814 if anilistSelectedOption.Key == "" {
815 continue animeSelectionLoop
816 }
808817
809818 if anilistSelectedOption.Label == "add_new" || anilistSelectedOption.Key == "add_new" {
810819 addResult := AddNewAnime(userCurdConfig, anime, user, databaseAnimes)
internal/episode_link_recovery_test.go +5 −16
@@ -11,7 +11,7 @@ func TestEpisodeLinkFailureRecoveryOptionsOrder(t *testing.T) {
1111 if len(withAudio) != 3 {
1212 t.Fatalf("expected 3 options with audio, got %#v", withAudio)
1313 }
14 if withAudio[0].Key != "remap" || !strings.HasPrefix(withAudio[0].Label, "1.") {
14 if withAudio[0].Key != "remap" {
1515 t.Fatalf("remap should be first: %#v", withAudio[0])
1616 }
1717 if withAudio[1].Key != "audio" || !strings.Contains(withAudio[1].Label, "dub") {
@@ -20,11 +20,10 @@ func TestEpisodeLinkFailureRecoveryOptionsOrder(t *testing.T) {
2020 if withAudio[2].Key != "episode" || !strings.Contains(strings.ToLower(withAudio[2].Label), "wrong") {
2121 t.Fatalf("episode correction should be last ranked action: %#v", withAudio[2])
2222 }
23
24 // Numbered labels must sort in the same priority order as DynamicSelect's alpha sort.
25 labels := []string{withAudio[0].Label, withAudio[1].Label, withAudio[2].Label}
26 if !(labels[0] < labels[1] && labels[1] < labels[2]) {
27 t.Fatalf("numbered labels should sort by priority, got %v", labels)
23 for _, opt := range withAudio {
24 if strings.HasPrefix(opt.Label, "1.") || strings.HasPrefix(opt.Label, "2.") {
25 t.Fatalf("recovery options should not be numbered: %#v", opt)
26 }
2827 }
2928
3029 withoutAudio := episodeLinkFailureRecoveryOptions("dub", false)
@@ -34,16 +33,6 @@ func TestEpisodeLinkFailureRecoveryOptionsOrder(t *testing.T) {
3433 if withoutAudio[0].Key != "remap" || withoutAudio[1].Key != "episode" {
3534 t.Fatalf("unexpected options without audio: %#v", withoutAudio)
3635 }
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 }
4736}
4837
4938func TestEpisodeLinkFailureDiagnosisIncludesEpisodeModeProviders(t *testing.T) {
internal/logic_regression_test.go +19
@@ -200,6 +200,25 @@ func TestFilterQuitOnlyShowsQuitNotBack(t *testing.T) {
200200 }
201201}
202202
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
203222func TestLegacyModeStillTypeToFilter(t *testing.T) {
204223 prev := GetGlobalConfig()
205224 SetGlobalConfig(&CurdConfig{VimKeys: false})
internal/provider_mapping.go +32 −16
@@ -370,16 +370,16 @@ func promptProviderSearchRecovery(config *CurdConfig, state *providerMappingSear
370370 if message == "" {
371371 message = fmt.Sprintf("No results found for '%s' on %s.", state.query, state.currentProviderLabel())
372372 }
373 CurdOut(message)
374373
375 selected, err := promptSelect(options)
374 selected, err := selectWithOptionalMessage(config, options, "Provider search", message)
376375 if err != nil {
377376 return "", err
378377 }
379 if selected.Key == "-1" {
378 selected = NormalizeSelectionKey(selected)
379 if SelectionMeansQuit(selected) {
380380 return "quit", nil
381381 }
382 if selected.Key == "-2" {
382 if SelectionMeansBack(selected) || selected.Key == "back" {
383383 return "back", nil
384384 }
385385 return selected.Key, nil
@@ -398,20 +398,31 @@ func promptProviderMatchRecovery(config *CurdConfig, state *providerMappingSearc
398398 }
399399 options = append(options, SelectionOption{Key: "back", Label: "Back to menu"})
400400
401 CurdOut("We didn't find an automatic provider match.")
402 selected, err := promptSelect(options)
401 selected, err := selectWithOptionalMessage(config, options, "Provider match", "We didn't find an automatic provider match.")
403402 if err != nil {
404403 return "", err
405404 }
406 if selected.Key == "-1" {
405 selected = NormalizeSelectionKey(selected)
406 if SelectionMeansQuit(selected) {
407407 return "quit", nil
408408 }
409 if selected.Key == "-2" {
409 if SelectionMeansBack(selected) || selected.Key == "back" {
410410 return "back", nil
411411 }
412412 return selected.Key, nil
413413}
414414
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
415426func activeProviderName(config *CurdConfig, state *providerMappingSearchState) string {
416427 if state != nil && state.sequential {
417428 if name := state.currentProviderLabel(); name != "" {
@@ -800,20 +811,19 @@ func episodeLinkFailureRecoveryOptions(preferredMode string, includeAudio bool)
800811 preferredMode = normalizeTranslationType(preferredMode)
801812 alternateMode := alternateTranslationType(preferredMode)
802813
814 // No numeric indices — callers must use DynamicSelectPreserveOrder / Rofi (keeps order).
803815 options := []SelectionOption{
804 {Key: "remap", Label: "1. Search for this anime again"},
816 {Key: "remap", Label: "Search for this anime again"},
805817 }
806 next := 2
807818 if includeAudio {
808819 options = append(options, SelectionOption{
809820 Key: "audio",
810 Label: fmt.Sprintf("%d. Try other audio (%s)", next, alternateMode),
821 Label: fmt.Sprintf("Try other audio (%s)", alternateMode),
811822 })
812 next++
813823 }
814824 options = append(options, SelectionOption{
815825 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)",
817827 })
818828 return options
819829}
@@ -876,16 +886,22 @@ func promptEpisodeLinkFailureRecovery(config *CurdConfig, anime *Anime, lastErr
876886 selected, err = RofiSelectWithMessage(options, false, "Playback recovery", diagnosis)
877887 } else {
878888 fmt.Println(diagnosis)
879 selected, err = promptSelect(options)
889 selected, err = promptSelectOrdered(options)
880890 }
881891 if err != nil {
882892 return "back"
883893 }
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 }
884902 switch selected.Key {
885903 case "remap", "audio", "episode":
886904 return selected.Key
887 case "-1", "-2", "quit", "back", "":
888 return "back"
889905 default:
890906 return "back"
891907 }
internal/selection_menu.go +52 −9
@@ -407,6 +407,35 @@ func detectHomeMenu(options []SelectionOption) bool {
407407 return false
408408}
409409
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
410439func findSelectionIndex(options []SelectionOption, previousKey string, previousLabel string, fallbackIndex int) int {
411440 if previousKey != "" {
412441 for idx, option := range options {
@@ -770,6 +799,10 @@ func DynamicSelectPreserveOrder(options []SelectionOption) (SelectionOption, err
770799
771800var promptSelect = DynamicSelect
772801
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
773806func DynamicSelectWithRefresh(options []SelectionOption, refreshConfig *SelectionRefreshConfig) (SelectionOption, error) {
774807 return dynamicSelectInternal(options, refreshConfig, false)
775808}
@@ -848,10 +881,14 @@ func dynamicSelectInternal(options []SelectionOption, refreshConfig *SelectionRe
848881 return SelectionOption{}, fmt.Errorf("unexpected model type")
849882 }
850883
851 if finalSelectionModel.selected < len(finalSelectionModel.filteredKeys) {
852 return finalSelectionModel.filteredKeys[finalSelectionModel.selected], nil
884 if finalSelectionModel.selected >= 0 && finalSelectionModel.selected < len(finalSelectionModel.filteredKeys) {
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
853890 }
854 return SelectionOption{}, nil
891 return SelectionOption{Key: "-2", Label: "Back"}, nil
855892}
856893
857894func buildRofiOptionsString(options []SelectionOption, isHomeMenu bool) string {
@@ -880,21 +917,27 @@ func parseRofiSelection(err error, rawSelection string, options []SelectionOptio
880917 }
881918
882919 selected := strings.TrimSpace(rawSelection)
883 switch selected {
884 case "":
920 // Strip accidental Pango/markup noise if a theme echoes it.
921 selected = strings.TrimSpace(ansiStrip.ReplaceAllString(selected, ""))
922 switch {
923 case selected == "":
885924 if isHomeMenu {
886925 return SelectionOption{Key: "-1", Label: "Quit"}, nil
887926 }
888927 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"):
890929 return SelectionOption{Label: "Back", Key: "-2"}, nil
891 case "Quit":
930 case strings.EqualFold(selected, "Quit"):
892931 return SelectionOption{Label: "Quit", Key: "-1"}, nil
893932 }
894933
895934 for _, opt := range options {
896 if opt.Label == selected {
897 return opt, nil
935 if opt.Label == selected || strings.EqualFold(opt.Label, selected) {
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
898941 }
899942 }
900943
internal/update.go +4 −3
@@ -553,18 +553,19 @@ func HandlePendingUpdatePrompt(config *CurdConfig, currentVersion string) bool {
553553 fmt.Println(header.Render(prompt))
554554 fmt.Println(message)
555555 fmt.Println()
556 selected, err = DynamicSelectPreserveOrder(options)
556 selected, err = promptSelectOrdered(options)
557557 }
558558 if err != nil {
559559 return false
560560 }
561 selected = NormalizeSelectionKey(selected)
561562 // 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) {
563564 ExitCurd(nil)
564565 return true
565566 }
566567 // 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 == "" {
568569 return false
569570 }
570571
internal/user_intent_regression_test.go +3
@@ -57,9 +57,12 @@ func (p *fakePlaybackProvider) GetEpisodeURL(config CurdConfig, id string, epNo
5757func withPromptSelect(t *testing.T, fn func([]SelectionOption) (SelectionOption, error)) {
5858 t.Helper()
5959 previous := promptSelect
60 previousOrdered := promptSelectOrdered
6061 promptSelect = fn
62 promptSelectOrdered = fn
6163 t.Cleanup(func() {
6264 promptSelect = previous
65 promptSelectOrdered = previousOrdered
6366 })
6467}
6568