logic/utils.go

60 lines
1.5 KiB
Go

package logic
import (
"fmt"
"sort"
"strings"
"git.milar.in/milarin/anilist"
"git.milar.in/milarin/slices"
"git.milar.in/milarin/tprint"
"git.milar.in/nyaanime/model"
)
var AllMediaListStatuses = []anilist.MediaListStatus{
anilist.MediaListStatusCurrent,
anilist.MediaListStatusPlanning,
anilist.MediaListStatusCompleted,
anilist.MediaListStatusDropped,
anilist.MediaListStatusPaused,
anilist.MediaListStatusRepeating,
}
func ParseMediaListStatus(str string) (anilist.MediaListStatus, error) {
s := anilist.MediaListStatus(strings.ToUpper(str))
allStatusesStr := slices.Map(AllMediaListStatuses, func(status anilist.MediaListStatus) string {
return string(status)
})
if !slices.Contains(AllMediaListStatuses, s) {
return s, ErrInvalidAnimeStatus.New(s, strings.Join(allStatusesStr, ","))
}
return s, nil
}
func Map2Table[K comparable](title string, m map[K]int) string {
table := tprint.NewTable(title, "priority")
entries := make([]model.Pair[K, int], 0, len(m))
for name, priority := range m {
entries = append(entries, model.Pair[K, int]{First: name, Second: priority})
}
sort.Slice(entries, func(i, j int) bool { return entries[i].Second > entries[j].Second })
for _, entry := range entries {
table.AddRow(entry.First, entry.Second)
}
return table.String()
}
func PrintPriorityTables() {
fmt.Println("generated priority values:")
fmt.Print(Map2Table("language", PreferredLanguages))
fmt.Print(Map2Table("subtitle", PreferredSubtitles))
fmt.Print(Map2Table("resolution", PreferredResolutions))
fmt.Println()
}