downloader/utils.go
Timon Ringwald 9110cf4076 polishing
2022-08-25 23:25:48 +02:00

50 lines
1.2 KiB
Go

package main
import (
"sort"
"strings"
"git.milar.in/milarin/anilist"
"git.milar.in/milarin/slices"
"git.milar.in/milarin/tprint"
)
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([]Pair[K, int], 0, len(m))
for name, priority := range m {
entries = append(entries, Pair[K, int]{name, 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()
}