Commit 5155a69c2e

5155a69c2e275fe5408f15fd06d2c204d2ca4db6

parent: eac08d5ef2

Unsigned

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

add new anime in search (template added)

Layout: unified · split

internal/selection_menu.go +53 −37
@@ -57,8 +57,14 @@ func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
5757 m.selected--
5858 }
5959 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:
6268 if len(msg.String()) == 1 && msg.String() >= " " && msg.String() <= "~" {
6369 m.filter += msg.String()
6470 updateFilter = true // Mark for updating the filter
@@ -78,46 +84,47 @@ func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
7884
7985// View renders the UI
8086func (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 }
94100
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 }
107113
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 }
117123
118 return b.String()
124 return b.String()
119125}
120126
127
121128// filterOptions filters options based on the search term
122129func (m *Model) filterOptions() {
123130 // Clear the filteredKeys slice before appending new matches
@@ -128,8 +135,17 @@ func (m *Model) filterOptions() {
128135 m.filteredKeys = append(m.filteredKeys, SelectionOption{Label: value, Key: key})
129136 }
130137 }
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 }
131146}
132147
148
133149// DynamicSelect displays a full-screen selection prompt with search
134150func DynamicSelect(options map[string]string) (SelectionOption, error) {
135151 // ClearScreen()