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

     1  package sgr
     2  
     3  import "strings"
     4  
     5  const (
     6  	ResetCode      = "0"
     7  	BoldCode       = "1"
     8  	FaintCode      = "2"
     9  	ItalicCode     = "3"
    10  	UnderlineCode  = "4"
    11  	SlowBlinkCode  = "5"
    12  	RapidBlinkCode = "6"
    13  	ReversedCode   = "7"
    14  	ConcealCode    = "8"
    15  	CrossedOutCode = "9"
    16  
    17  	PrimaryFontCode      = "10"
    18  	AlternativeFont1Code = "11"
    19  	AlternativeFont2Code = "12"
    20  	AlternativeFont3Code = "13"
    21  	AlternativeFont4Code = "14"
    22  	AlternativeFont5Code = "15"
    23  	AlternativeFont6Code = "16"
    24  	AlternativeFont7Code = "17"
    25  	AlternativeFont8Code = "18"
    26  	AlternativeFont9Code = "19"
    27  
    28  	FrakturCode              = "20"
    29  	DoublyUnderlinedCode     = "21"
    30  	NormalIntensityCode      = "22"
    31  	NotItalicBlackLetterCode = "23"
    32  	NotUnderlinedCode        = "24"
    33  	NotBlinkingCode          = "25"
    34  	ProportionalSpacingCode  = "26"
    35  	NotReversedCode          = "27"
    36  	RevealCode               = "28"
    37  	NotCrossedOutCode        = "29"
    38  
    39  	FgBlackCode        = "30"
    40  	FgRedCode          = "31"
    41  	FgGreenCode        = "32"
    42  	FgYellowCode       = "33"
    43  	FgBlueCode         = "34"
    44  	FgMagentaCode      = "35"
    45  	FgCyanCode         = "36"
    46  	FgWhiteCode        = "37"
    47  	CustomFgColorCode  = "38"
    48  	DefaultFgColorCode = "39"
    49  
    50  	BgBlackCode        = "40"
    51  	BgRedCode          = "41"
    52  	BgGreenCode        = "42"
    53  	BgYellowCode       = "43"
    54  	BgBlueCode         = "44"
    55  	BgMagentaCode      = "45"
    56  	BgCyanCode         = "46"
    57  	BgWhiteCode        = "47"
    58  	CustomBgColorCode  = "48"
    59  	DefaultBgColorCode = "49"
    60  
    61  	DisableProportionalSpacingCode = "50"
    62  	FramedCode                     = "51"
    63  	EncircledCode                  = "52"
    64  	OverlinedCode                  = "53"
    65  	NotFramedEncircledCode         = "54"
    66  	NotOverlinedCode               = "55"
    67  	CustomUnderlineColorCode       = "58"
    68  	DefaultUnderlineColorCode      = "59"
    69  
    70  	IdeogramUnderlineCode       = "60"
    71  	IdeogramDoubleUnderlineCode = "61"
    72  	IdeogramOverlineCode        = "62"
    73  	IdeogramDoubleOverlineCode  = "63"
    74  	IdeogramStressMarkingCode   = "64"
    75  	NoIdeogramAttributesCode    = "65"
    76  
    77  	SuperscriptCode             = "73"
    78  	SubscriptCode               = "74"
    79  	NotSuperscriptSubscriptCode = "75"
    80  
    81  	FgBrightBlackCode   = "90"
    82  	FgBrightRedCode     = "91"
    83  	FgBrightGreenCode   = "92"
    84  	FgBrightYellowCode  = "93"
    85  	FgBrightBlueCode    = "94"
    86  	FgBrightMagentaCode = "95"
    87  	FgBrightCyanCode    = "96"
    88  	FgBrightWhiteCode   = "97"
    89  
    90  	BgBrightBlackCode   = "100"
    91  	BgBrightRedCode     = "101"
    92  	BgBrightGreenCode   = "102"
    93  	BgBrightYellowCode  = "103"
    94  	BgBrightBlueCode    = "104"
    95  	BgBrightMagentaCode = "105"
    96  	BgBrightCyanCode    = "106"
    97  	BgBrightWhiteCode   = "107"
    98  )
    99  
   100  func WrapCode(s string, c string) string {
   101  	return SGR_CSIHead + c + SGR_CSIEnd + s + ResetSGR_CSI
   102  }
   103  
   104  func WrapCodes(s string, cs ...string) string {
   105  	switch len(cs) {
   106  	case 0:
   107  		return ""
   108  	case 1:
   109  		return WrapCode(s, cs[0])
   110  	}
   111  	n := len(SGR_CSIHead) + len(SGR_CSIEnd) + len(cs) + len(s) + len(ResetSGR_CSI) - 1
   112  	for i := 0; i < len(cs); i++ {
   113  		n += len(cs[i])
   114  	}
   115  
   116  	var b strings.Builder
   117  	b.Grow(n)
   118  	b.WriteString(SGR_CSIHead)
   119  	b.WriteString(cs[0])
   120  	for _, c := range cs[1:] {
   121  		b.WriteByte(';')
   122  		b.WriteString(c)
   123  	}
   124  	b.WriteString(SGR_CSIEnd)
   125  	b.WriteString(s)
   126  	b.WriteString(ResetSGR_CSI)
   127  	return b.String()
   128  }