Commit 9839c6b1f8
Unsigned
Layout: unified · split
internal/selection_menu.go +42 −15
| @@ -8,8 +8,8 @@ import ( | ||
| 8 | 8 | |
| 9 | 9 | // SelectionOption holds the label and the internal key |
| 10 | 10 | type SelectionOption struct { |
| 11 | Label string | |
| 12 | Key string | |
| 11 | Label string | |
| 12 | Key string | |
| 13 | 13 | } |
| 14 | 14 | |
| 15 | 15 | // Model represents the application state for the selection prompt |
| @@ -18,6 +18,7 @@ type Model struct { | ||
| 18 | 18 | filter string |
| 19 | 19 | filteredKeys []SelectionOption |
| 20 | 20 | selected int |
| 21 | terminalHeight int // New field to hold the terminal height | |
| 21 | 22 | } |
| 22 | 23 | |
| 23 | 24 | // Init initializes the model |
| @@ -27,6 +28,11 @@ func (m Model) Init() tea.Cmd { | ||
| 27 | 28 | |
| 28 | 29 | // Update handles user input and updates the model |
| 29 | 30 | func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { |
| 31 | // Handle terminal resize messages | |
| 32 | if wsm, ok := msg.(tea.WindowSizeMsg); ok { | |
| 33 | m.terminalHeight = wsm.Height // Get the terminal height | |
| 34 | } | |
| 35 | ||
| 30 | 36 | // Only filter options if a key press modifies the filter |
| 31 | 37 | updateFilter := false |
| 32 | 38 | |
| @@ -53,7 +59,7 @@ func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { | ||
| 53 | 59 | case "enter": |
| 54 | 60 | return m, tea.Quit // Or return the selected option |
| 55 | 61 | default: |
| 56 | if msg.String() != "" { | |
| 62 | if len(msg.String()) == 1 && msg.String() >= " " && msg.String() <= "~" { | |
| 57 | 63 | m.filter += msg.String() |
| 58 | 64 | updateFilter = true // Mark for updating the filter |
| 59 | 65 | } |
| @@ -70,7 +76,6 @@ func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { | ||
| 70 | 76 | return m, nil |
| 71 | 77 | } |
| 72 | 78 | |
| 73 | ||
| 74 | 79 | // View renders the UI |
| 75 | 80 | func (m Model) View() string { |
| 76 | 81 | var b strings.Builder |
| @@ -81,11 +86,31 @@ func (m Model) View() string { | ||
| 81 | 86 | if len(m.filteredKeys) == 0 { |
| 82 | 87 | b.WriteString("No matches found.\n") |
| 83 | 88 | } else { |
| 84 | for i, option := range m.filteredKeys { | |
| 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 | } | |
| 94 | ||
| 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 | } | |
| 107 | ||
| 108 | // Display the filtered keys with selection indicator | |
| 109 | for i := start; i < end; i++ { | |
| 85 | 110 | if i == m.selected { |
| 86 | b.WriteString(fmt.Sprintf("▶ %s\n", option.Label)) | |
| 111 | b.WriteString(fmt.Sprintf("▶ %s\n", m.filteredKeys[i].Label)) | |
| 87 | 112 | } else { |
| 88 | b.WriteString(fmt.Sprintf(" %s\n", option.Label)) | |
| 113 | b.WriteString(fmt.Sprintf(" %s\n", m.filteredKeys[i].Label)) | |
| 89 | 114 | } |
| 90 | 115 | } |
| 91 | 116 | } |
| @@ -95,18 +120,20 @@ func (m Model) View() string { | ||
| 95 | 120 | |
| 96 | 121 | // filterOptions filters options based on the search term |
| 97 | 122 | func (m *Model) filterOptions() { |
| 98 | m.filteredKeys = nil | |
| 99 | for key, value := range m.options { | |
| 100 | if strings.Contains(strings.ToLower(value), strings.ToLower(m.filter)) { // Check values instead of keys | |
| 101 | m.filteredKeys = append(m.filteredKeys, SelectionOption{Label: value, Key: key}) // Set Label to value | |
| 102 | } | |
| 103 | } | |
| 123 | // Clear the filteredKeys slice before appending new matches | |
| 124 | m.filteredKeys = []SelectionOption{} | |
| 125 | ||
| 126 | for key, value := range m.options { | |
| 127 | if strings.Contains(strings.ToLower(value), strings.ToLower(m.filter)) { | |
| 128 | m.filteredKeys = append(m.filteredKeys, SelectionOption{Label: value, Key: key}) | |
| 129 | } | |
| 130 | } | |
| 104 | 131 | } |
| 105 | 132 | |
| 106 | 133 | // DynamicSelect displays a full-screen selection prompt with search |
| 107 | 134 | func DynamicSelect(options map[string]string) (SelectionOption, error) { |
| 108 | ClearScreen() | |
| 109 | defer RestoreScreen() | |
| 135 | // ClearScreen() | |
| 136 | // defer RestoreScreen() | |
| 110 | 137 | model := &Model{ // Use a pointer to Model |
| 111 | 138 | options: options, |
| 112 | 139 | filteredKeys: make([]SelectionOption, 0), // Initialize filteredKeys |