more tinkering.
This commit is contained in:
@@ -206,6 +206,15 @@ func (col *Color) colorful() colorful.Color {
|
||||
}
|
||||
}
|
||||
|
||||
func MustParse(raw string) Color {
|
||||
col, err := Parse(raw)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return col
|
||||
}
|
||||
|
||||
func Parse(raw string) (col Color, err error) {
|
||||
if raw == "" {
|
||||
return
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
package formattools
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func CompactIDList(ids []string) []string {
|
||||
sort.Strings(ids)
|
||||
|
||||
currentGroup := ""
|
||||
currentIDs := make([]string, 0, len(ids))
|
||||
groups := make([]string, 0, 2)
|
||||
for _, id := range ids {
|
||||
if id == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
split := strings.SplitN(id, ":", 3)
|
||||
if len(split) < 3 {
|
||||
continue
|
||||
}
|
||||
|
||||
group := id[:len(split[0])+len(split[1])+1]
|
||||
|
||||
if group != currentGroup {
|
||||
if len(currentIDs) > 0 {
|
||||
if len(currentIDs) == 1 {
|
||||
groups = append(groups, fmt.Sprintf("%s:%s", currentGroup, currentIDs[0]))
|
||||
} else {
|
||||
groups = append(groups, fmt.Sprintf("%s:{%s}", currentGroup, strings.Join(currentIDs, ",")))
|
||||
}
|
||||
currentIDs = currentIDs[:0]
|
||||
}
|
||||
currentGroup = group
|
||||
}
|
||||
|
||||
currentIDs = append(currentIDs, split[2])
|
||||
}
|
||||
|
||||
if len(currentIDs) > 0 {
|
||||
if len(currentIDs) == 1 {
|
||||
groups = append(groups, fmt.Sprintf("%s:%s", currentGroup, currentIDs[0]))
|
||||
} else {
|
||||
groups = append(groups, fmt.Sprintf("%s:{%s}", currentGroup, strings.Join(currentIDs, ",")))
|
||||
}
|
||||
}
|
||||
|
||||
return groups
|
||||
}
|
||||
@@ -1,6 +1,9 @@
|
||||
package gentools
|
||||
|
||||
import "fmt"
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func FmtSprintArray[T any](arr []T) []string {
|
||||
res := make([]string, len(arr))
|
||||
@@ -10,3 +13,7 @@ func FmtSprintArray[T any](arr []T) []string {
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
func FmtSprintArrayJoin[T any](arr []T, sep string) string {
|
||||
return strings.Join(FmtSprintArray(arr), sep)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
package gentools
|
||||
|
||||
import "strings"
|
||||
|
||||
func FlagString[T ~uint32](value T, flags []T, names []string) string {
|
||||
if value == 0 {
|
||||
return "(none)"
|
||||
}
|
||||
|
||||
parts := make([]string, 0, len(flags))
|
||||
for i, flag := range flags {
|
||||
if value&flag == flag {
|
||||
parts = append(parts, names[i])
|
||||
}
|
||||
}
|
||||
|
||||
return strings.Join(parts, "|")
|
||||
}
|
||||
Reference in New Issue
Block a user