downloader/utils.go

25 lines
569 B
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
2022-08-25 18:13:29 +02:00
"git.milar.in/milarin/tprint"
"git.milar.in/nyaanime/model"
2022-08-18 14:25:37 +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
entries := make([]model.Pair[K, int], 0, len(m))
2022-08-25 18:13:29 +02:00
for name, priority := range m {
entries = append(entries, model.Pair[K, int]{First: name, Second: 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()
}