ctrl.go

  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// Common attributes include:
 42//   - 1	132 columns
 43//   - 2	Printer port
 44//   - 4	Sixel
 45//   - 6	Selective erase
 46//   - 7	Soft character set (DRCS)
 47//   - 8	User-defined keys (UDKs)
 48//   - 9	National replacement character sets (NRCS) (International terminal only)
 49//   - 12	Yugoslavian (SCS)
 50//   - 15	Technical character set
 51//   - 18	Windowing capability
 52//   - 21	Horizontal scrolling
 53//   - 23	Greek
 54//   - 24	Turkish
 55//   - 42	ISO Latin-2 character set
 56//   - 44	PCTerm
 57//   - 45	Soft key map
 58//   - 46	ASCII emulation
 59//
 60// See https://vt100.net/docs/vt510-rm/DA1.html
 61func PrimaryDeviceAttributes(attrs ...int) string {
 62	if len(attrs) == 0 {
 63		return RequestPrimaryDeviceAttributes
 64	} else if len(attrs) == 1 && attrs[0] == 0 {
 65		return "\x1b[0c"
 66	}
 67
 68	as := make([]string, len(attrs))
 69	for i, a := range attrs {
 70		as[i] = strconv.Itoa(a)
 71	}
 72	return "\x1b[?" + strings.Join(as, ";") + "c"
 73}
 74
 75// DA1 is an alias for [PrimaryDeviceAttributes].
 76func DA1(attrs ...int) string {
 77	return PrimaryDeviceAttributes(attrs...)
 78}
 79
 80// RequestPrimaryDeviceAttributes is a control sequence that requests the
 81// terminal's primary device attributes (DA1).
 82//
 83//	CSI c
 84//
 85// See https://vt100.net/docs/vt510-rm/DA1.html
 86const RequestPrimaryDeviceAttributes = "\x1b[c"
 87
 88// SecondaryDeviceAttributes (DA2) is a control sequence that reports the
 89// terminal's secondary device attributes.
 90//
 91//	CSI > c
 92//	CSI > 0 c
 93//	CSI > Ps ; ... c
 94//
 95// See https://vt100.net/docs/vt510-rm/DA2.html
 96func SecondaryDeviceAttributes(attrs ...int) string {
 97	if len(attrs) == 0 {
 98		return RequestSecondaryDeviceAttributes
 99	}
100
101	as := make([]string, len(attrs))
102	for i, a := range attrs {
103		as[i] = strconv.Itoa(a)
104	}
105	return "\x1b[>" + strings.Join(as, ";") + "c"
106}
107
108// DA2 is an alias for [SecondaryDeviceAttributes].
109func DA2(attrs ...int) string {
110	return SecondaryDeviceAttributes(attrs...)
111}
112
113// RequestSecondaryDeviceAttributes is a control sequence that requests the
114// terminal's secondary device attributes (DA2).
115//
116//	CSI > c
117//
118// See https://vt100.net/docs/vt510-rm/DA2.html
119const RequestSecondaryDeviceAttributes = "\x1b[>c"
120
121// TertiaryDeviceAttributes (DA3) is a control sequence that reports the
122// terminal's tertiary device attributes.
123//
124//	CSI = c
125//	CSI = 0 c
126//	DCS ! | Text ST
127//
128// Where Text is the unit ID for the terminal.
129//
130// If no unit ID is given, or if the unit ID is 0, this function returns the
131// request sequence. Otherwise, it returns the response sequence.
132//
133// See https://vt100.net/docs/vt510-rm/DA3.html
134func TertiaryDeviceAttributes(unitID string) string {
135	switch unitID {
136	case "":
137		return RequestTertiaryDeviceAttributes
138	case "0":
139		return "\x1b[=0c"
140	}
141
142	return "\x1bP!|" + unitID + "\x1b\\"
143}
144
145// DA3 is an alias for [TertiaryDeviceAttributes].
146func DA3(unitID string) string {
147	return TertiaryDeviceAttributes(unitID)
148}
149
150// RequestTertiaryDeviceAttributes is a control sequence that requests the
151// terminal's tertiary device attributes (DA3).
152//
153//	CSI = c
154//
155// See https://vt100.net/docs/vt510-rm/DA3.html
156const RequestTertiaryDeviceAttributes = "\x1b[=c"