main
Raw Download raw file
 1package ansi
 2
 3import "strconv"
 4
 5// Select Graphic Rendition (SGR) is a command that sets display attributes.
 6//
 7// Default is 0.
 8//
 9//	CSI Ps ; Ps ... m
10//
11// See: https://vt100.net/docs/vt510-rm/SGR.html
12func SelectGraphicRendition(ps ...Attr) string {
13	if len(ps) == 0 {
14		return ResetStyle
15	}
16
17	var s Style
18	for _, p := range ps {
19		attr, ok := attrStrings[p]
20		if ok {
21			s = append(s, attr)
22		} else {
23			if p < 0 {
24				p = 0
25			}
26			s = append(s, strconv.Itoa(p))
27		}
28	}
29
30	return s.String()
31}
32
33// SGR is an alias for [SelectGraphicRendition].
34func SGR(ps ...Attr) string {
35	return SelectGraphicRendition(ps...)
36}
37
38var attrStrings = map[int]string{
39	ResetAttr:                        "0",
40	BoldAttr:                         "1",
41	FaintAttr:                        "2",
42	ItalicAttr:                       "3",
43	UnderlineAttr:                    "4",
44	SlowBlinkAttr:                    "5",
45	RapidBlinkAttr:                   "6",
46	ReverseAttr:                      "7",
47	ConcealAttr:                      "8",
48	StrikethroughAttr:                "9",
49	NoBoldAttr:                       "21",
50	NormalIntensityAttr:              "22",
51	NoItalicAttr:                     "23",
52	NoUnderlineAttr:                  "24",
53	NoBlinkAttr:                      "25",
54	NoReverseAttr:                    "27",
55	NoConcealAttr:                    "28",
56	NoStrikethroughAttr:              "29",
57	BlackForegroundColorAttr:         "30",
58	RedForegroundColorAttr:           "31",
59	GreenForegroundColorAttr:         "32",
60	YellowForegroundColorAttr:        "33",
61	BlueForegroundColorAttr:          "34",
62	MagentaForegroundColorAttr:       "35",
63	CyanForegroundColorAttr:          "36",
64	WhiteForegroundColorAttr:         "37",
65	ExtendedForegroundColorAttr:      "38",
66	DefaultForegroundColorAttr:       "39",
67	BlackBackgroundColorAttr:         "40",
68	RedBackgroundColorAttr:           "41",
69	GreenBackgroundColorAttr:         "42",
70	YellowBackgroundColorAttr:        "43",
71	BlueBackgroundColorAttr:          "44",
72	MagentaBackgroundColorAttr:       "45",
73	CyanBackgroundColorAttr:          "46",
74	WhiteBackgroundColorAttr:         "47",
75	ExtendedBackgroundColorAttr:      "48",
76	DefaultBackgroundColorAttr:       "49",
77	ExtendedUnderlineColorAttr:       "58",
78	DefaultUnderlineColorAttr:        "59",
79	BrightBlackForegroundColorAttr:   "90",
80	BrightRedForegroundColorAttr:     "91",
81	BrightGreenForegroundColorAttr:   "92",
82	BrightYellowForegroundColorAttr:  "93",
83	BrightBlueForegroundColorAttr:    "94",
84	BrightMagentaForegroundColorAttr: "95",
85	BrightCyanForegroundColorAttr:    "96",
86	BrightWhiteForegroundColorAttr:   "97",
87	BrightBlackBackgroundColorAttr:   "100",
88	BrightRedBackgroundColorAttr:     "101",
89	BrightGreenBackgroundColorAttr:   "102",
90	BrightYellowBackgroundColorAttr:  "103",
91	BrightBlueBackgroundColorAttr:    "104",
92	BrightMagentaBackgroundColorAttr: "105",
93	BrightCyanBackgroundColorAttr:    "106",
94	BrightWhiteBackgroundColorAttr:   "107",
95}