Commit a99fd31522
Unregistered key
Layout: unified · split
internal/anilist.go +99 −17
| @@ -8,6 +8,7 @@ import ( | |||
| 8 | "net/http" | 8 | "net/http" |
| 9 | "os" | 9 | "os" |
| 10 | "path/filepath" | 10 | "path/filepath" |
| 11 | "sort" | ||
| 11 | "strconv" | 12 | "strconv" |
| 12 | "strings" | 13 | "strings" |
| 13 | ) | 14 | ) |
| @@ -85,13 +86,61 @@ func GetAnimeMapPreview(animeList AnimeList) map[string]RofiSelectPreview { | |||
| 85 | return animeMap | 86 | return animeMap |
| 86 | } | 87 | } |
| 87 | 88 | ||
| 89 | // fuzzy matching w/ Levenshtein distance | ||
| 90 | func levenshtein(a, b string) int { | ||
| 91 | a = strings.ToLower(a) | ||
| 92 | b = strings.ToLower(b) | ||
| 93 | ar, br := []rune(a), []rune(b) | ||
| 94 | alen, blen := len(ar), len(br) | ||
| 95 | if alen == 0 { | ||
| 96 | return blen | ||
| 97 | } | ||
| 98 | if blen == 0 { | ||
| 99 | return alen | ||
| 100 | } | ||
| 101 | matrix := make([][]int, alen+1) | ||
| 102 | for i := range matrix { | ||
| 103 | matrix[i] = make([]int, blen+1) | ||
| 104 | } | ||
| 105 | for i := 0; i <= alen; i++ { | ||
| 106 | matrix[i][0] = i | ||
| 107 | } | ||
| 108 | for j := 0; j <= blen; j++ { | ||
| 109 | matrix[0][j] = j | ||
| 110 | } | ||
| 111 | for i := 1; i <= alen; i++ { | ||
| 112 | for j := 1; j <= blen; j++ { | ||
| 113 | cost := 0 | ||
| 114 | if ar[i-1] != br[j-1] { | ||
| 115 | cost = 1 | ||
| 116 | } | ||
| 117 | matrix[i][j] = min3( | ||
| 118 | matrix[i-1][j]+1, | ||
| 119 | matrix[i][j-1]+1, | ||
| 120 | matrix[i-1][j-1]+cost, | ||
| 121 | ) | ||
| 122 | } | ||
| 123 | } | ||
| 124 | return matrix[alen][blen] | ||
| 125 | } | ||
| 126 | |||
| 127 | func min3(a, b, c int) int { | ||
| 128 | if a < b && a < c { | ||
| 129 | return a | ||
| 130 | } | ||
| 131 | if b < c { | ||
| 132 | return b | ||
| 133 | } | ||
| 134 | return c | ||
| 135 | } | ||
| 136 | |||
| 88 | // SearchAnimeAnilist sends the query to AniList and returns a map of title to ID | 137 | // SearchAnimeAnilist sends the query to AniList and returns a map of title to ID |
| 89 | func SearchAnimeAnilistPreview(query, token string) (map[string]RofiSelectPreview, error) { | 138 | func SearchAnimeAnilistPreview(query, token string) (map[string]RofiSelectPreview, error) { |
| 90 | url := "https://graphql.anilist.co" | 139 | url := "https://graphql.anilist.co" |
| 91 | 140 | ||
| 92 | queryString := ` | 141 | queryString := ` |
| 93 | query ($search: String) { | 142 | query ($search: String) { |
| 94 | Page(page: 1, perPage: 10) { | 143 | Page(page: 1, perPage: 50) { |
| 95 | media(search: $search, type: ANIME) { | 144 | media(search: $search, type: ANIME) { |
| 96 | id | 145 | id |
| 97 | title { | 146 | title { |
| @@ -149,29 +198,45 @@ func SearchAnimeAnilistPreview(query, token string) (map[string]RofiSelectPrevie | |||
| 149 | animeList := responseData["data"].Page.Media | 198 | animeList := responseData["data"].Page.Media |
| 150 | animeDict := make(map[string]RofiSelectPreview) | 199 | animeDict := make(map[string]RofiSelectPreview) |
| 151 | 200 | ||
| 152 | // Map titles and cover images to their IDs | 201 | type scoredAnime struct { |
| 202 | id string | ||
| 203 | title string | ||
| 204 | cover string | ||
| 205 | score int | ||
| 206 | } | ||
| 207 | var scored []scoredAnime | ||
| 153 | for _, anime := range animeList { | 208 | for _, anime := range animeList { |
| 154 | idStr := strconv.Itoa(anime.ID) | 209 | idStr := strconv.Itoa(anime.ID) |
| 155 | title := anime.Title.English | 210 | title := anime.Title.English |
| 156 | if title == "" { | 211 | if title == "" { |
| 157 | title = anime.Title.Romaji | 212 | title = anime.Title.Romaji |
| 158 | } | 213 | } |
| 159 | animeDict[idStr] = RofiSelectPreview{ | 214 | cover := anime.CoverImage.Large |
| 160 | Title: title, | 215 | score := levenshtein(title, query) |
| 161 | CoverImage: anime.CoverImage.Large, | 216 | scored = append(scored, scoredAnime{idStr, title, cover, score}) |
| 217 | } | ||
| 218 | sort.Slice(scored, func(i, j int) bool { | ||
| 219 | return scored[i].score < scored[j].score | ||
| 220 | }) | ||
| 221 | for i, s := range scored { | ||
| 222 | if i >= 10 { | ||
| 223 | break | ||
| 224 | } | ||
| 225 | animeDict[s.id] = RofiSelectPreview{ | ||
| 226 | Title: s.title, | ||
| 227 | CoverImage: s.cover, | ||
| 162 | } | 228 | } |
| 163 | } | 229 | } |
| 164 | |||
| 165 | return animeDict, nil | 230 | return animeDict, nil |
| 166 | } | 231 | } |
| 167 | 232 | ||
| 168 | // SearchAnimeAnilist sends the query to AniList and returns a map of title to ID | 233 | // SearchAnimeAnilist sends the query to AniList and returns a map of title to ID |
| 169 | func SearchAnimeAnilist(query, token string) (map[string]string, error) { | 234 | func SearchAnimeAnilist(query, token string) ([]SelectionOption, error) { |
| 170 | url := "https://graphql.anilist.co" | 235 | url := "https://graphql.anilist.co" |
| 171 | 236 | ||
| 172 | queryString := ` | 237 | queryString := ` |
| 173 | query ($search: String) { | 238 | query ($search: String) { |
| 174 | Page(page: 1, perPage: 10) { | 239 | Page(page: 1, perPage: 50) { |
| 175 | media(search: $search, type: ANIME) { | 240 | media(search: $search, type: ANIME) { |
| 176 | id | 241 | id |
| 177 | title { | 242 | title { |
| @@ -224,19 +289,36 @@ func SearchAnimeAnilist(query, token string) (map[string]string, error) { | |||
| 224 | } | 289 | } |
| 225 | 290 | ||
| 226 | animeList := responseData["data"].Page.Media | 291 | animeList := responseData["data"].Page.Media |
| 227 | animeDict := make(map[string]string) | 292 | var results []SelectionOption |
| 228 | 293 | ||
| 229 | // Map titles to their IDs as strings | 294 | type scoredAnime struct { |
| 295 | id string | ||
| 296 | title string | ||
| 297 | score int | ||
| 298 | } | ||
| 299 | var scored []scoredAnime | ||
| 230 | for _, anime := range animeList { | 300 | for _, anime := range animeList { |
| 231 | idStr := strconv.Itoa(anime.ID) // Convert ID to string | 301 | idStr := strconv.Itoa(anime.ID) |
| 232 | if anime.Title.English != "" { | 302 | title := anime.Title.English |
| 233 | animeDict[idStr] = anime.Title.English | 303 | if title == "" { |
| 234 | } else { | 304 | title = anime.Title.Romaji |
| 235 | animeDict[idStr] = anime.Title.Romaji | ||
| 236 | } | 305 | } |
| 306 | score := levenshtein(title, query) | ||
| 307 | scored = append(scored, scoredAnime{idStr, title, score}) | ||
| 237 | } | 308 | } |
| 238 | 309 | sort.Slice(scored, func(i, j int) bool { | |
| 239 | return animeDict, nil | 310 | return scored[i].score < scored[j].score |
| 311 | }) | ||
| 312 | for i, s := range scored { | ||
| 313 | if i >= 10 { | ||
| 314 | break | ||
| 315 | } | ||
| 316 | results = append(results, SelectionOption{ | ||
| 317 | Key: s.id, | ||
| 318 | Label: s.title, | ||
| 319 | }) | ||
| 320 | } | ||
| 321 | return results, nil | ||
| 240 | } | 322 | } |
| 241 | 323 | ||
| 242 | // Function to get AniList user ID and username | 324 | // Function to get AniList user ID and username |
internal/curd.go +7 −5
| @@ -14,7 +14,7 @@ import ( | |||
| 14 | "strconv" | 14 | "strconv" |
| 15 | "strings" | 15 | "strings" |
| 16 | "time" | 16 | "time" |
| 17 | 17 | "bufio" | |
| 18 | "github.com/gen2brain/beeep" | 18 | "github.com/gen2brain/beeep" |
| 19 | ) | 19 | ) |
| 20 | 20 | ||
| @@ -550,8 +550,8 @@ func UpdateCurd(repo, fileName string) error { | |||
| 550 | 550 | ||
| 551 | func AddNewAnime(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAnimes *[]Anime) SelectionOption { | 551 | func AddNewAnime(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseAnimes *[]Anime) SelectionOption { |
| 552 | var query string | 552 | var query string |
| 553 | var animeMap map[string]string | ||
| 554 | var animeMapPreview map[string]RofiSelectPreview | 553 | var animeMapPreview map[string]RofiSelectPreview |
| 554 | var animeOptions []SelectionOption | ||
| 555 | var err error | 555 | var err error |
| 556 | var anilistSelectedOption SelectionOption | 556 | var anilistSelectedOption SelectionOption |
| 557 | 557 | ||
| @@ -564,12 +564,14 @@ func AddNewAnime(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseA | |||
| 564 | query = userInput | 564 | query = userInput |
| 565 | } else { | 565 | } else { |
| 566 | CurdOut("Enter the anime name:") | 566 | CurdOut("Enter the anime name:") |
| 567 | fmt.Scanln(&query) | 567 | reader := bufio.NewReader(os.Stdin) |
| 568 | input, _ := reader.ReadString('\n') | ||
| 569 | query = strings.TrimSpace(input) | ||
| 568 | } | 570 | } |
| 569 | if userCurdConfig.RofiSelection && userCurdConfig.ImagePreview { | 571 | if userCurdConfig.RofiSelection && userCurdConfig.ImagePreview { |
| 570 | animeMapPreview, err = SearchAnimeAnilistPreview(query, user.Token) | 572 | animeMapPreview, err = SearchAnimeAnilistPreview(query, user.Token) |
| 571 | } else { | 573 | } else { |
| 572 | animeMap, err = SearchAnimeAnilist(query, user.Token) | 574 | animeOptions, err = SearchAnimeAnilist(query, user.Token) |
| 573 | } | 575 | } |
| 574 | if err != nil { | 576 | if err != nil { |
| 575 | Log(fmt.Sprintf("Failed to search anime: %v", err)) | 577 | Log(fmt.Sprintf("Failed to search anime: %v", err)) |
| @@ -578,7 +580,7 @@ func AddNewAnime(userCurdConfig *CurdConfig, anime *Anime, user *User, databaseA | |||
| 578 | if userCurdConfig.RofiSelection && userCurdConfig.ImagePreview { | 580 | if userCurdConfig.RofiSelection && userCurdConfig.ImagePreview { |
| 579 | anilistSelectedOption, err = DynamicSelectPreview(animeMapPreview, false) | 581 | anilistSelectedOption, err = DynamicSelectPreview(animeMapPreview, false) |
| 580 | } else { | 582 | } else { |
| 581 | anilistSelectedOption, err = DynamicSelect(animeMap) | 583 | anilistSelectedOption, err = DynamicSelectFromSlice(animeOptions) |
| 582 | } | 584 | } |
| 583 | 585 | ||
| 584 | if anilistSelectedOption.Key == "-1" { | 586 | if anilistSelectedOption.Key == "-1" { |
internal/selection_menu.go +83 −19
| @@ -25,14 +25,16 @@ type SelectionOption struct { | |||
| 25 | 25 | ||
| 26 | // Model represents the application state for the selection prompt | 26 | // Model represents the application state for the selection prompt |
| 27 | type Model struct { | 27 | type Model struct { |
| 28 | options map[string]string | 28 | options map[string]string |
| 29 | filter string | 29 | filter string |
| 30 | filteredKeys []SelectionOption | 30 | filteredKeys []SelectionOption |
| 31 | selected int | 31 | selected int |
| 32 | terminalWidth int | 32 | terminalWidth int |
| 33 | terminalHeight int | 33 | terminalHeight int |
| 34 | scrollOffset int // Track the topmost visible item | 34 | scrollOffset int // Track the topmost visible item |
| 35 | addNewOption bool // Add this field | 35 | addNewOption bool |
| 36 | useSliceMode bool // Track if we're using slice-based filtering | ||
| 37 | originalOptions []SelectionOption // Store original options for slice-based filtering | ||
| 36 | } | 38 | } |
| 37 | 39 | ||
| 38 | var ( | 40 | var ( |
| @@ -183,21 +185,31 @@ func (m Model) visibleItemsCount() int { | |||
| 183 | func (m *Model) filterOptions() { | 185 | func (m *Model) filterOptions() { |
| 184 | m.filteredKeys = []SelectionOption{} | 186 | m.filteredKeys = []SelectionOption{} |
| 185 | 187 | ||
| 186 | for key, value := range m.options { | 188 | if m.useSliceMode { |
| 189 | // slice-based filtering, filter from the original options | ||
| 190 | for _, option := range m.originalOptions { | ||
| 191 | if strings.Contains(strings.ToLower(option.Label), strings.ToLower(m.filter)) { | ||
| 192 | m.filteredKeys = append(m.filteredKeys, option) | ||
| 193 | } | ||
| 194 | } | ||
| 195 | } else { | ||
| 196 | // map-based filtering | ||
| 197 | for key, value := range m.options { | ||
| 187 | // When the key is " ", compare and display using the value instead | 198 | // When the key is " ", compare and display using the value instead |
| 188 | if key == " " { | 199 | if key == " " { |
| 189 | if strings.Contains(strings.ToLower(value), strings.ToLower(m.filter)) { | 200 | if strings.Contains(strings.ToLower(value), strings.ToLower(m.filter)) { |
| 201 | m.filteredKeys = append(m.filteredKeys, SelectionOption{Label: value, Key: key}) | ||
| 202 | } | ||
| 203 | } else if strings.Contains(strings.ToLower(value), strings.ToLower(m.filter)) { | ||
| 190 | m.filteredKeys = append(m.filteredKeys, SelectionOption{Label: value, Key: key}) | 204 | m.filteredKeys = append(m.filteredKeys, SelectionOption{Label: value, Key: key}) |
| 191 | } | 205 | } |
| 192 | } else if strings.Contains(strings.ToLower(value), strings.ToLower(m.filter)) { | ||
| 193 | m.filteredKeys = append(m.filteredKeys, SelectionOption{Label: value, Key: key}) | ||
| 194 | } | 206 | } |
| 195 | } | ||
| 196 | 207 | ||
| 197 | // Sort the filtered options alphabetically | 208 | // Sort the filtered options alphabetically |
| 198 | sort.Slice(m.filteredKeys, func(i, j int) bool { | 209 | sort.Slice(m.filteredKeys, func(i, j int) bool { |
| 199 | return m.filteredKeys[i].Label < m.filteredKeys[j].Label | 210 | return m.filteredKeys[i].Label < m.filteredKeys[j].Label |
| 200 | }) | 211 | }) |
| 212 | } | ||
| 201 | 213 | ||
| 202 | // Add "Add new anime" option if enabled | 214 | // Add "Add new anime" option if enabled |
| 203 | if m.addNewOption { | 215 | if m.addNewOption { |
| @@ -420,6 +432,55 @@ func RofiSelect(options map[string]string) (SelectionOption, error) { | |||
| 420 | return SelectionOption{}, fmt.Errorf("selected option not found in original list") | 432 | return SelectionOption{}, fmt.Errorf("selected option not found in original list") |
| 421 | } | 433 | } |
| 422 | 434 | ||
| 435 | func DynamicSelectFromSlice(options []SelectionOption) (SelectionOption, error) { | ||
| 436 | if GetGlobalConfig().RofiSelection { | ||
| 437 | optionsMap := make(map[string]string) | ||
| 438 | for _, option := range options { | ||
| 439 | optionsMap[option.Key] = option.Label | ||
| 440 | } | ||
| 441 | return RofiSelect(optionsMap) | ||
| 442 | } | ||
| 443 | |||
| 444 | // slice to maintain order | ||
| 445 | var orderedOptions []SelectionOption | ||
| 446 | for _, option := range options { | ||
| 447 | orderedOptions = append(orderedOptions, option) | ||
| 448 | } | ||
| 449 | |||
| 450 | model := &Model{ | ||
| 451 | options: make(map[string]string), // empty map since we're using slice | ||
| 452 | filteredKeys: orderedOptions, | ||
| 453 | useSliceMode: true, // slice-based filtering | ||
| 454 | originalOptions: orderedOptions, // store original options for filtering | ||
| 455 | } | ||
| 456 | |||
| 457 | model.filteredKeys = append(model.filteredKeys, SelectionOption{ | ||
| 458 | Label: "Quit", | ||
| 459 | Key: "-1", | ||
| 460 | }) | ||
| 461 | |||
| 462 | p := tea.NewProgram(model) | ||
| 463 | |||
| 464 | finalModel, err := p.Run() | ||
| 465 | if err != nil { | ||
| 466 | return SelectionOption{}, err | ||
| 467 | } | ||
| 468 | |||
| 469 | // resetting terminal state | ||
| 470 | fmt.Print("\033[?25h") // show cursor | ||
| 471 | fmt.Print("\033[?7h") // line wrapping | ||
| 472 | |||
| 473 | finalSelectionModel, ok := finalModel.(*Model) | ||
| 474 | if !ok { | ||
| 475 | return SelectionOption{}, fmt.Errorf("unexpected model type") | ||
| 476 | } | ||
| 477 | |||
| 478 | if finalSelectionModel.selected < len(finalSelectionModel.filteredKeys) { | ||
| 479 | return finalSelectionModel.filteredKeys[finalSelectionModel.selected], nil | ||
| 480 | } | ||
| 481 | return SelectionOption{}, nil | ||
| 482 | } | ||
| 483 | |||
| 423 | // DynamicSelect displays a simple selection prompt without extra features | 484 | // DynamicSelect displays a simple selection prompt without extra features |
| 424 | func DynamicSelect(options map[string]string) (SelectionOption, error) { | 485 | func DynamicSelect(options map[string]string) (SelectionOption, error) { |
| 425 | if GetGlobalConfig().RofiSelection { | 486 | if GetGlobalConfig().RofiSelection { |
| @@ -444,7 +505,10 @@ func DynamicSelect(options map[string]string) (SelectionOption, error) { | |||
| 444 | } | 505 | } |
| 445 | } | 506 | } |
| 446 | } else { | 507 | } else { |
| 447 | // For other selections, maintain alphabetical order | 508 | // For other selections, preserve the order as passed in |
| 509 | // Since Go maps don't preserve insertion order, we need to handle this differently | ||
| 510 | // For now, we'll use the order as it comes from the map iteration | ||
| 511 | // (This will be random, but it's better than alphabetical sorting for search results) | ||
| 448 | for key, value := range options { | 512 | for key, value := range options { |
| 449 | orderedOptions = append(orderedOptions, SelectionOption{ | 513 | orderedOptions = append(orderedOptions, SelectionOption{ |
| 450 | Key: key, | 514 | Key: key, |