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 (
88
99// SelectionOption holds the label and the internal key
1010type SelectionOption struct {
11 Label string
12 Key string
11 Label string
12 Key string
1313}
1414
1515// Model represents the application state for the selection prompt
@@ -18,6 +18,7 @@ type Model struct {
1818 filter string
1919 filteredKeys []SelectionOption
2020 selected int
21 terminalHeight int // New field to hold the terminal height
2122}
2223
2324// Init initializes the model
@@ -27,6 +28,11 @@ func (m Model) Init() tea.Cmd {
2728
2829// Update handles user input and updates the model
2930func (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
3036 // Only filter options if a key press modifies the filter
3137 updateFilter := false
3238
@@ -53,7 +59,7 @@ func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
5359 case "enter":
5460 return m, tea.Quit // Or return the selected option
5561 default:
56 if msg.String() != "" {
62 if len(msg.String()) == 1 && msg.String() >= " " && msg.String() <= "~" {
5763 m.filter += msg.String()
5864 updateFilter = true // Mark for updating the filter
5965 }
@@ -70,7 +76,6 @@ func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
7076 return m, nil
7177}
7278
73
7479// View renders the UI
7580func (m Model) View() string {
7681 var b strings.Builder
@@ -81,11 +86,31 @@ func (m Model) View() string {
8186 if len(m.filteredKeys) == 0 {
8287 b.WriteString("No matches found.\n")
8388 } 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++ {
85110 if i == m.selected {
86 b.WriteString(fmt.Sprintf("▶ %s\n", option.Label))
111 b.WriteString(fmt.Sprintf("▶ %s\n", m.filteredKeys[i].Label))
87112 } else {
88 b.WriteString(fmt.Sprintf(" %s\n", option.Label))
113 b.WriteString(fmt.Sprintf(" %s\n", m.filteredKeys[i].Label))
89114 }
90115 }
91116 }
@@ -95,18 +120,20 @@ func (m Model) View() string {
95120
96121// filterOptions filters options based on the search term
97122func (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 }
104131}
105132
106133// DynamicSelect displays a full-screen selection prompt with search
107134func DynamicSelect(options map[string]string) (SelectionOption, error) {
108 ClearScreen()
109 defer RestoreScreen()
135 // ClearScreen()
136 // defer RestoreScreen()
110137 model := &Model{ // Use a pointer to Model
111138 options: options,
112139 filteredKeys: make([]SelectionOption, 0), // Initialize filteredKeys