ctrl.go 3.6 KB

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