ctrl.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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[>0q"
  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. // Deprecated: use [RequestNameVersion] instead.
  25. const RequestXTVersion = RequestNameVersion
  26. // PrimaryDeviceAttributes (DA1) is a control sequence that reports the
  27. // terminal's primary device attributes.
  28. //
  29. // CSI c
  30. // CSI 0 c
  31. // CSI ? Ps ; ... c
  32. //
  33. // If no attributes are given, or if the attribute is 0, this function returns
  34. // the request sequence. Otherwise, it returns the response sequence.
  35. //
  36. // See https://vt100.net/docs/vt510-rm/DA1.html
  37. func PrimaryDeviceAttributes(attrs ...int) string {
  38. if len(attrs) == 0 {
  39. return "\x1b[c"
  40. } else if len(attrs) == 1 && attrs[0] == 0 {
  41. return "\x1b[0c"
  42. }
  43. as := make([]string, len(attrs))
  44. for i, a := range attrs {
  45. as[i] = strconv.Itoa(a)
  46. }
  47. return "\x1b[?" + strings.Join(as, ";") + "c"
  48. }
  49. // DA1 is an alias for [PrimaryDeviceAttributes].
  50. func DA1(attrs ...int) string {
  51. return PrimaryDeviceAttributes(attrs...)
  52. }
  53. // RequestPrimaryDeviceAttributes is a control sequence that requests the
  54. // terminal's primary device attributes (DA1).
  55. //
  56. // CSI c
  57. //
  58. // See https://vt100.net/docs/vt510-rm/DA1.html
  59. const RequestPrimaryDeviceAttributes = "\x1b[c"
  60. // SecondaryDeviceAttributes (DA2) is a control sequence that reports the
  61. // terminal's secondary device attributes.
  62. //
  63. // CSI > c
  64. // CSI > 0 c
  65. // CSI > Ps ; ... c
  66. //
  67. // See https://vt100.net/docs/vt510-rm/DA2.html
  68. func SecondaryDeviceAttributes(attrs ...int) string {
  69. if len(attrs) == 0 {
  70. return "\x1b[>c"
  71. }
  72. as := make([]string, len(attrs))
  73. for i, a := range attrs {
  74. as[i] = strconv.Itoa(a)
  75. }
  76. return "\x1b[>" + strings.Join(as, ";") + "c"
  77. }
  78. // DA2 is an alias for [SecondaryDeviceAttributes].
  79. func DA2(attrs ...int) string {
  80. return SecondaryDeviceAttributes(attrs...)
  81. }
  82. // TertiaryDeviceAttributes (DA3) is a control sequence that reports the
  83. // terminal's tertiary device attributes.
  84. //
  85. // CSI = c
  86. // CSI = 0 c
  87. // DCS ! | Text ST
  88. //
  89. // Where Text is the unit ID for the terminal.
  90. //
  91. // If no unit ID is given, or if the unit ID is 0, this function returns the
  92. // request sequence. Otherwise, it returns the response sequence.
  93. //
  94. // See https://vt100.net/docs/vt510-rm/DA3.html
  95. func TertiaryDeviceAttributes(unitID string) string {
  96. switch unitID {
  97. case "":
  98. return "\x1b[=c"
  99. case "0":
  100. return "\x1b[=0c"
  101. }
  102. return "\x1bP!|" + unitID + "\x1b\\"
  103. }
  104. // DA3 is an alias for [TertiaryDeviceAttributes].
  105. func DA3(unitID string) string {
  106. return TertiaryDeviceAttributes(unitID)
  107. }