Skip to content

Commit 8a7fd52

Browse files
committed
Refactor colorized Println methods to allow nested style (bold, eg)
1 parent 51a970c commit 8a7fd52

1 file changed

Lines changed: 14 additions & 6 deletions

File tree

‎shared/utils/cli/color/color.go‎

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,23 @@ const colorYellow string = "\033[33m"
1313
const colorLightBlue string = "\033[36m"
1414

1515
func color(color, msg string) string {
16-
return fmt.Sprintf("%s%s%s", color, msg, colorReset)
16+
return color + msg + colorReset
17+
}
18+
19+
func colorPrintln(colorFunc func(string) string, msgs ...string) {
20+
colorized := make([]string, len(msgs))
21+
for i, msg := range msgs {
22+
colorized[i] = colorFunc(msg)
23+
}
24+
fmt.Println(strings.Join(colorized, " "))
1725
}
1826

1927
func Red(msg string) string {
2028
return color(colorRed, msg)
2129
}
2230

2331
func RedPrintln(msgs ...string) {
24-
fmt.Println(Red(strings.Join(msgs, " ")))
32+
colorPrintln(Red, msgs...)
2533
}
2634

2735
func RedPrintf(format string, a ...any) {
@@ -37,7 +45,7 @@ func Green(msg string) string {
3745
}
3846

3947
func GreenPrintln(msgs ...string) {
40-
fmt.Println(Green(strings.Join(msgs, " ")))
48+
colorPrintln(Green, msgs...)
4149
}
4250

4351
func GreenPrintf(format string, a ...any) {
@@ -53,7 +61,7 @@ func Yellow(msg string) string {
5361
}
5462

5563
func YellowPrintln(msgs ...string) {
56-
fmt.Println(Yellow(strings.Join(msgs, " ")))
64+
colorPrintln(Yellow, msgs...)
5765
}
5866

5967
func YellowPrintf(format string, a ...any) {
@@ -69,7 +77,7 @@ func LightBlue(msg string) string {
6977
}
7078

7179
func LightBluePrintln(msgs ...string) {
72-
fmt.Println(LightBlue(strings.Join(msgs, " ")))
80+
colorPrintln(LightBlue, msgs...)
7381
}
7482

7583
func LightBluePrintf(format string, a ...any) {
@@ -85,7 +93,7 @@ func Bold(msg string) string {
8593
}
8694

8795
func BoldPrintln(msgs ...string) {
88-
fmt.Println(Bold(strings.Join(msgs, " ")))
96+
colorPrintln(Bold, msgs...)
8997
}
9098

9199
func BoldPrintf(format string, a ...any) {

0 commit comments

Comments
 (0)