Commit c3eba1630c

c3eba1630cd6a897ddbd00db4c453340d59f8982

parent: dcc3028f34

Unsigned

Wraient <wraient.911@gmail.com> · 2024-10-22 11:24 UTC

Add: Graceful quiter

Layout: unified · split

internal/selection_menu.go +11 −8
@@ -16,13 +16,13 @@ type SelectionOption struct {
1616
1717// Model represents the application state for the selection prompt
1818type Model struct {
19 options map[string]string
20 filter string
21 filteredKeys []SelectionOption
22 selected int
23 terminalWidth int
19 options map[string]string
20 filter string
21 filteredKeys []SelectionOption
22 selected int
23 terminalWidth int
2424 terminalHeight int
25 scrollOffset int // Track the topmost visible item
25 scrollOffset int // Track the topmost visible item
2626}
2727
2828// Init initializes the model
@@ -44,7 +44,9 @@ func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
4444 case tea.KeyMsg:
4545 switch msg.String() {
4646 case "ctrl+c", "q":
47 return m, tea.Quit
47 // Return quit selection option instead of quitting the program
48 m.filteredKeys[m.selected] = SelectionOption{"quit", "-1"}
49 return m, tea.Quit // Properly exit the program
4850 case "backspace":
4951 if len(m.filter) > 0 {
5052 m.filter = m.filter[:len(m.filter)-1]
@@ -73,6 +75,7 @@ func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
7375 case "enter":
7476 if m.filteredKeys[m.selected].Key == "add_new" {
7577 fmt.Println("Adding a new anime...")
78 m.filteredKeys[m.selected] = SelectionOption{"add_new", "0"}
7679 return m, tea.Quit
7780 }
7881 return m, tea.Quit
@@ -86,7 +89,7 @@ func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
8689
8790 if updateFilter {
8891 m.filterOptions()
89 m.selected = 0 // Reset selection to the first item after filtering
92 m.selected = 0 // Reset selection to the first item after filtering
9093 m.scrollOffset = 0 // Reset scrolling
9194 }
9295