From 6457800748f4769b96508da5054208b28a0dee6e Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Sun, 24 Oct 2021 16:26:03 +1100 Subject: [PATCH] fix another issue with indentation --- pkg/utils/color.go | 22 ---------------------- pkg/utils/color_test.go | 4 +++- pkg/utils/formatting.go | 30 +++++++++++++++++++++++++++++- pkg/utils/formatting_test.go | 17 ++++++++++++----- 4 files changed, 44 insertions(+), 29 deletions(-) diff --git a/pkg/utils/color.go b/pkg/utils/color.go index d337aa691..84d5196d5 100644 --- a/pkg/utils/color.go +++ b/pkg/utils/color.go @@ -10,28 +10,6 @@ func Decolorise(str string) string { return re.ReplaceAllString(str, "") } -func getPadWidths(stringArrays [][]string) []int { - maxWidth := 0 - for _, stringArray := range stringArrays { - if len(stringArray) > maxWidth { - maxWidth = len(stringArray) - } - } - if maxWidth-1 < 0 { - return []int{} - } - padWidths := make([]int, maxWidth-1) - for i := range padWidths { - for _, strings := range stringArrays { - uncoloredString := Decolorise(strings[i]) - if len(uncoloredString) > padWidths[i] { - padWidths[i] = len(uncoloredString) - } - } - } - return padWidths -} - func IsValidHexValue(v string) bool { if len(v) != 4 && len(v) != 7 { return false diff --git a/pkg/utils/color_test.go b/pkg/utils/color_test.go index 8fc6759ca..37144e955 100644 --- a/pkg/utils/color_test.go +++ b/pkg/utils/color_test.go @@ -1,6 +1,8 @@ package utils -import "testing" +import ( + "testing" +) func TestDecolorise(t *testing.T) { var tests = []struct { diff --git a/pkg/utils/formatting.go b/pkg/utils/formatting.go index 3b4b35bb8..8a439bc32 100644 --- a/pkg/utils/formatting.go +++ b/pkg/utils/formatting.go @@ -1,6 +1,10 @@ package utils -import "strings" +import ( + "strings" + + "github.com/mattn/go-runewidth" +) // WithPadding pads a string as much as you want func WithPadding(str string, padding int) string { @@ -37,3 +41,27 @@ func getPaddedDisplayStrings(stringArrays [][]string, padWidths []int) []string } return paddedDisplayStrings } + +func getPadWidths(stringArrays [][]string) []int { + maxWidth := 0 + for _, stringArray := range stringArrays { + if len(stringArray) > maxWidth { + maxWidth = len(stringArray) + } + } + if maxWidth-1 < 0 { + return []int{} + } + padWidths := make([]int, maxWidth-1) + for i := range padWidths { + for _, strings := range stringArrays { + uncoloredString := Decolorise(strings[i]) + + width := runewidth.StringWidth(uncoloredString) + if width > padWidths[i] { + padWidths[i] = width + } + } + } + return padWidths +} diff --git a/pkg/utils/formatting_test.go b/pkg/utils/formatting_test.go index 3fe3f22f0..26d560766 100644 --- a/pkg/utils/formatting_test.go +++ b/pkg/utils/formatting_test.go @@ -56,11 +56,11 @@ func TestGetPaddedDisplayStrings(t *testing.T) { // TestGetPadWidths is a function. func TestGetPadWidths(t *testing.T) { type scenario struct { - stringArrays [][]string - expected []int + input [][]string + expected []int } - scenarios := []scenario{ + tests := []scenario{ { [][]string{{""}, {""}}, []int{}, @@ -73,9 +73,16 @@ func TestGetPadWidths(t *testing.T) { [][]string{{"aa", "b", "ccc"}, {"c", "d", "e"}}, []int{2, 1}, }, + { + [][]string{{"AŁ", "b", "ccc"}, {"c", "d", "e"}}, + []int{2, 1}, + }, } - for _, s := range scenarios { - assert.EqualValues(t, s.expected, getPadWidths(s.stringArrays)) + for _, test := range tests { + output := getPadWidths(test.input) + if !assert.EqualValues(t, output, test.expected) { + t.Errorf("getPadWidths(%v) = %v, want %v", test.input, output, test.expected) + } } }