package color import ( "strconv" "strings" ) const ( csi = "\x1b[" Clear = "\x1b[0m" ) type Attribute int // https://en.wikipedia.org/wiki/ANSI_escape_code const ( Reset Attribute = iota Bold Faint Italic Underline SlowBlink RapidBlink ReverseVideo Conceal CrossedOut ) const ( 重置 Attribute = iota 加粗 弱化 斜体 下划线 缓慢闪烁 快速闪烁 反显 消隐 划除 ) type Color int const ( Black Color = iota + 30 Red Green Yellow Blue Magenta Cyan White ) type Sequence []Attribute func (seq Sequence) String() string { var b strings.Builder b.WriteString(csi) if len(seq) > 0 { b.WriteString(strconv.Itoa(int(seq[0]))) } for _, v := range seq[1:] { b.WriteRune(';') b.WriteString(strconv.Itoa(int(v))) } b.WriteRune('m') return b.String() } func (seq Sequence) Base(attrs ...Attribute) Sequence { return append(seq, attrs...) } func (seq Sequence) Bg(c Color) Sequence { return append(seq, add(c, 10)) } func (seq Sequence) Fg(c Color) Sequence { return append(seq, Attribute(c)) } func (seq Sequence) Hi(c Color) Sequence { return append(seq, add(c, 60)) } func (seq Sequence) HiBg(c Color) Sequence { return append(seq, add(c, 70)) } func (seq Sequence) BgRGB(r, g, b int) Sequence { return append(seq, Sequence{48, 2, Attribute(r), Attribute(g), Attribute(b)}...) } func (seq Sequence) FgRGB(r, g, b int) Sequence { return append(seq, Sequence{38, 2, Attribute(r), Attribute(g), Attribute(b)}...) } func Base(attrs ...Attribute) Sequence { return Sequence(attrs) } func Bg(c Color) Sequence { return []Attribute{add(c, 10)} } func Fg(c Color) Sequence { return []Attribute{Attribute(c)} } func Hi(c Color) Sequence { return []Attribute{add(c, 60)} } func HiBg(c Color) Sequence { return []Attribute{add(c, 70)} } func add(c Color, offset int) Attribute { return Attribute(c) + Attribute(offset) }