Commit 1695850319

1695850319b0ecd9518a72d27204919dbe26a304

parent: e8f271e504

Unsigned

Wraient <wraient.911@gmail.com> · 2024-10-18 09:18 UTC

fix selection bug

Layout: unified · split

internal/selection_menu.go +15
@@ -27,6 +27,9 @@ func (m Model) Init() tea.Cmd {
27 27
28// Update handles user input and updates the model 28// Update handles user input and updates the model
29func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { 29func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
30 // Only filter options if a key press modifies the filter
31 updateFilter := false
32
30 switch msg := msg.(type) { 33 switch msg := msg.(type) {
31 case tea.KeyMsg: 34 case tea.KeyMsg:
32 switch msg.String() { 35 switch msg.String() {
@@ -35,12 +38,15 @@ func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
35 case "backspace": 38 case "backspace":
36 if len(m.filter) > 0 { 39 if len(m.filter) > 0 {
37 m.filter = m.filter[:len(m.filter)-1] 40 m.filter = m.filter[:len(m.filter)-1]
41 updateFilter = true // Mark for updating the filter
38 } 42 }
39 case "down": 43 case "down":
44 // Move cursor down within bounds
40 if m.selected < len(m.filteredKeys)-1 { 45 if m.selected < len(m.filteredKeys)-1 {
41 m.selected++ 46 m.selected++
42 } 47 }
43 case "up": 48 case "up":
49 // Move cursor up within bounds
44 if m.selected > 0 { 50 if m.selected > 0 {
45 m.selected-- 51 m.selected--
46 } 52 }
@@ -49,13 +55,22 @@ func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
49 default: 55 default:
50 if msg.String() != "" { 56 if msg.String() != "" {
51 m.filter += msg.String() 57 m.filter += msg.String()
58 updateFilter = true // Mark for updating the filter
52 } 59 }
53 } 60 }
61 }
62
63 // Only filter options if the filter was actually modified
64 if updateFilter {
54 m.filterOptions() 65 m.filterOptions()
66 // Reset selected index after filtering
67 m.selected = 0
55 } 68 }
69
56 return m, nil 70 return m, nil
57} 71}
58 72
73
59// View renders the UI 74// View renders the UI
60func (m Model) View() string { 75func (m Model) View() string {
61 var b strings.Builder 76 var b strings.Builder