Commit 9839c6b1f8

9839c6b1f8ad2db0d0763df42979052c317cc827

parent: c91e5a383c

Unsigned

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

fix: for longer animelist

Layout: unified · split

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