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

     1  package sgr
     2  
     3  type Flag [2]uint64
     4  
     5  var zeroFlag Flag
     6  
     7  func Flags(flags ...Flag) (fs Flag) {
     8  	for _, flag := range flags {
     9  		fs = fs.Add(flag)
    10  	}
    11  	return
    12  }
    13  
    14  func (f Flag) Add(flag Flag) Flag {
    15  	return Flag{f[0] | flag[0], f[1] | flag[1]}
    16  }
    17  
    18  func (f Flag) Remove(flag Flag) Flag {
    19  	return Flag{f[0] & ^flag[0], f[1] & ^flag[1]}
    20  }
    21  
    22  func (f Flag) Has(flag Flag) bool {
    23  	return f[0]&flag[0] != 0 || f[1]&flag[1] != 0
    24  }
    25  
    26  func (f Flag) HasMulti(flags ...Flag) bool {
    27  	for _, flag := range flags {
    28  		f[0] &= flag[0]
    29  		f[1] &= flag[1]
    30  	}
    31  	return f != zeroFlag
    32  }
    33  
    34  func (f Flag) Color() Flag {
    35  	f[1] &= 0xffff000000000000
    36  	f[0] &= 0x000000000000ffff | 1<<17 | 1<<19 | 1<<21
    37  	return f
    38  }
    39  
    40  func (f Flag) Meta() *SGRMeta {
    41  	return flagMetaMap[f]
    42  }
    43  
    44  func (f Flag) byte(i int) byte {
    45  	if i < 8 {
    46  		return byte((f[1] >> (i * 8)) & 0xff)
    47  	} else if i -= 8; i < 8 {
    48  		return byte((f[0] >> (i * 8)) & 0xff)
    49  	}
    50  	panic("index out of range")
    51  }
    52  
    53  var (
    54  	Reset = Flag{0, 1 << 0}
    55  
    56  	Bold                 = Flag{0, 1 << 8}
    57  	Faint                = Flag{0, 1 << 9}
    58  	Italic               = Flag{0, 1 << 10}
    59  	Fraktur              = Flag{0, 1 << 11}
    60  	Reversed             = Flag{0, 1 << 12}
    61  	NormalIntensity      = Flag{0, 1 << 13}
    62  	NotItalicBlackLetter = Flag{0, 1 << 14}
    63  	NotReversed          = Flag{0, 1 << 15}
    64  
    65  	Underline        = Flag{0, 1 << 16}
    66  	DoublyUnderlined = Flag{0, 1 << 17}
    67  	SlowBlink        = Flag{0, 1 << 18}
    68  	RapidBlink       = Flag{0, 1 << 19}
    69  	Overlined        = Flag{0, 1 << 20}
    70  	NotUnderlined    = Flag{0, 1 << 21}
    71  	NotBlinking      = Flag{0, 1 << 22}
    72  	NotOverlined     = Flag{0, 1 << 23}
    73  
    74  	Conceal                    = Flag{0, 1 << 24}
    75  	CrossedOut                 = Flag{0, 1 << 25}
    76  	DisableProportionalSpacing = Flag{0, 1 << 26}
    77  	Reveal                     = Flag{0, 1 << 27}
    78  	NotCrossedOut              = Flag{0, 1 << 28}
    79  	ProportionalSpacing        = Flag{0, 1 << 29}
    80  
    81  	Framed                  = Flag{0, 1 << 32}
    82  	Encircled               = Flag{0, 1 << 33}
    83  	Superscript             = Flag{0, 1 << 34}
    84  	Subscript               = Flag{0, 1 << 35}
    85  	NotFramedEncircled      = Flag{0, 1 << 36}
    86  	NotSuperscriptSubscript = Flag{0, 1 << 37}
    87  
    88  	IdeogramUnderline       = Flag{0, 1 << 40}
    89  	IdeogramDoubleUnderline = Flag{0, 1 << 41}
    90  	IdeogramOverline        = Flag{0, 1 << 42}
    91  	IdeogramDoubleOverline  = Flag{0, 1 << 43}
    92  	IdeogramStressMarking   = Flag{0, 1 << 44}
    93  	NoIdeogramAttributes    = Flag{0, 1 << 45}
    94  
    95  	FgBlack   = Flag{0, 1 << 48}
    96  	FgRed     = Flag{0, 1 << 49}
    97  	FgGreen   = Flag{0, 1 << 50}
    98  	FgYellow  = Flag{0, 1 << 51}
    99  	FgBlue    = Flag{0, 1 << 52}
   100  	FgMagenta = Flag{0, 1 << 53}
   101  	FgCyan    = Flag{0, 1 << 54}
   102  	FgWhite   = Flag{0, 1 << 55}
   103  
   104  	BgBlack   = Flag{0, 1 << 56}
   105  	BgRed     = Flag{0, 1 << 57}
   106  	BgGreen   = Flag{0, 1 << 58}
   107  	BgYellow  = Flag{0, 1 << 59}
   108  	BgBlue    = Flag{0, 1 << 60}
   109  	BgMagenta = Flag{0, 1 << 61}
   110  	BgCyan    = Flag{0, 1 << 62}
   111  	BgWhite   = Flag{0, 1 << 63}
   112  
   113  	FgBrightBlack   = Flag{1 << 0, 0}
   114  	FgBrightRed     = Flag{1 << 1, 0}
   115  	FgBrightGreen   = Flag{1 << 2, 0}
   116  	FgBrightYellow  = Flag{1 << 3, 0}
   117  	FgBrightBlue    = Flag{1 << 4, 0}
   118  	FgBrightMagenta = Flag{1 << 5, 0}
   119  	FgBrightCyan    = Flag{1 << 6, 0}
   120  	FgBrightWhite   = Flag{1 << 7, 0}
   121  
   122  	BgBrightBlack   = Flag{1 << 8, 0}
   123  	BgBrightRed     = Flag{1 << 9, 0}
   124  	BgBrightGreen   = Flag{1 << 10, 0}
   125  	BgBrightYellow  = Flag{1 << 11, 0}
   126  	BgBrightBlue    = Flag{1 << 12, 0}
   127  	BgBrightMagenta = Flag{1 << 13, 0}
   128  	BgBrightCyan    = Flag{1 << 14, 0}
   129  	BgBrightWhite   = Flag{1 << 15, 0}
   130  
   131  	CustomFgColor         = Flag{1 << 16, 0}
   132  	DefaultFgColor        = Flag{1 << 17, 0}
   133  	CustomBgColor         = Flag{1 << 18, 0}
   134  	DefaultBgColor        = Flag{1 << 19, 0}
   135  	CustomUnderlineColor  = Flag{1 << 20, 0}
   136  	DefaultUnderlineColor = Flag{1 << 21, 0}
   137  
   138  	FgColor256        = Flag{1 << 24, 0}
   139  	FgColorRGB        = Flag{1 << 25, 0}
   140  	BgColor256        = Flag{1 << 26, 0}
   141  	BgColorRGB        = Flag{1 << 27, 0}
   142  	UnderlineColor256 = Flag{1 << 28, 0}
   143  	UnderlineColorRGB = Flag{1 << 29, 0}
   144  
   145  	PrimaryFont      = Flag{1 << 32, 0}
   146  	AlternativeFont1 = Flag{1 << 33, 0}
   147  	AlternativeFont2 = Flag{1 << 34, 0}
   148  	AlternativeFont3 = Flag{1 << 35, 0}
   149  	AlternativeFont4 = Flag{1 << 36, 0}
   150  	AlternativeFont5 = Flag{1 << 37, 0}
   151  	AlternativeFont6 = Flag{1 << 38, 0}
   152  	AlternativeFont7 = Flag{1 << 39, 0}
   153  
   154  	AlternativeFont8 = Flag{1 << 40, 0}
   155  	AlternativeFont9 = Flag{1 << 41, 0}
   156  )
   157  
   158  func WrapFlag(s string, flag Flag) string {
   159  	meta := flag.Meta()
   160  	if meta != nil {
   161  		return WrapCode(s, meta.Code)
   162  	}
   163  	return s
   164  }