gitee.com/sy_183/go-common@v1.0.5-0.20231205030221-958cfe129b47/sgr/sgr.go (about) 1 package sgr 2 3 import ( 4 "gitee.com/sy_183/go-common/slice" 5 "strconv" 6 ) 7 8 var u8ts [256]string 9 10 func initU8ts() { 11 for i := 0; i < 256; i++ { 12 u8ts[i] = strconv.Itoa(i) 13 } 14 } 15 16 func init() { 17 initU8ts() 18 } 19 20 // SGR Select Graphic Rendition 21 type SGR struct { 22 Flag Flag 23 FgColor256 Color256 24 FgColorRGB ColorRGB 25 BgColor256 Color256 26 BgColorRGB ColorRGB 27 UlColor256 Color256 28 UlColorRGB ColorRGB 29 } 30 31 func NewSGR(flags ...Flag) *SGR { 32 return &SGR{Flag: Flags(flags...)} 33 } 34 35 func (s *SGR) Options(opts ...Option) *SGR { 36 for _, opt := range opts { 37 opt.apply(s) 38 } 39 return s 40 } 41 42 func (s *SGR) CachedCodes() []string { 43 var cs []string 44 if c := cacheTable1[s.Flag.byte(1)]; c != "" { 45 cs = append(cs, c) 46 } 47 if c := cacheTable2[s.Flag.byte(2)]; c != "" { 48 cs = append(cs, c) 49 } 50 if c := cacheTable3[s.Flag.byte(3)&0b00111111]; c != "" { 51 cs = append(cs, c) 52 } 53 if c := cacheTable4[s.Flag.byte(4)&0b00111111]; c != "" { 54 cs = append(cs, c) 55 } 56 if c := cacheTable5[s.Flag.byte(5)&0b00111111]; c != "" { 57 cs = append(cs, c) 58 } 59 return cs 60 } 61 62 func (s *SGR) FgColorCodes() []string { 63 var cs []string 64 if s.Flag.Has(DefaultFgColor) { 65 cs = append(cs, DefaultFgColorCode) 66 } else if s.Flag.Has(FgColorRGB) { 67 cs = append(cs, CustomFgColorCode, "2", u8ts[s.FgColorRGB.R], u8ts[s.FgColorRGB.G], u8ts[s.FgColorRGB.B]) 68 } else if s.Flag.Has(FgColor256) { 69 cs = append(cs, CustomFgColorCode, "5", u8ts[s.FgColor256]) 70 } else if c := fgNormalColorTable[s.Flag.byte(6)]; c != "" { 71 cs = append(cs, c) 72 } else if c := fgBrightColorTable[s.Flag.byte(8)]; c != "" { 73 cs = append(cs, c) 74 } 75 return cs 76 } 77 78 func (s *SGR) BgColorCodes() []string { 79 var cs []string 80 if s.Flag.Has(DefaultBgColor) { 81 cs = append(cs, DefaultBgColorCode) 82 } else if s.Flag.Has(BgColorRGB) { 83 cs = append(cs, CustomBgColorCode, "2", u8ts[s.BgColorRGB.R], u8ts[s.BgColorRGB.G], u8ts[s.BgColorRGB.B]) 84 } else if s.Flag.Has(BgColor256) { 85 cs = append(cs, CustomBgColorCode, "5", u8ts[s.BgColor256]) 86 } else if c := bgNormalColorTable[s.Flag.byte(7)]; c != "" { 87 cs = append(cs, c) 88 } else if c := bgBrightColorTable[s.Flag.byte(9)]; c != "" { 89 cs = append(cs, c) 90 } 91 return cs 92 } 93 94 func (s *SGR) UlColorCodes() []string { 95 var cs []string 96 if s.Flag.Has(DefaultUnderlineColor) { 97 cs = append(cs, DefaultUnderlineColorCode) 98 } else if s.Flag.Has(UnderlineColorRGB) { 99 cs = append(cs, CustomUnderlineColorCode, "2", u8ts[s.UlColorRGB.R], u8ts[s.UlColorRGB.G], u8ts[s.UlColorRGB.B]) 100 } else if s.Flag.Has(UnderlineColor256) { 101 cs = append(cs, CustomUnderlineColorCode, "5", u8ts[s.UlColor256]) 102 } 103 return cs 104 } 105 106 func (s *SGR) Codes() []string { 107 if s.Flag.Has(Reset) { 108 return []string{ResetCode} 109 } 110 return slice.JoinNew(s.CachedCodes(), s.FgColorCodes(), s.BgColorCodes(), s.UlColorCodes()) 111 } 112 113 func (s *SGR) CSI() string { 114 return MakeCSI(s.Codes()...) 115 } 116 117 func (s *SGR) Wrap(str string) string { 118 return WrapCodes(str, s.Codes()...) 119 } 120 121 func WrapSGR(s string, sgr *SGR) string { 122 return WrapCodes(s, sgr.Codes()...) 123 }