status.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package ansi
  2. import (
  3. "strconv"
  4. "strings"
  5. )
  6. // StatusReport represents a terminal status report.
  7. type StatusReport interface {
  8. // StatusReport returns the status report identifier.
  9. StatusReport() int
  10. }
  11. // ANSIReport represents an ANSI terminal status report.
  12. type ANSIStatusReport int //nolint:revive
  13. // Report returns the status report identifier.
  14. func (s ANSIStatusReport) StatusReport() int {
  15. return int(s)
  16. }
  17. // DECStatusReport represents a DEC terminal status report.
  18. type DECStatusReport int
  19. // Status returns the status report identifier.
  20. func (s DECStatusReport) StatusReport() int {
  21. return int(s)
  22. }
  23. // DeviceStatusReport (DSR) is a control sequence that reports the terminal's
  24. // status.
  25. // The terminal responds with a DSR sequence.
  26. //
  27. // CSI Ps n
  28. // CSI ? Ps n
  29. //
  30. // If one of the statuses is a [DECStatus], the sequence will use the DEC
  31. // format.
  32. //
  33. // See also https://vt100.net/docs/vt510-rm/DSR.html
  34. func DeviceStatusReport(statues ...StatusReport) string {
  35. var dec bool
  36. list := make([]string, len(statues))
  37. seq := "\x1b["
  38. for i, status := range statues {
  39. list[i] = strconv.Itoa(status.StatusReport())
  40. switch status.(type) {
  41. case DECStatusReport:
  42. dec = true
  43. }
  44. }
  45. if dec {
  46. seq += "?"
  47. }
  48. return seq + strings.Join(list, ";") + "n"
  49. }
  50. // DSR is an alias for [DeviceStatusReport].
  51. func DSR(status StatusReport) string {
  52. return DeviceStatusReport(status)
  53. }
  54. // RequestCursorPositionReport is an escape sequence that requests the current
  55. // cursor position.
  56. //
  57. // CSI 6 n
  58. //
  59. // The terminal will report the cursor position as a CSI sequence in the
  60. // following format:
  61. //
  62. // CSI Pl ; Pc R
  63. //
  64. // Where Pl is the line number and Pc is the column number.
  65. // See: https://vt100.net/docs/vt510-rm/CPR.html
  66. const RequestCursorPositionReport = "\x1b[6n"
  67. // RequestExtendedCursorPositionReport (DECXCPR) is a sequence for requesting
  68. // the cursor position report including the current page number.
  69. //
  70. // CSI ? 6 n
  71. //
  72. // The terminal will report the cursor position as a CSI sequence in the
  73. // following format:
  74. //
  75. // CSI ? Pl ; Pc ; Pp R
  76. //
  77. // Where Pl is the line number, Pc is the column number, and Pp is the page
  78. // number.
  79. // See: https://vt100.net/docs/vt510-rm/DECXCPR.html
  80. const RequestExtendedCursorPositionReport = "\x1b[?6n"
  81. // CursorPositionReport (CPR) is a control sequence that reports the cursor's
  82. // position.
  83. //
  84. // CSI Pl ; Pc R
  85. //
  86. // Where Pl is the line number and Pc is the column number.
  87. //
  88. // See also https://vt100.net/docs/vt510-rm/CPR.html
  89. func CursorPositionReport(line, column int) string {
  90. if line < 1 {
  91. line = 1
  92. }
  93. if column < 1 {
  94. column = 1
  95. }
  96. return "\x1b[" + strconv.Itoa(line) + ";" + strconv.Itoa(column) + "R"
  97. }
  98. // CPR is an alias for [CursorPositionReport].
  99. func CPR(line, column int) string {
  100. return CursorPositionReport(line, column)
  101. }
  102. // ExtendedCursorPositionReport (DECXCPR) is a control sequence that reports the
  103. // cursor's position along with the page number (optional).
  104. //
  105. // CSI ? Pl ; Pc R
  106. // CSI ? Pl ; Pc ; Pv R
  107. //
  108. // Where Pl is the line number, Pc is the column number, and Pv is the page
  109. // number.
  110. //
  111. // If the page number is zero or negative, the returned sequence won't include
  112. // the page number.
  113. //
  114. // See also https://vt100.net/docs/vt510-rm/DECXCPR.html
  115. func ExtendedCursorPositionReport(line, column, page int) string {
  116. if line < 1 {
  117. line = 1
  118. }
  119. if column < 1 {
  120. column = 1
  121. }
  122. if page < 1 {
  123. return "\x1b[?" + strconv.Itoa(line) + ";" + strconv.Itoa(column) + "R"
  124. }
  125. return "\x1b[?" + strconv.Itoa(line) + ";" + strconv.Itoa(column) + ";" + strconv.Itoa(page) + "R"
  126. }
  127. // DECXCPR is an alias for [ExtendedCursorPositionReport].
  128. func DECXCPR(line, column, page int) string {
  129. return ExtendedCursorPositionReport(line, column, page)
  130. }