Standardise on using lo for slice functions

We've been sometimes using lo and sometimes using my slices package, and we need to pick one
for consistency. Lo is more extensive and better maintained so we're going with that.

My slices package was a superset of go's own slices package so in some places I've just used
the official one (the methods were just wrappers anyway).

I've also moved the remaining methods into the utils package.
This commit is contained in:
Jesse Duffield
2023-07-24 13:06:42 +10:00
parent ea54cb6e9c
commit e33fe37a99
58 changed files with 212 additions and 732 deletions

View File

@@ -3,7 +3,6 @@ package utils
import (
"strings"
"github.com/jesseduffield/generics/slices"
"github.com/mattn/go-runewidth"
"github.com/samber/lo"
)
@@ -117,15 +116,15 @@ func getPaddedDisplayStrings(stringArrays [][]string, columnConfigs []ColumnConf
}
func getPadWidths(stringArrays [][]string) []int {
maxWidth := slices.MaxBy(stringArrays, func(stringArray []string) int {
maxWidth := MaxFn(stringArrays, func(stringArray []string) int {
return len(stringArray)
})
if maxWidth-1 < 0 {
return []int{}
}
return slices.Map(lo.Range(maxWidth-1), func(i int) int {
return slices.MaxBy(stringArrays, func(stringArray []string) int {
return lo.Map(lo.Range(maxWidth-1), func(i int, _ int) int {
return MaxFn(stringArrays, func(stringArray []string) int {
uncoloredStr := Decolorise(stringArray[i])
return runewidth.StringWidth(uncoloredStr)
@@ -133,6 +132,16 @@ func getPadWidths(stringArrays [][]string) []int {
})
}
func MaxFn[T any](items []T, fn func(T) int) int {
max := 0
for _, item := range items {
if fn(item) > max {
max = fn(item)
}
}
return max
}
// TruncateWithEllipsis returns a string, truncated to a certain length, with an ellipsis
func TruncateWithEllipsis(str string, limit int) string {
if runewidth.StringWidth(str) > limit && limit <= 3 {