main
Raw Download raw file
  1package ansi
  2
  3import (
  4	"strconv"
  5	"strings"
  6)
  7
  8// RequestNameVersion (XTVERSION) is a control sequence that requests the
  9// terminal's name and version. It responds with a DSR sequence identifying the
 10// terminal.
 11//
 12//	CSI > 0 q
 13//	DCS > | text ST
 14//
 15// See https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-PC-Style-Function-Keys
 16const (
 17	RequestNameVersion = "\x1b[>q"
 18	XTVERSION          = RequestNameVersion
 19)
 20
 21// RequestXTVersion is a control sequence that requests the terminal's XTVERSION. It responds with a DSR sequence identifying the version.
 22//
 23//	CSI > Ps q
 24//	DCS > | text ST
 25//
 26// See https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-PC-Style-Function-Keys
 27//
 28// Deprecated: use [RequestNameVersion] instead.
 29const RequestXTVersion = RequestNameVersion
 30
 31// PrimaryDeviceAttributes (DA1) is a control sequence that reports the
 32// terminal's primary device attributes.
 33//
 34//	CSI c
 35//	CSI 0 c
 36//	CSI ? Ps ; ... c
 37//
 38// If no attributes are given, or if the attribute is 0, this function returns
 39// the request sequence. Otherwise, it returns the response sequence.
 40//
 41// See https://vt100.net/docs/vt510-rm/DA1.html
 42func PrimaryDeviceAttributes(attrs ...int) string {
 43	if len(attrs) == 0 {
 44		return RequestPrimaryDeviceAttributes
 45	} else if len(attrs) == 1 && attrs[0] == 0 {
 46		return "\x1b[0c"
 47	}
 48
 49	as := make([]string, len(attrs))
 50	for i, a := range attrs {
 51		as[i] = strconv.Itoa(a)
 52	}
 53	return "\x1b[?" + strings.Join(as, ";") + "c"
 54}
 55
 56// DA1 is an alias for [PrimaryDeviceAttributes].
 57func DA1(attrs ...int) string {
 58	return PrimaryDeviceAttributes(attrs...)
 59}
 60
 61// RequestPrimaryDeviceAttributes is a control sequence that requests the
 62// terminal's primary device attributes (DA1).
 63//
 64//	CSI c
 65//
 66// See https://vt100.net/docs/vt510-rm/DA1.html
 67const RequestPrimaryDeviceAttributes = "\x1b[c"
 68
 69// SecondaryDeviceAttributes (DA2) is a control sequence that reports the
 70// terminal's secondary device attributes.
 71//
 72//	CSI > c
 73//	CSI > 0 c
 74//	CSI > Ps ; ... c
 75//
 76// See https://vt100.net/docs/vt510-rm/DA2.html
 77func SecondaryDeviceAttributes(attrs ...int) string {
 78	if len(attrs) == 0 {
 79		return RequestSecondaryDeviceAttributes
 80	}
 81
 82	as := make([]string, len(attrs))
 83	for i, a := range attrs {
 84		as[i] = strconv.Itoa(a)
 85	}
 86	return "\x1b[>" + strings.Join(as, ";") + "c"
 87}
 88
 89// DA2 is an alias for [SecondaryDeviceAttributes].
 90func DA2(attrs ...int) string {
 91	return SecondaryDeviceAttributes(attrs...)
 92}
 93
 94// RequestSecondaryDeviceAttributes is a control sequence that requests the
 95// terminal's secondary device attributes (DA2).
 96//
 97//	CSI > c
 98//
 99// See https://vt100.net/docs/vt510-rm/DA2.html
100const RequestSecondaryDeviceAttributes = "\x1b[>c"
101
102// TertiaryDeviceAttributes (DA3) is a control sequence that reports the
103// terminal's tertiary device attributes.
104//
105//	CSI = c
106//	CSI = 0 c
107//	DCS ! | Text ST
108//
109// Where Text is the unit ID for the terminal.
110//
111// If no unit ID is given, or if the unit ID is 0, this function returns the
112// request sequence. Otherwise, it returns the response sequence.
113//
114// See https://vt100.net/docs/vt510-rm/DA3.html
115func TertiaryDeviceAttributes(unitID string) string {
116	switch unitID {
117	case "":
118		return RequestTertiaryDeviceAttributes
119	case "0":
120		return "\x1b[=0c"
121	}
122
123	return "\x1bP!|" + unitID + "\x1b\\"
124}
125
126// DA3 is an alias for [TertiaryDeviceAttributes].
127func DA3(unitID string) string {
128	return TertiaryDeviceAttributes(unitID)
129}
130
131// RequestTertiaryDeviceAttributes is a control sequence that requests the
132// terminal's tertiary device attributes (DA3).
133//
134//	CSI = c
135//
136// See https://vt100.net/docs/vt510-rm/DA3.html
137const RequestTertiaryDeviceAttributes = "\x1b[=c"