logic/utils.go

60 lines
1.5 KiB
Go
Raw Normal View History

2023-01-13 11:10:07 +01:00
package logic
import (
2023-01-14 11:53:36 +01:00
"fmt"
2023-01-14 11:52:17 +01:00
"sort"
2023-01-13 11:10:07 +01:00
"strings"
"git.milar.in/milarin/anilist"
"git.milar.in/milarin/slices"
2023-01-14 11:52:17 +01:00
"git.milar.in/milarin/tprint"
"git.milar.in/nyaanime/model"
2023-01-13 11:10:07 +01: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(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
}
2023-01-14 11:52:17 +01:00
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()
}
2023-01-14 11:53:36 +01:00
func PrintPriorityTables() {
fmt.Println("generated priority values:")
fmt.Print(Map2Table("language", PreferredLanguages))
fmt.Print(Map2Table("subtitle", PreferredSubtitles))
fmt.Print(Map2Table("resolution", PreferredResolutions))
fmt.Println()
}