From 639df512f3c7cb97ba7a4ab904f52457617384d0 Mon Sep 17 00:00:00 2001 From: Jesse Duffield Duffield Date: Sun, 24 Feb 2019 17:05:17 +1100 Subject: decolorise strings before calculating padwidths --- pkg/utils/utils.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'pkg') diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index 417823b84..b704c2576 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -7,6 +7,7 @@ import ( "os" "path/filepath" "reflect" + "regexp" "strings" "time" @@ -159,6 +160,11 @@ func renderDisplayableList(items []Displayable, isFocused bool) (string, error) return strings.Join(paddedDisplayStrings, "\n"), nil } +func decolorise(str string) string { + re := regexp.MustCompile(`\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]`) + return re.ReplaceAllString(str, "") +} + func getPadWidths(stringArrays [][]string) []int { if len(stringArrays[0]) <= 1 { return []int{} @@ -166,8 +172,9 @@ func getPadWidths(stringArrays [][]string) []int { padWidths := make([]int, len(stringArrays[0])-1) for i := range padWidths { for _, strings := range stringArrays { - if len(strings[i]) > padWidths[i] { - padWidths[i] = len(strings[i]) + uncoloredString := decolorise(strings[i]) + if len(uncoloredString) > padWidths[i] { + padWidths[i] = len(uncoloredString) } } } -- cgit v1.2.3