Skip to content

Commit fb5a864

Browse files
suwakeindeloof
authored andcommitted
Efficiency of ansiColorCode function
Signed-off-by: keitosuwahara <[email protected]>
1 parent 95660c5 commit fb5a864

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

cmd/formatter/colors.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package formatter
1919
import (
2020
"fmt"
2121
"strconv"
22+
"strings"
2223
"sync"
2324

2425
"github.com/docker/cli/cli/command"
@@ -91,11 +92,15 @@ func ansiColor(code, s string, formatOpts ...string) string {
9192

9293
// Everything about ansiColorCode color https://hyperskill.org/learn/step/18193
9394
func ansiColorCode(code string, formatOpts ...string) string {
94-
res := "\033["
95+
var sb strings.Builder
96+
sb.WriteString("\033[")
9597
for _, c := range formatOpts {
96-
res = fmt.Sprintf("%s%s;", res, c)
98+
sb.WriteString(c)
99+
sb.WriteString(";")
97100
}
98-
return fmt.Sprintf("%s%sm", res, code)
101+
sb.WriteString(code)
102+
sb.WriteString("m")
103+
return sb.String()
99104
}
100105

101106
func makeColorFunc(code string) colorFunc {

0 commit comments

Comments
 (0)