screen.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package ansi
  2. import "strconv"
  3. // EraseDisplay (ED) clears the screen or parts of the screen. Possible values:
  4. //
  5. // 0: Clear from cursor to end of screen.
  6. // 1: Clear from cursor to beginning of the screen.
  7. // 2: Clear entire screen (and moves cursor to upper left on DOS).
  8. // 3: Clear entire screen and delete all lines saved in the scrollback buffer.
  9. //
  10. // CSI <n> J
  11. //
  12. // See: https://vt100.net/docs/vt510-rm/ED.html
  13. func EraseDisplay(n int) string {
  14. if n < 0 {
  15. n = 0
  16. }
  17. return "\x1b[" + strconv.Itoa(n) + "J"
  18. }
  19. // EraseDisplay constants.
  20. // These are the possible values for the EraseDisplay function.
  21. const (
  22. EraseDisplayRight = "\x1b[0J"
  23. EraseDisplayLeft = "\x1b[1J"
  24. EraseEntireDisplay = "\x1b[2J"
  25. )
  26. // EraseLine (EL) clears the current line or parts of the line. Possible values:
  27. //
  28. // 0: Clear from cursor to end of line.
  29. // 1: Clear from cursor to beginning of the line.
  30. // 2: Clear entire line.
  31. //
  32. // The cursor position is not affected.
  33. //
  34. // CSI <n> K
  35. //
  36. // See: https://vt100.net/docs/vt510-rm/EL.html
  37. func EraseLine(n int) string {
  38. if n < 0 {
  39. n = 0
  40. }
  41. return "\x1b[" + strconv.Itoa(n) + "K"
  42. }
  43. // EraseLine constants.
  44. // These are the possible values for the EraseLine function.
  45. const (
  46. EraseLineRight = "\x1b[0K"
  47. EraseLineLeft = "\x1b[1K"
  48. EraseEntireLine = "\x1b[2K"
  49. )
  50. // ScrollUp (SU) scrolls the screen up n lines. New lines are added at the
  51. // bottom of the screen.
  52. //
  53. // CSI <n> S
  54. //
  55. // See: https://vt100.net/docs/vt510-rm/SU.html
  56. func ScrollUp(n int) string {
  57. var s string
  58. if n > 1 {
  59. s = strconv.Itoa(n)
  60. }
  61. return "\x1b[" + s + "S"
  62. }
  63. // ScrollDown (SD) scrolls the screen down n lines. New lines are added at the
  64. // top of the screen.
  65. //
  66. // CSI <n> T
  67. //
  68. // See: https://vt100.net/docs/vt510-rm/SD.html
  69. func ScrollDown(n int) string {
  70. var s string
  71. if n > 1 {
  72. s = strconv.Itoa(n)
  73. }
  74. return "\x1b[" + s + "T"
  75. }
  76. // InsertLine (IL) inserts n blank lines at the current cursor position.
  77. // Existing lines are moved down.
  78. //
  79. // CSI <n> L
  80. //
  81. // See: https://vt100.net/docs/vt510-rm/IL.html
  82. func InsertLine(n int) string {
  83. var s string
  84. if n > 1 {
  85. s = strconv.Itoa(n)
  86. }
  87. return "\x1b[" + s + "L"
  88. }
  89. // DeleteLine (DL) deletes n lines at the current cursor position. Existing
  90. // lines are moved up.
  91. //
  92. // CSI <n> M
  93. //
  94. // See: https://vt100.net/docs/vt510-rm/DL.html
  95. func DeleteLine(n int) string {
  96. var s string
  97. if n > 1 {
  98. s = strconv.Itoa(n)
  99. }
  100. return "\x1b[" + s + "M"
  101. }
  102. // SetScrollingRegion (DECSTBM) sets the top and bottom margins for the scrolling
  103. // region. The default is the entire screen.
  104. //
  105. // CSI <top> ; <bottom> r
  106. //
  107. // See: https://vt100.net/docs/vt510-rm/DECSTBM.html
  108. func SetScrollingRegion(t, b int) string {
  109. if t < 0 {
  110. t = 0
  111. }
  112. if b < 0 {
  113. b = 0
  114. }
  115. return "\x1b[" + strconv.Itoa(t) + ";" + strconv.Itoa(b) + "r"
  116. }