status.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package ansi
  2. import (
  3. "strconv"
  4. "strings"
  5. )
  6. // Status represents a terminal status report.
  7. type Status interface {
  8. // Status returns the status report identifier.
  9. Status() int
  10. }
  11. // ANSIStatus represents an ANSI terminal status report.
  12. type ANSIStatus int //nolint:revive
  13. // Status returns the status report identifier.
  14. func (s ANSIStatus) Status() int {
  15. return int(s)
  16. }
  17. // DECStatus represents a DEC terminal status report.
  18. type DECStatus int
  19. // Status returns the status report identifier.
  20. func (s DECStatus) Status() 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 ...Status) 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.Status())
  40. switch status.(type) {
  41. case DECStatus:
  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 Status) string {
  52. return DeviceStatusReport(status)
  53. }
  54. // CursorPositionReport (CPR) is a control sequence that reports the cursor's
  55. // position.
  56. //
  57. // CSI Pl ; Pc R
  58. //
  59. // Where Pl is the line number and Pc is the column number.
  60. //
  61. // See also https://vt100.net/docs/vt510-rm/CPR.html
  62. func CursorPositionReport(line, column int) string {
  63. if line < 1 {
  64. line = 1
  65. }
  66. if column < 1 {
  67. column = 1
  68. }
  69. return "\x1b[" + strconv.Itoa(line) + ";" + strconv.Itoa(column) + "R"
  70. }
  71. // CPR is an alias for [CursorPositionReport].
  72. func CPR(line, column int) string {
  73. return CursorPositionReport(line, column)
  74. }
  75. // ExtendedCursorPositionReport (DECXCPR) is a control sequence that reports the
  76. // cursor's position along with the page number (optional).
  77. //
  78. // CSI ? Pl ; Pc R
  79. // CSI ? Pl ; Pc ; Pv R
  80. //
  81. // Where Pl is the line number, Pc is the column number, and Pv is the page
  82. // number.
  83. //
  84. // If the page number is zero or negative, the returned sequence won't include
  85. // the page number.
  86. //
  87. // See also https://vt100.net/docs/vt510-rm/DECXCPR.html
  88. func ExtendedCursorPositionReport(line, column, page int) string {
  89. if line < 1 {
  90. line = 1
  91. }
  92. if column < 1 {
  93. column = 1
  94. }
  95. if page < 1 {
  96. return "\x1b[?" + strconv.Itoa(line) + ";" + strconv.Itoa(column) + "R"
  97. }
  98. return "\x1b[?" + strconv.Itoa(line) + ";" + strconv.Itoa(column) + ";" + strconv.Itoa(page) + "R"
  99. }
  100. // DECXCPR is an alias for [ExtendedCursorPositionReport].
  101. func DECXCPR(line, column, page int) string {
  102. return ExtendedCursorPositionReport(line, column, page)
  103. }