summaryrefslogtreecommitdiffstats
path: root/pkg/utils/color.go
diff options
context:
space:
mode:
Diffstat (limited to 'pkg/utils/color.go')
-rw-r--r--pkg/utils/color.go49
1 files changed, 49 insertions, 0 deletions
diff --git a/pkg/utils/color.go b/pkg/utils/color.go
new file mode 100644
index 000000000..b38dccf7f
--- /dev/null
+++ b/pkg/utils/color.go
@@ -0,0 +1,49 @@
+package utils
+
+import (
+ "fmt"
+ "regexp"
+
+ "github.com/fatih/color"
+)
+
+// ColoredString takes a string and a colour attribute and returns a colored
+// string with that attribute
+func ColoredString(str string, colorAttributes ...color.Attribute) string {
+ colour := color.New(colorAttributes...)
+ return ColoredStringDirect(str, colour)
+}
+
+// ColoredStringDirect used for aggregating a few color attributes rather than
+// just sending a single one
+func ColoredStringDirect(str string, colour *color.Color) string {
+ return colour.SprintFunc()(fmt.Sprint(str))
+}
+
+// Decolorise strips a string of color
+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 {
+ 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
+}