gitee.com/sy_183/go-common@v1.0.5-0.20231205030221-958cfe129b47/sgr/csi.go (about)

     1  package sgr
     2  
     3  import "strings"
     4  
     5  const (
     6  	SGR_CSIHead = "\x1b["
     7  	SGR_CSIEnd  = "m"
     8  )
     9  
    10  const (
    11  	ResetSGR_CSI = SGR_CSIHead + ResetCode + SGR_CSIEnd
    12  
    13  	FgBlackCSI        = SGR_CSIHead + FgBlackCode + SGR_CSIEnd
    14  	FgRedCSI          = SGR_CSIHead + FgRedCode + SGR_CSIEnd
    15  	FgGreenCSI        = SGR_CSIHead + FgGreenCode + SGR_CSIEnd
    16  	FgYellowCSI       = SGR_CSIHead + FgYellowCode + SGR_CSIEnd
    17  	FgBlueCSI         = SGR_CSIHead + FgBlueCode + SGR_CSIEnd
    18  	FgMagentaCSI      = SGR_CSIHead + FgMagentaCode + SGR_CSIEnd
    19  	FgCyanCSI         = SGR_CSIHead + FgCyanCode + SGR_CSIEnd
    20  	FgWhiteCSI        = SGR_CSIHead + FgWhiteCode + SGR_CSIEnd
    21  	CustomFgColorCSI  = SGR_CSIHead + CustomFgColorCode + SGR_CSIEnd
    22  	DefaultFgColorCSI = SGR_CSIHead + DefaultFgColorCode + SGR_CSIEnd
    23  
    24  	BgBlackCSI        = SGR_CSIHead + BgBlackCode + SGR_CSIEnd
    25  	BgRedCSI          = SGR_CSIHead + BgRedCode + SGR_CSIEnd
    26  	BgGreenCSI        = SGR_CSIHead + BgGreenCode + SGR_CSIEnd
    27  	BgYellowCSI       = SGR_CSIHead + BgYellowCode + SGR_CSIEnd
    28  	BgBlueCSI         = SGR_CSIHead + BgBlueCode + SGR_CSIEnd
    29  	BgMagentaCSI      = SGR_CSIHead + BgMagentaCode + SGR_CSIEnd
    30  	BgCyanCSI         = SGR_CSIHead + BgCyanCode + SGR_CSIEnd
    31  	BgWhiteCSI        = SGR_CSIHead + BgWhiteCode + SGR_CSIEnd
    32  	CustomBgColorCSI  = SGR_CSIHead + CustomBgColorCode + SGR_CSIEnd
    33  	DefaultBgColorCSI = SGR_CSIHead + DefaultBgColorCode + SGR_CSIEnd
    34  
    35  	FgBrightBlackCSI   = SGR_CSIHead + FgBrightBlackCode + SGR_CSIEnd
    36  	FgBrightRedCSI     = SGR_CSIHead + FgBrightRedCode + SGR_CSIEnd
    37  	FgBrightGreenCSI   = SGR_CSIHead + FgBrightGreenCode + SGR_CSIEnd
    38  	FgBrightYellowCSI  = SGR_CSIHead + FgBrightYellowCode + SGR_CSIEnd
    39  	FgBrightBlueCSI    = SGR_CSIHead + FgBrightBlueCode + SGR_CSIEnd
    40  	FgBrightMagentaCSI = SGR_CSIHead + FgBrightMagentaCode + SGR_CSIEnd
    41  	FgBrightCyanCSI    = SGR_CSIHead + FgBrightCyanCode + SGR_CSIEnd
    42  	FgBrightWhiteCSI   = SGR_CSIHead + FgBrightWhiteCode + SGR_CSIEnd
    43  
    44  	BgBrightBlackCSI   = SGR_CSIHead + BgBrightBlackCode + SGR_CSIEnd
    45  	BgBrightRedCSI     = SGR_CSIHead + BgBrightRedCode + SGR_CSIEnd
    46  	BgBrightGreenCSI   = SGR_CSIHead + BgBrightGreenCode + SGR_CSIEnd
    47  	BgBrightYellowCSI  = SGR_CSIHead + BgBrightYellowCode + SGR_CSIEnd
    48  	BgBrightBlueCSI    = SGR_CSIHead + BgBrightBlueCode + SGR_CSIEnd
    49  	BgBrightMagentaCSI = SGR_CSIHead + BgBrightMagentaCode + SGR_CSIEnd
    50  	BgBrightCyanCSI    = SGR_CSIHead + BgBrightCyanCode + SGR_CSIEnd
    51  	BgBrightWhiteCSI   = SGR_CSIHead + BgBrightWhiteCode + SGR_CSIEnd
    52  )
    53  
    54  func WrapCSI(s string, csi string) string {
    55  	return csi + s + ResetSGR_CSI
    56  }
    57  
    58  func MakeCSI(codes ...string) string {
    59  	switch len(codes) {
    60  	case 0:
    61  		return ""
    62  	case 1:
    63  		return SGR_CSIHead + codes[0] + SGR_CSIEnd
    64  	}
    65  	n := len(SGR_CSIHead) + len(SGR_CSIEnd) + len(codes) - 1
    66  	for i := 0; i < len(codes); i++ {
    67  		n += len(codes[i])
    68  	}
    69  
    70  	var b strings.Builder
    71  	b.Grow(n)
    72  	b.WriteString(SGR_CSIHead)
    73  	b.WriteString(codes[0])
    74  	for _, c := range codes[1:] {
    75  		b.WriteByte(';')
    76  		b.WriteString(c)
    77  	}
    78  	b.WriteString(SGR_CSIEnd)
    79  	return b.String()
    80  }