Efficiency of ansiColorCode function

Signed-off-by: keitosuwahara <keitosuwahara0816@gmail.com>
This commit is contained in:
keitosuwahara 2025-07-24 08:53:32 +09:00 committed by Nicolas De loof
parent 95660c5e5a
commit fb5a8644c3

View File

@ -19,6 +19,7 @@ package formatter
import ( import (
"fmt" "fmt"
"strconv" "strconv"
"strings"
"sync" "sync"
"github.com/docker/cli/cli/command" "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 // Everything about ansiColorCode color https://hyperskill.org/learn/step/18193
func ansiColorCode(code string, formatOpts ...string) string { func ansiColorCode(code string, formatOpts ...string) string {
res := "\033[" var sb strings.Builder
sb.WriteString("\033[")
for _, c := range formatOpts { 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 { func makeColorFunc(code string) colorFunc {