downloader/utils.go

25 lines
569 B
Go
Raw Normal View History

package main
import (
"sort"
"git.milar.in/milarin/tprint"
"git.milar.in/nyaanime/model"
)
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()
}