diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2022-03-19 19:12:58 +1100 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2022-03-24 20:14:41 +1100 |
commit | 1b75ed37403ac2997cb6a5ede92d87f1a1eb96b1 (patch) | |
tree | 6a1b70201901725cd689c90f32a75fe34e77a603 /pkg/utils | |
parent | bf4f06ab4e6ceefe388e0efefcc553526f3d96c2 (diff) |
many more generics
Diffstat (limited to 'pkg/utils')
-rw-r--r-- | pkg/utils/formatting.go | 31 | ||||
-rw-r--r-- | pkg/utils/fuzzy_search.go | 10 |
2 files changed, 17 insertions, 24 deletions
diff --git a/pkg/utils/formatting.go b/pkg/utils/formatting.go index d33028063..657d1d2eb 100644 --- a/pkg/utils/formatting.go +++ b/pkg/utils/formatting.go @@ -3,7 +3,9 @@ package utils import ( "strings" + "github.com/jesseduffield/generics/slices" "github.com/mattn/go-runewidth" + "github.com/samber/lo" ) // WithPadding pads a string as much as you want @@ -83,27 +85,20 @@ func getPaddedDisplayStrings(stringArrays [][]string, padWidths []int) string { } func getPadWidths(stringArrays [][]string) []int { - maxWidth := 0 - for _, stringArray := range stringArrays { - if len(stringArray) > maxWidth { - maxWidth = len(stringArray) - } - } + maxWidth := slices.MaxBy(stringArrays, func(stringArray []string) int { + return len(stringArray) + }) + if maxWidth-1 < 0 { return []int{} } - padWidths := make([]int, maxWidth-1) - for i := range padWidths { - for _, strings := range stringArrays { - uncoloredStr := Decolorise(strings[i]) - - width := runewidth.StringWidth(uncoloredStr) - if width > padWidths[i] { - padWidths[i] = width - } - } - } - return padWidths + return slices.Map(lo.Range(maxWidth-1), func(i int) int { + return slices.MaxBy(stringArrays, func(stringArray []string) int { + uncoloredStr := Decolorise(stringArray[i]) + + return runewidth.StringWidth(uncoloredStr) + }) + }) } // TruncateWithEllipsis returns a string, truncated to a certain length, with an ellipsis diff --git a/pkg/utils/fuzzy_search.go b/pkg/utils/fuzzy_search.go index 4199d6c8b..5fce3dde9 100644 --- a/pkg/utils/fuzzy_search.go +++ b/pkg/utils/fuzzy_search.go @@ -3,6 +3,7 @@ package utils import ( "sort" + "github.com/jesseduffield/generics/slices" "github.com/sahilm/fuzzy" ) @@ -14,10 +15,7 @@ func FuzzySearch(needle string, haystack []string) []string { matches := fuzzy.Find(needle, haystack) sort.Sort(matches) - result := make([]string, len(matches)) - for i, match := range matches { - result[i] = match.Str - } - - return result + return slices.Map(matches, func(match fuzzy.Match) string { + return match.Str + }) } |