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) {
57 m.selected-- 57 m.selected--
58 } 58 }
59 case "enter": 59 case "enter":
60 return m, tea.Quit // Or return the selected option 60 // If "Add new anime" is selected, handle it here
61 default: 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:
62 if len(msg.String()) == 1 && msg.String() >= " " && msg.String() <= "~" { 68 if len(msg.String()) == 1 && msg.String() >= " " && msg.String() <= "~" {
63 m.filter += msg.String() 69 m.filter += msg.String()
64 updateFilter = true // Mark for updating the filter 70 updateFilter = true // Mark for updating the filter
@@ -78,46 +84,47 @@ func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
78 84
79// View renders the UI 85// View renders the UI
80func (m Model) View() string { 86func (m Model) View() string {
81 var b strings.Builder 87 var b strings.Builder
82 88
83 b.WriteString("Search (Press Ctrl+C to quit):\n\n") 89 b.WriteString("Search (Press Ctrl+C to quit):\n\n")
84 b.WriteString("Filter: " + m.filter + "\n\n") 90 b.WriteString("Filter: " + m.filter + "\n\n")
85 91
86 if len(m.filteredKeys) == 0 { 92 if len(m.filteredKeys) == 0 {
87 b.WriteString("No matches found.\n") 93 b.WriteString("No matches found.\n")
88 } else { 94 } else {
89 // Determine how many entries can fit in the terminal 95 // Determine how many entries can fit in the terminal
90 maxEntries := m.terminalHeight - 4 // Adjust for header and padding 96 maxEntries := m.terminalHeight - 4 // Adjust for header and padding
91 if maxEntries < 1 { 97 if maxEntries < 1 {
92 maxEntries = 1 // At least show one option 98 maxEntries = 1 // At least show one option
93 } 99 }
94 100
95 // Limit the number of displayed entries 101 // Limit the number of displayed entries
96 start := m.selected 102 start := m.selected
97 if start >= len(m.filteredKeys) { 103 if start >= len(m.filteredKeys) {
98 start = len(m.filteredKeys) - 1 104 start = len(m.filteredKeys) - 1
99 } 105 }
100 end := start + maxEntries 106 end := start + maxEntries
101 if end > len(m.filteredKeys) { 107 if end > len(m.filteredKeys) {
102 end = len(m.filteredKeys) 108 end = len(m.filteredKeys)
103 } 109 }
104 if start > end { 110 if start > end {
105 start = end 111 start = end
106 } 112 }
107 113
108 // Display the filtered keys with selection indicator 114 // Display the filtered keys with selection indicator
109 for i := start; i < end; i++ { 115 for i := start; i < end; i++ {
110 if i == m.selected { 116 if i == m.selected {
111 b.WriteString(fmt.Sprintf("▶ %s\n", m.filteredKeys[i].Label)) 117 b.WriteString(fmt.Sprintf("▶ %s\n", m.filteredKeys[i].Label))
112 } else { 118 } else {
113 b.WriteString(fmt.Sprintf(" %s\n", m.filteredKeys[i].Label)) 119 b.WriteString(fmt.Sprintf(" %s\n", m.filteredKeys[i].Label))
114 } 120 }
115 } 121 }
116 } 122 }
117 123
118 return b.String() 124 return b.String()
119} 125}
120 126
127
121// filterOptions filters options based on the search term 128// filterOptions filters options based on the search term
122func (m *Model) filterOptions() { 129func (m *Model) filterOptions() {
123 // Clear the filteredKeys slice before appending new matches 130 // Clear the filteredKeys slice before appending new matches
@@ -128,8 +135,17 @@ func (m *Model) filterOptions() {
128 m.filteredKeys = append(m.filteredKeys, SelectionOption{Label: value, Key: key}) 135 m.filteredKeys = append(m.filteredKeys, SelectionOption{Label: value, Key: key})
129 } 136 }
130 } 137 }
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 }
131} 146}
132 147
148
133// DynamicSelect displays a full-screen selection prompt with search 149// DynamicSelect displays a full-screen selection prompt with search
134func DynamicSelect(options map[string]string) (SelectionOption, error) { 150func DynamicSelect(options map[string]string) (SelectionOption, error) {
135 // ClearScreen() 151 // ClearScreen()