package ansi
import (
)
const ResetStyle = "\x1b[m"
type Attr = int
type Style []string
func ( ...Attr) Style {
if len() == 0 {
return Style{}
}
:= make(Style, 0, len())
for , := range {
, := attrStrings[]
if {
= append(, )
} else {
if < 0 {
= 0
}
= append(, strconv.Itoa())
}
}
return
}
func ( Style) () string {
if len() == 0 {
return ResetStyle
}
return "\x1b[" + strings.Join(, ";") + "m"
}
func ( Style) ( string) string {
if len() == 0 {
return
}
return .String() + + ResetStyle
}
func ( Style) () Style {
return append(, attrReset)
}
func ( Style) () Style {
return append(, attrBold)
}
func ( Style) () Style {
return append(, attrFaint)
}
func ( Style) ( bool) Style {
if {
return append(, attrItalic)
}
return append(, attrNoItalic)
}
func ( Style) ( bool) Style {
if {
return append(, attrUnderline)
}
return append(, attrNoUnderline)
}
func ( Style) ( Underline) Style {
switch {
case UnderlineNone:
return .Underline(false)
case UnderlineSingle:
return .Underline(true)
case UnderlineDouble:
return append(, underlineDouble)
case UnderlineCurly:
return append(, underlineCurly)
case UnderlineDotted:
return append(, underlineDotted)
case UnderlineDashed:
return append(, underlineDashed)
}
return
}
func ( Style) ( bool) Style {
if {
return append(, attrBlink)
}
return append(, attrNoBlink)
}
func ( Style) ( bool) Style {
if {
return append(, attrRapidBlink)
}
return append(, attrNoBlink)
}
func ( Style) ( bool) Style {
if {
return append(, attrReverse)
}
return append(, attrNoReverse)
}
func ( Style) ( bool) Style {
if {
return append(, attrConceal)
}
return append(, attrNoConceal)
}
func ( Style) ( bool) Style {
if {
return append(, attrStrikethrough)
}
return append(, attrNoStrikethrough)
}
func ( Style) () Style {
return append(, attrNormalIntensity)
}
func ( Style) () Style {
return append(, attrNoItalic)
}
func ( Style) () Style {
return append(, attrNoUnderline)
}
func ( Style) () Style {
return append(, attrNoBlink)
}
func ( Style) () Style {
return append(, attrNoReverse)
}
func ( Style) () Style {
return append(, attrNoConceal)
}
func ( Style) () Style {
return append(, attrNoStrikethrough)
}
func ( Style) () Style {
return append(, attrDefaultForegroundColor)
}
func ( Style) () Style {
return append(, attrDefaultBackgroundColor)
}
func ( Style) () Style {
return append(, attrDefaultUnderlineColor)
}
func ( Style) ( Color) Style {
if == nil {
return append(, attrDefaultForegroundColor)
}
return append(, foregroundColorString())
}
func ( Style) ( Color) Style {
if == nil {
return append(, attrDefaultBackgroundColor)
}
return append(, backgroundColorString())
}
func ( Style) ( Color) Style {
if == nil {
return append(, attrDefaultUnderlineColor)
}
return append(, underlineColorString())
}
type Underline = byte
type UnderlineStyle = byte
const (
underlineDouble = "4:2"
underlineCurly = "4:3"
underlineDotted = "4:4"
underlineDashed = "4:5"
)
const (
UnderlineNone Underline = iota
UnderlineSingle
UnderlineDouble
UnderlineCurly
UnderlineDotted
UnderlineDashed
)
const (
NoUnderlineStyle Underline = iota
SingleUnderlineStyle
DoubleUnderlineStyle
CurlyUnderlineStyle
DottedUnderlineStyle
DashedUnderlineStyle
)
const (
UnderlineStyleNone Underline = iota
UnderlineStyleSingle
UnderlineStyleDouble
UnderlineStyleCurly
UnderlineStyleDotted
UnderlineStyleDashed
)
const (
AttrReset Attr = 0
AttrBold Attr = 1
AttrFaint Attr = 2
AttrItalic Attr = 3
AttrUnderline Attr = 4
AttrBlink Attr = 5
AttrRapidBlink Attr = 6
AttrReverse Attr = 7
AttrConceal Attr = 8
AttrStrikethrough Attr = 9
AttrNormalIntensity Attr = 22
AttrNoItalic Attr = 23
AttrNoUnderline Attr = 24
AttrNoBlink Attr = 25
AttrNoReverse Attr = 27
AttrNoConceal Attr = 28
AttrNoStrikethrough Attr = 29
AttrBlackForegroundColor Attr = 30
AttrRedForegroundColor Attr = 31
AttrGreenForegroundColor Attr = 32
AttrYellowForegroundColor Attr = 33
AttrBlueForegroundColor Attr = 34
AttrMagentaForegroundColor Attr = 35
AttrCyanForegroundColor Attr = 36
AttrWhiteForegroundColor Attr = 37
AttrExtendedForegroundColor Attr = 38
AttrDefaultForegroundColor Attr = 39
AttrBlackBackgroundColor Attr = 40
AttrRedBackgroundColor Attr = 41
AttrGreenBackgroundColor Attr = 42
AttrYellowBackgroundColor Attr = 43
AttrBlueBackgroundColor Attr = 44
AttrMagentaBackgroundColor Attr = 45
AttrCyanBackgroundColor Attr = 46
AttrWhiteBackgroundColor Attr = 47
AttrExtendedBackgroundColor Attr = 48
AttrDefaultBackgroundColor Attr = 49
AttrExtendedUnderlineColor Attr = 58
AttrDefaultUnderlineColor Attr = 59
AttrBrightBlackForegroundColor Attr = 90
AttrBrightRedForegroundColor Attr = 91
AttrBrightGreenForegroundColor Attr = 92
AttrBrightYellowForegroundColor Attr = 93
AttrBrightBlueForegroundColor Attr = 94
AttrBrightMagentaForegroundColor Attr = 95
AttrBrightCyanForegroundColor Attr = 96
AttrBrightWhiteForegroundColor Attr = 97
AttrBrightBlackBackgroundColor Attr = 100
AttrBrightRedBackgroundColor Attr = 101
AttrBrightGreenBackgroundColor Attr = 102
AttrBrightYellowBackgroundColor Attr = 103
AttrBrightBlueBackgroundColor Attr = 104
AttrBrightMagentaBackgroundColor Attr = 105
AttrBrightCyanBackgroundColor Attr = 106
AttrBrightWhiteBackgroundColor Attr = 107
AttrRGBColorIntroducer Attr = 2
AttrExtendedColorIntroducer Attr = 5
)
const (
ResetAttr = AttrReset
BoldAttr = AttrBold
FaintAttr = AttrFaint
ItalicAttr = AttrItalic
UnderlineAttr = AttrUnderline
SlowBlinkAttr = AttrBlink
RapidBlinkAttr = AttrRapidBlink
ReverseAttr = AttrReverse
ConcealAttr = AttrConceal
StrikethroughAttr = AttrStrikethrough
NormalIntensityAttr = AttrNormalIntensity
NoItalicAttr = AttrNoItalic
NoUnderlineAttr = AttrNoUnderline
NoBlinkAttr = AttrNoBlink
NoReverseAttr = AttrNoReverse
NoConcealAttr = AttrNoConceal
NoStrikethroughAttr = AttrNoStrikethrough
BlackForegroundColorAttr = AttrBlackForegroundColor
RedForegroundColorAttr = AttrRedForegroundColor
GreenForegroundColorAttr = AttrGreenForegroundColor
YellowForegroundColorAttr = AttrYellowForegroundColor
BlueForegroundColorAttr = AttrBlueForegroundColor
MagentaForegroundColorAttr = AttrMagentaForegroundColor
CyanForegroundColorAttr = AttrCyanForegroundColor
WhiteForegroundColorAttr = AttrWhiteForegroundColor
ExtendedForegroundColorAttr = AttrExtendedForegroundColor
DefaultForegroundColorAttr = AttrDefaultForegroundColor
BlackBackgroundColorAttr = AttrBlackBackgroundColor
RedBackgroundColorAttr = AttrRedBackgroundColor
GreenBackgroundColorAttr = AttrGreenBackgroundColor
YellowBackgroundColorAttr = AttrYellowBackgroundColor
BlueBackgroundColorAttr = AttrBlueBackgroundColor
MagentaBackgroundColorAttr = AttrMagentaBackgroundColor
CyanBackgroundColorAttr = AttrCyanBackgroundColor
WhiteBackgroundColorAttr = AttrWhiteBackgroundColor
ExtendedBackgroundColorAttr = AttrExtendedBackgroundColor
DefaultBackgroundColorAttr = AttrDefaultBackgroundColor
ExtendedUnderlineColorAttr = AttrExtendedUnderlineColor
DefaultUnderlineColorAttr = AttrDefaultUnderlineColor
BrightBlackForegroundColorAttr = AttrBrightBlackForegroundColor
BrightRedForegroundColorAttr = AttrBrightRedForegroundColor
BrightGreenForegroundColorAttr = AttrBrightGreenForegroundColor
BrightYellowForegroundColorAttr = AttrBrightYellowForegroundColor
BrightBlueForegroundColorAttr = AttrBrightBlueForegroundColor
BrightMagentaForegroundColorAttr = AttrBrightMagentaForegroundColor
BrightCyanForegroundColorAttr = AttrBrightCyanForegroundColor
BrightWhiteForegroundColorAttr = AttrBrightWhiteForegroundColor
BrightBlackBackgroundColorAttr = AttrBrightBlackBackgroundColor
BrightRedBackgroundColorAttr = AttrBrightRedBackgroundColor
BrightGreenBackgroundColorAttr = AttrBrightGreenBackgroundColor
BrightYellowBackgroundColorAttr = AttrBrightYellowBackgroundColor
BrightBlueBackgroundColorAttr = AttrBrightBlueBackgroundColor
BrightMagentaBackgroundColorAttr = AttrBrightMagentaBackgroundColor
BrightCyanBackgroundColorAttr = AttrBrightCyanBackgroundColor
BrightWhiteBackgroundColorAttr = AttrBrightWhiteBackgroundColor
RGBColorIntroducerAttr = AttrRGBColorIntroducer
ExtendedColorIntroducerAttr = AttrExtendedColorIntroducer
)
const (
attrReset = "0"
attrBold = "1"
attrFaint = "2"
attrItalic = "3"
attrUnderline = "4"
attrBlink = "5"
attrRapidBlink = "6"
attrReverse = "7"
attrConceal = "8"
attrStrikethrough = "9"
attrNormalIntensity = "22"
attrNoItalic = "23"
attrNoUnderline = "24"
attrNoBlink = "25"
attrNoReverse = "27"
attrNoConceal = "28"
attrNoStrikethrough = "29"
attrBlackForegroundColor = "30"
attrRedForegroundColor = "31"
attrGreenForegroundColor = "32"
attrYellowForegroundColor = "33"
attrBlueForegroundColor = "34"
attrMagentaForegroundColor = "35"
attrCyanForegroundColor = "36"
attrWhiteForegroundColor = "37"
attrExtendedForegroundColor = "38"
attrDefaultForegroundColor = "39"
attrBlackBackgroundColor = "40"
attrRedBackgroundColor = "41"
attrGreenBackgroundColor = "42"
attrYellowBackgroundColor = "43"
attrBlueBackgroundColor = "44"
attrMagentaBackgroundColor = "45"
attrCyanBackgroundColor = "46"
attrWhiteBackgroundColor = "47"
attrExtendedBackgroundColor = "48"
attrDefaultBackgroundColor = "49"
attrExtendedUnderlineColor = "58"
attrDefaultUnderlineColor = "59"
attrBrightBlackForegroundColor = "90"
attrBrightRedForegroundColor = "91"
attrBrightGreenForegroundColor = "92"
attrBrightYellowForegroundColor = "93"
attrBrightBlueForegroundColor = "94"
attrBrightMagentaForegroundColor = "95"
attrBrightCyanForegroundColor = "96"
attrBrightWhiteForegroundColor = "97"
attrBrightBlackBackgroundColor = "100"
attrBrightRedBackgroundColor = "101"
attrBrightGreenBackgroundColor = "102"
attrBrightYellowBackgroundColor = "103"
attrBrightBlueBackgroundColor = "104"
attrBrightMagentaBackgroundColor = "105"
attrBrightCyanBackgroundColor = "106"
attrBrightWhiteBackgroundColor = "107"
)
func ( Color) string {
switch c := .(type) {
case nil:
return attrDefaultForegroundColor
case BasicColor:
switch {
case Black:
return attrBlackForegroundColor
case Red:
return attrRedForegroundColor
case Green:
return attrGreenForegroundColor
case Yellow:
return attrYellowForegroundColor
case Blue:
return attrBlueForegroundColor
case Magenta:
return attrMagentaForegroundColor
case Cyan:
return attrCyanForegroundColor
case White:
return attrWhiteForegroundColor
case BrightBlack:
return attrBrightBlackForegroundColor
case BrightRed:
return attrBrightRedForegroundColor
case BrightGreen:
return attrBrightGreenForegroundColor
case BrightYellow:
return attrBrightYellowForegroundColor
case BrightBlue:
return attrBrightBlueForegroundColor
case BrightMagenta:
return attrBrightMagentaForegroundColor
case BrightCyan:
return attrBrightCyanForegroundColor
case BrightWhite:
return attrBrightWhiteForegroundColor
}
case ExtendedColor:
return "38;5;" + strconv.FormatUint(uint64(), 10)
case TrueColor, color.Color:
, , , := .RGBA()
return "38;2;" +
strconv.FormatUint(uint64(shift()), 10) + ";" +
strconv.FormatUint(uint64(shift()), 10) + ";" +
strconv.FormatUint(uint64(shift()), 10)
}
return attrDefaultForegroundColor
}
func ( Color) string {
switch c := .(type) {
case nil:
return attrDefaultBackgroundColor
case BasicColor:
switch {
case Black:
return attrBlackBackgroundColor
case Red:
return attrRedBackgroundColor
case Green:
return attrGreenBackgroundColor
case Yellow:
return attrYellowBackgroundColor
case Blue:
return attrBlueBackgroundColor
case Magenta:
return attrMagentaBackgroundColor
case Cyan:
return attrCyanBackgroundColor
case White:
return attrWhiteBackgroundColor
case BrightBlack:
return attrBrightBlackBackgroundColor
case BrightRed:
return attrBrightRedBackgroundColor
case BrightGreen:
return attrBrightGreenBackgroundColor
case BrightYellow:
return attrBrightYellowBackgroundColor
case BrightBlue:
return attrBrightBlueBackgroundColor
case BrightMagenta:
return attrBrightMagentaBackgroundColor
case BrightCyan:
return attrBrightCyanBackgroundColor
case BrightWhite:
return attrBrightWhiteBackgroundColor
}
case ExtendedColor:
return "48;5;" + strconv.FormatUint(uint64(), 10)
case TrueColor, color.Color:
, , , := .RGBA()
return "48;2;" +
strconv.FormatUint(uint64(shift()), 10) + ";" +
strconv.FormatUint(uint64(shift()), 10) + ";" +
strconv.FormatUint(uint64(shift()), 10)
}
return attrDefaultBackgroundColor
}
func ( Color) string {
switch c := .(type) {
case nil:
return attrDefaultUnderlineColor
case BasicColor:
return "58;5;" + strconv.FormatUint(uint64(), 10)
case ExtendedColor:
return "58;5;" + strconv.FormatUint(uint64(), 10)
case TrueColor, color.Color:
, , , := .RGBA()
return "58;2;" +
strconv.FormatUint(uint64(shift()), 10) + ";" +
strconv.FormatUint(uint64(shift()), 10) + ";" +
strconv.FormatUint(uint64(shift()), 10)
}
return attrDefaultUnderlineColor
}
func ( Params, *color.Color) int {
if len() < 2 {
return 0
}
:= [0]
:= [1]
:= .Param(0)
:= 2
:= func() (, , , int) {
switch {
case .HasMore() && .HasMore() && len() > 8 && [2].HasMore() && [3].HasMore() && [4].HasMore() && [5].HasMore() && [6].HasMore() && [7].HasMore():
+= 7
return [3].Param(0), [4].Param(0), [5].Param(0), [6].Param(0)
case .HasMore() && .HasMore() && len() > 7 && [2].HasMore() && [3].HasMore() && [4].HasMore() && [5].HasMore() && [6].HasMore():
+= 6
return [3].Param(0), [4].Param(0), [5].Param(0), [6].Param(0)
case .HasMore() && .HasMore() && len() > 6 && [2].HasMore() && [3].HasMore() && [4].HasMore() && [5].HasMore():
+= 5
return [3].Param(0), [4].Param(0), [5].Param(0), [6].Param(0)
case .HasMore() && .HasMore() && len() > 5 && [2].HasMore() && [3].HasMore() && [4].HasMore() && ![5].HasMore():
+= 4
return [3].Param(0), [4].Param(0), [5].Param(0), -1
case .HasMore() && .HasMore() && .Param(0) == 2 && [2].HasMore() && [3].HasMore() && ![4].HasMore():
fallthrough
case !.HasMore() && !.HasMore() && .Param(0) == 2 && ![2].HasMore() && ![3].HasMore() && ![4].HasMore():
+= 3
return [2].Param(0), [3].Param(0), [4].Param(0), -1
}
return -1, -1, -1, -1
}
switch {
case 0:
return 2
case 1:
* = color.Transparent
return 2
case 2:
if len() < 5 {
return 0
}
, , , := ()
if == -1 || == -1 || == -1 {
return 0
}
* = color.RGBA{
R: uint8(),
G: uint8(),
B: uint8(),
A: 0xff,
}
return
case 3:
if len() < 5 {
return 0
}
, , , := ()
if == -1 || == -1 || == -1 {
return 0
}
* = color.CMYK{
C: uint8(),
M: uint8(),
Y: uint8(),
K: 0,
}
return
case 4:
if len() < 6 {
return 0
}
, , , := ()
if == -1 || == -1 || == -1 || == -1 {
return 0
}
* = color.CMYK{
C: uint8(),
M: uint8(),
Y: uint8(),
K: uint8(),
}
return
case 5:
if len() < 3 {
return 0
}
switch {
case .HasMore() && .HasMore() && ![2].HasMore():
case !.HasMore() && !.HasMore() && ![2].HasMore():
default:
return 0
}
* = ExtendedColor([2].Param(0))
return 3
case 6:
if len() < 6 {
return 0
}
, , , := ()
if == -1 || == -1 || == -1 || == -1 {
return 0
}
* = color.RGBA{
R: uint8(),
G: uint8(),
B: uint8(),
A: uint8(),
}
return
default:
return 0
}
}