downloader/utils.go

50 lines
1.2 KiB
Go
Raw Normal View History

2022-08-15 15:55:27 +02:00
package main
2022-08-18 14:25:37 +02:00
import (
2022-08-25 18:13:29 +02:00
"sort"
2022-08-18 14:25:37 +02:00
"strings"
"git.milar.in/milarin/anilist"
"git.milar.in/milarin/slices"
2022-08-25 18:13:29 +02:00
"git.milar.in/milarin/tprint"
2022-08-18 14:25:37 +02:00
)
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(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
2022-08-15 15:55:27 +02:00
}
2022-08-21 21:14:44 +02:00
2022-08-25 18:13:29 +02:00
func Map2Table[K comparable](title string, m map[K]int) string {
table := tprint.NewTable(title, "priority")
2022-08-21 21:14:44 +02:00
2022-08-25 18:13:29 +02:00
entries := make([]Pair[K, int], 0, len(m))
for name, priority := range m {
entries = append(entries, Pair[K, int]{name, priority})
2022-08-21 21:14:44 +02:00
}
2022-08-25 18:13:29 +02:00
sort.Slice(entries, func(i, j int) bool { return entries[i].Second > entries[j].Second })
2022-08-21 21:14:44 +02:00
2022-08-25 18:13:29 +02:00
for _, entry := range entries {
table.AddRow(entry.First, entry.Second)
}
2022-08-25 18:13:29 +02:00
return table.String()
}