1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-12 11:15:00 +02:00

fix another issue with indentation

This commit is contained in:
Jesse Duffield 2021-10-24 16:26:03 +11:00
parent 7d9461877a
commit 6457800748
4 changed files with 44 additions and 29 deletions

View File

@ -10,28 +10,6 @@ func Decolorise(str string) string {
return re.ReplaceAllString(str, "") 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 { func IsValidHexValue(v string) bool {
if len(v) != 4 && len(v) != 7 { if len(v) != 4 && len(v) != 7 {
return false return false

View File

@ -1,6 +1,8 @@
package utils package utils
import "testing" import (
"testing"
)
func TestDecolorise(t *testing.T) { func TestDecolorise(t *testing.T) {
var tests = []struct { var tests = []struct {

View File

@ -1,6 +1,10 @@
package utils package utils
import "strings" import (
"strings"
"github.com/mattn/go-runewidth"
)
// WithPadding pads a string as much as you want // WithPadding pads a string as much as you want
func WithPadding(str string, padding int) string { func WithPadding(str string, padding int) string {
@ -37,3 +41,27 @@ func getPaddedDisplayStrings(stringArrays [][]string, padWidths []int) []string
} }
return paddedDisplayStrings 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
}

View File

@ -56,11 +56,11 @@ func TestGetPaddedDisplayStrings(t *testing.T) {
// TestGetPadWidths is a function. // TestGetPadWidths is a function.
func TestGetPadWidths(t *testing.T) { func TestGetPadWidths(t *testing.T) {
type scenario struct { type scenario struct {
stringArrays [][]string input [][]string
expected []int expected []int
} }
scenarios := []scenario{ tests := []scenario{
{ {
[][]string{{""}, {""}}, [][]string{{""}, {""}},
[]int{}, []int{},
@ -73,9 +73,16 @@ func TestGetPadWidths(t *testing.T) {
[][]string{{"aa", "b", "ccc"}, {"c", "d", "e"}}, [][]string{{"aa", "b", "ccc"}, {"c", "d", "e"}},
[]int{2, 1}, []int{2, 1},
}, },
{
[][]string{{"AŁ", "b", "ccc"}, {"c", "d", "e"}},
[]int{2, 1},
},
} }
for _, s := range scenarios { for _, test := range tests {
assert.EqualValues(t, s.expected, getPadWidths(s.stringArrays)) output := getPadWidths(test.input)
if !assert.EqualValues(t, output, test.expected) {
t.Errorf("getPadWidths(%v) = %v, want %v", test.input, output, test.expected)
}
} }
} }