From fb5a8644c32e800ab4fc0bc3c13c18a77a11a395 Mon Sep 17 00:00:00 2001 From: keitosuwahara Date: Thu, 24 Jul 2025 08:53:32 +0900 Subject: [PATCH] Efficiency of ansiColorCode function Signed-off-by: keitosuwahara --- cmd/formatter/colors.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/cmd/formatter/colors.go b/cmd/formatter/colors.go index cc13bc209..1151101c1 100644 --- a/cmd/formatter/colors.go +++ b/cmd/formatter/colors.go @@ -19,6 +19,7 @@ package formatter import ( "fmt" "strconv" + "strings" "sync" "github.com/docker/cli/cli/command" @@ -91,11 +92,15 @@ func ansiColor(code, s string, formatOpts ...string) string { // Everything about ansiColorCode color https://hyperskill.org/learn/step/18193 func ansiColorCode(code string, formatOpts ...string) string { - res := "\033[" + var sb strings.Builder + sb.WriteString("\033[") for _, c := range formatOpts { - res = fmt.Sprintf("%s%s;", res, c) + sb.WriteString(c) + sb.WriteString(";") } - return fmt.Sprintf("%s%sm", res, code) + sb.WriteString(code) + sb.WriteString("m") + return sb.String() } func makeColorFunc(code string) colorFunc {