Skip to content

Commit c36bbbf

Browse files
authored
[bug] Support multiple template colours in lakectl console output (#9642)
* [bug] Support multiple template colours in lakectl console output Now e.g. `{{... | bold | red}}` formats both both _and_ red (previously this would only be red). * [bug] ColoredText.Add does _not_ modify c Thanks, Copilot!
1 parent 078a850 commit c36bbbf

File tree

2 files changed

+79
-10
lines changed

2 files changed

+79
-10
lines changed

cmd/lakectl/cmd/common_helpers.go

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -88,23 +88,54 @@ type Pagination struct {
8888
After string
8989
}
9090

91+
// ColoredText is text with an array of colors applied to it. Without it go-pretty cannot
92+
// render combinations, e.g. bold-green.
93+
type ColoredText struct {
94+
Colors text.Colors
95+
Text interface{} // Usually just a string, but anything goes
96+
}
97+
98+
// String converts c to a string with ANSI control codes.
99+
func (c ColoredText) String() string {
100+
return c.Colors.Sprint(c.Text)
101+
}
102+
103+
// Add returns c with color added to it.
104+
func (c ColoredText) Add(color text.Color) ColoredText {
105+
return ColoredText{
106+
Colors: append(c.Colors, color),
107+
Text: c.Text,
108+
}
109+
}
110+
111+
// Colored returns text as ColoredText with no Color if it is not already ColoredText.
112+
func Colored(text interface{}) ColoredText {
113+
if c, ok := text.(ColoredText); ok {
114+
return c
115+
}
116+
return ColoredText{Colors: nil, Text: text}
117+
}
118+
91119
func WriteTo(tpl string, data interface{}, w io.Writer) {
92120
templ := template.New("output")
93121
templ.Funcs(template.FuncMap{
94-
"red": func(arg interface{}) string {
95-
return text.FgHiRed.Sprint(arg)
122+
"red": func(arg interface{}) ColoredText {
123+
return Colored(arg).Add(text.FgHiRed)
124+
},
125+
"yellow": func(arg interface{}) ColoredText {
126+
return Colored(arg).Add(text.FgHiYellow)
96127
},
97-
"yellow": func(arg interface{}) string {
98-
return text.FgHiYellow.Sprint(arg)
128+
"green": func(arg interface{}) ColoredText {
129+
return Colored(arg).Add(text.FgHiGreen)
99130
},
100-
"green": func(arg interface{}) string {
101-
return text.FgHiGreen.Sprint(arg)
131+
"blue": func(arg interface{}) ColoredText {
132+
return Colored(arg).Add(text.FgHiBlue)
102133
},
103-
"blue": func(arg interface{}) string {
104-
return text.FgHiBlue.Sprint(arg)
134+
"bold": func(arg interface{}) ColoredText {
135+
return Colored(arg).Add(text.Bold)
105136
},
106-
"bold": func(arg interface{}) string {
107-
return text.Bold.Sprint(arg)
137+
"underline": func(arg interface{}) ColoredText {
138+
return Colored(arg).Add(text.Underline)
108139
},
109140
"date": func(ts int64) string {
110141
return time.Unix(ts, 0).String()

cmd/lakectl/cmd/common_helpers_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package cmd
22

33
import (
4+
"strings"
45
"testing"
6+
7+
"github.com/jedib0t/go-pretty/v6/text"
58
)
69

710
func TestIsValidAccessKeyID(t *testing.T) {
@@ -52,3 +55,38 @@ func TestIsValidSecretAccessKey(t *testing.T) {
5255
})
5356
}
5457
}
58+
59+
func TestColors(t *testing.T) {
60+
text.EnableColors()
61+
defer text.DisableColors()
62+
tests := []struct {
63+
name string
64+
template string
65+
want string
66+
}{
67+
{name: "plain", template: `abc`, want: "abc"},
68+
{name: "red", template: `{{"abc" | red}}def`, want: "\x1b[91mabc\x1b[0mdef"},
69+
{name: "yellow", template: `{{"abc" | yellow}}def`, want: "\x1b[93mabc\x1b[0mdef"},
70+
{name: "green", template: `{{"abc" | green}}def`, want: "\x1b[92mabc\x1b[0mdef"},
71+
{name: "blue", template: `{{"abc" | blue}}def`, want: "\x1b[94mabc\x1b[0mdef"},
72+
{name: "bold", template: `{{"abc" | bold}}def`, want: "\x1b[1mabc\x1b[0mdef"},
73+
{name: "underline", template: `{{"abc" | underline}}def`, want: "\x1b[4mabc\x1b[0mdef"},
74+
{name: "boldgreen", template: `{{"abc" | bold | green}}def`, want: "\x1b[1;92mabc\x1b[0mdef"},
75+
{name: "greenbold", template: `{{"abc" | green | bold}}def`, want: "\x1b[92;1mabc\x1b[0mdef"},
76+
{name: "redunderline", template: `{{"abc" | red | underline}}def`, want: "\x1b[91;4mabc\x1b[0mdef"},
77+
{name: "red-number", template: `{{2 | red}}`, want: "\x1b[91m2\x1b[0m"},
78+
}
79+
80+
for _, tc := range tests {
81+
t.Run(tc.name, func(t *testing.T) {
82+
w := strings.Builder{}
83+
WriteTo(tc.template, nil, &w)
84+
got := w.String()
85+
if got != tc.want {
86+
t.Errorf("%s got %q want %q", tc.template, got, tc.want)
87+
}
88+
// Show off the nice colors!
89+
t.Log(got)
90+
})
91+
}
92+
}

0 commit comments

Comments
 (0)