cursor.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. package ansi
  2. import "strconv"
  3. // SaveCursor (DECSC) is an escape sequence that saves the current cursor
  4. // position.
  5. //
  6. // ESC 7
  7. //
  8. // See: https://vt100.net/docs/vt510-rm/DECSC.html
  9. const SaveCursor = "\x1b7"
  10. // RestoreCursor (DECRC) is an escape sequence that restores the cursor
  11. // position.
  12. //
  13. // ESC 8
  14. //
  15. // See: https://vt100.net/docs/vt510-rm/DECRC.html
  16. const RestoreCursor = "\x1b8"
  17. // RequestCursorPosition (CPR) is an escape sequence that requests the current
  18. // cursor position.
  19. //
  20. // CSI 6 n
  21. //
  22. // The terminal will report the cursor position as a CSI sequence in the
  23. // following format:
  24. //
  25. // CSI Pl ; Pc R
  26. //
  27. // Where Pl is the line number and Pc is the column number.
  28. // See: https://vt100.net/docs/vt510-rm/CPR.html
  29. const RequestCursorPosition = "\x1b[6n"
  30. // RequestExtendedCursorPosition (DECXCPR) is a sequence for requesting the
  31. // cursor position report including the current page number.
  32. //
  33. // CSI ? 6 n
  34. //
  35. // The terminal will report the cursor position as a CSI sequence in the
  36. // following format:
  37. //
  38. // CSI ? Pl ; Pc ; Pp R
  39. //
  40. // Where Pl is the line number, Pc is the column number, and Pp is the page
  41. // number.
  42. // See: https://vt100.net/docs/vt510-rm/DECXCPR.html
  43. const RequestExtendedCursorPosition = "\x1b[?6n"
  44. // CursorUp (CUU) returns a sequence for moving the cursor up n cells.
  45. //
  46. // CSI n A
  47. //
  48. // See: https://vt100.net/docs/vt510-rm/CUU.html
  49. func CursorUp(n int) string {
  50. var s string
  51. if n > 1 {
  52. s = strconv.Itoa(n)
  53. }
  54. return "\x1b[" + s + "A"
  55. }
  56. // CursorUp1 is a sequence for moving the cursor up one cell.
  57. //
  58. // This is equivalent to CursorUp(1).
  59. const CursorUp1 = "\x1b[A"
  60. // CursorDown (CUD) returns a sequence for moving the cursor down n cells.
  61. //
  62. // CSI n B
  63. //
  64. // See: https://vt100.net/docs/vt510-rm/CUD.html
  65. func CursorDown(n int) string {
  66. var s string
  67. if n > 1 {
  68. s = strconv.Itoa(n)
  69. }
  70. return "\x1b[" + s + "B"
  71. }
  72. // CursorDown1 is a sequence for moving the cursor down one cell.
  73. //
  74. // This is equivalent to CursorDown(1).
  75. const CursorDown1 = "\x1b[B"
  76. // CursorRight (CUF) returns a sequence for moving the cursor right n cells.
  77. //
  78. // CSI n C
  79. //
  80. // See: https://vt100.net/docs/vt510-rm/CUF.html
  81. func CursorRight(n int) string {
  82. var s string
  83. if n > 1 {
  84. s = strconv.Itoa(n)
  85. }
  86. return "\x1b[" + s + "C"
  87. }
  88. // CursorRight1 is a sequence for moving the cursor right one cell.
  89. //
  90. // This is equivalent to CursorRight(1).
  91. const CursorRight1 = "\x1b[C"
  92. // CursorLeft (CUB) returns a sequence for moving the cursor left n cells.
  93. //
  94. // CSI n D
  95. //
  96. // See: https://vt100.net/docs/vt510-rm/CUB.html
  97. func CursorLeft(n int) string {
  98. var s string
  99. if n > 1 {
  100. s = strconv.Itoa(n)
  101. }
  102. return "\x1b[" + s + "D"
  103. }
  104. // CursorLeft1 is a sequence for moving the cursor left one cell.
  105. //
  106. // This is equivalent to CursorLeft(1).
  107. const CursorLeft1 = "\x1b[D"
  108. // CursorNextLine (CNL) returns a sequence for moving the cursor to the
  109. // beginning of the next line n times.
  110. //
  111. // CSI n E
  112. //
  113. // See: https://vt100.net/docs/vt510-rm/CNL.html
  114. func CursorNextLine(n int) string {
  115. var s string
  116. if n > 1 {
  117. s = strconv.Itoa(n)
  118. }
  119. return "\x1b[" + s + "E"
  120. }
  121. // CursorPreviousLine (CPL) returns a sequence for moving the cursor to the
  122. // beginning of the previous line n times.
  123. //
  124. // CSI n F
  125. //
  126. // See: https://vt100.net/docs/vt510-rm/CPL.html
  127. func CursorPreviousLine(n int) string {
  128. var s string
  129. if n > 1 {
  130. s = strconv.Itoa(n)
  131. }
  132. return "\x1b[" + s + "F"
  133. }
  134. // MoveCursor (CUP) returns a sequence for moving the cursor to the given row
  135. // and column.
  136. //
  137. // CSI n ; m H
  138. //
  139. // See: https://vt100.net/docs/vt510-rm/CUP.html
  140. func MoveCursor(row, col int) string {
  141. if row < 0 {
  142. row = 0
  143. }
  144. if col < 0 {
  145. col = 0
  146. }
  147. return "\x1b[" + strconv.Itoa(row) + ";" + strconv.Itoa(col) + "H"
  148. }
  149. // MoveCursorOrigin is a sequence for moving the cursor to the upper left
  150. // corner of the screen. This is equivalent to MoveCursor(1, 1).
  151. const MoveCursorOrigin = "\x1b[1;1H"
  152. // SaveCursorPosition (SCP or SCOSC) is a sequence for saving the cursor
  153. // position.
  154. //
  155. // CSI s
  156. //
  157. // This acts like Save, except the page number where the cursor is located is
  158. // not saved.
  159. //
  160. // See: https://vt100.net/docs/vt510-rm/SCOSC.html
  161. const SaveCursorPosition = "\x1b[s"
  162. // RestoreCursorPosition (RCP or SCORC) is a sequence for restoring the cursor
  163. // position.
  164. //
  165. // CSI u
  166. //
  167. // This acts like Restore, except the cursor stays on the same page where the
  168. // cursor was saved.
  169. //
  170. // See: https://vt100.net/docs/vt510-rm/SCORC.html
  171. const RestoreCursorPosition = "\x1b[u"