Commit 5155a69c2e
Unsigned
Layout: unified · split
internal/selection_menu.go +53 −37
| @@ -57,8 +57,14 @@ func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { | ||
| 57 | 57 | m.selected-- |
| 58 | 58 | } |
| 59 | 59 | case "enter": |
| 60 | return m, tea.Quit // Or return the selected option | |
| 61 | default: | |
| 60 | // If "Add new anime" is selected, handle it here | |
| 61 | if m.filteredKeys[m.selected].Key == "add_new" { | |
| 62 | // Implement your logic for adding a new anime | |
| 63 | fmt.Println("Adding a new anime...") // Temporary print statement | |
| 64 | return m, tea.Quit | |
| 65 | } | |
| 66 | return m, tea.Quit // Or return the selected option for other cases | |
| 67 | default: | |
| 62 | 68 | if len(msg.String()) == 1 && msg.String() >= " " && msg.String() <= "~" { |
| 63 | 69 | m.filter += msg.String() |
| 64 | 70 | updateFilter = true // Mark for updating the filter |
| @@ -78,46 +84,47 @@ func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { | ||
| 78 | 84 | |
| 79 | 85 | // View renders the UI |
| 80 | 86 | func (m Model) View() string { |
| 81 | var b strings.Builder | |
| 82 | ||
| 83 | b.WriteString("Search (Press Ctrl+C to quit):\n\n") | |
| 84 | b.WriteString("Filter: " + m.filter + "\n\n") | |
| 85 | ||
| 86 | if len(m.filteredKeys) == 0 { | |
| 87 | b.WriteString("No matches found.\n") | |
| 88 | } else { | |
| 89 | // Determine how many entries can fit in the terminal | |
| 90 | maxEntries := m.terminalHeight - 4 // Adjust for header and padding | |
| 91 | if maxEntries < 1 { | |
| 92 | maxEntries = 1 // At least show one option | |
| 93 | } | |
| 87 | var b strings.Builder | |
| 88 | ||
| 89 | b.WriteString("Search (Press Ctrl+C to quit):\n\n") | |
| 90 | b.WriteString("Filter: " + m.filter + "\n\n") | |
| 91 | ||
| 92 | if len(m.filteredKeys) == 0 { | |
| 93 | b.WriteString("No matches found.\n") | |
| 94 | } else { | |
| 95 | // Determine how many entries can fit in the terminal | |
| 96 | maxEntries := m.terminalHeight - 4 // Adjust for header and padding | |
| 97 | if maxEntries < 1 { | |
| 98 | maxEntries = 1 // At least show one option | |
| 99 | } | |
| 94 | 100 | |
| 95 | // Limit the number of displayed entries | |
| 96 | start := m.selected | |
| 97 | if start >= len(m.filteredKeys) { | |
| 98 | start = len(m.filteredKeys) - 1 | |
| 99 | } | |
| 100 | end := start + maxEntries | |
| 101 | if end > len(m.filteredKeys) { | |
| 102 | end = len(m.filteredKeys) | |
| 103 | } | |
| 104 | if start > end { | |
| 105 | start = end | |
| 106 | } | |
| 101 | // Limit the number of displayed entries | |
| 102 | start := m.selected | |
| 103 | if start >= len(m.filteredKeys) { | |
| 104 | start = len(m.filteredKeys) - 1 | |
| 105 | } | |
| 106 | end := start + maxEntries | |
| 107 | if end > len(m.filteredKeys) { | |
| 108 | end = len(m.filteredKeys) | |
| 109 | } | |
| 110 | if start > end { | |
| 111 | start = end | |
| 112 | } | |
| 107 | 113 | |
| 108 | // Display the filtered keys with selection indicator | |
| 109 | for i := start; i < end; i++ { | |
| 110 | if i == m.selected { | |
| 111 | b.WriteString(fmt.Sprintf("▶ %s\n", m.filteredKeys[i].Label)) | |
| 112 | } else { | |
| 113 | b.WriteString(fmt.Sprintf(" %s\n", m.filteredKeys[i].Label)) | |
| 114 | } | |
| 115 | } | |
| 116 | } | |
| 114 | // Display the filtered keys with selection indicator | |
| 115 | for i := start; i < end; i++ { | |
| 116 | if i == m.selected { | |
| 117 | b.WriteString(fmt.Sprintf("▶ %s\n", m.filteredKeys[i].Label)) | |
| 118 | } else { | |
| 119 | b.WriteString(fmt.Sprintf(" %s\n", m.filteredKeys[i].Label)) | |
| 120 | } | |
| 121 | } | |
| 122 | } | |
| 117 | 123 | |
| 118 | return b.String() | |
| 124 | return b.String() | |
| 119 | 125 | } |
| 120 | 126 | |
| 127 | ||
| 121 | 128 | // filterOptions filters options based on the search term |
| 122 | 129 | func (m *Model) filterOptions() { |
| 123 | 130 | // Clear the filteredKeys slice before appending new matches |
| @@ -128,8 +135,17 @@ func (m *Model) filterOptions() { | ||
| 128 | 135 | m.filteredKeys = append(m.filteredKeys, SelectionOption{Label: value, Key: key}) |
| 129 | 136 | } |
| 130 | 137 | } |
| 138 | ||
| 139 | // If no matches were found, add the "Add new anime" option | |
| 140 | if len(m.filteredKeys) == 0 { | |
| 141 | m.filteredKeys = append(m.filteredKeys, SelectionOption{ | |
| 142 | Label: "Add new anime", | |
| 143 | Key: "add_new", // Special key to identify this option | |
| 144 | }) | |
| 145 | } | |
| 131 | 146 | } |
| 132 | 147 | |
| 148 | ||
| 133 | 149 | // DynamicSelect displays a full-screen selection prompt with search |
| 134 | 150 | func DynamicSelect(options map[string]string) (SelectionOption, error) { |
| 135 | 151 | // ClearScreen() |