aec.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package aec
  2. import "fmt"
  3. // EraseMode is listed in a variable EraseModes.
  4. type EraseMode uint
  5. var (
  6. // EraseModes is a list of EraseMode.
  7. EraseModes struct {
  8. // All erase all.
  9. All EraseMode
  10. // Head erase to head.
  11. Head EraseMode
  12. // Tail erase to tail.
  13. Tail EraseMode
  14. }
  15. // Save saves the cursor position.
  16. Save ANSI
  17. // Restore restores the cursor position.
  18. Restore ANSI
  19. // Hide hides the cursor.
  20. Hide ANSI
  21. // Show shows the cursor.
  22. Show ANSI
  23. // Report reports the cursor position.
  24. Report ANSI
  25. )
  26. // Up moves up the cursor.
  27. func Up(n uint) ANSI {
  28. if n == 0 {
  29. return empty
  30. }
  31. return newAnsi(fmt.Sprintf(esc+"%dA", n))
  32. }
  33. // Down moves down the cursor.
  34. func Down(n uint) ANSI {
  35. if n == 0 {
  36. return empty
  37. }
  38. return newAnsi(fmt.Sprintf(esc+"%dB", n))
  39. }
  40. // Right moves right the cursor.
  41. func Right(n uint) ANSI {
  42. if n == 0 {
  43. return empty
  44. }
  45. return newAnsi(fmt.Sprintf(esc+"%dC", n))
  46. }
  47. // Left moves left the cursor.
  48. func Left(n uint) ANSI {
  49. if n == 0 {
  50. return empty
  51. }
  52. return newAnsi(fmt.Sprintf(esc+"%dD", n))
  53. }
  54. // NextLine moves down the cursor to head of a line.
  55. func NextLine(n uint) ANSI {
  56. if n == 0 {
  57. return empty
  58. }
  59. return newAnsi(fmt.Sprintf(esc+"%dE", n))
  60. }
  61. // PreviousLine moves up the cursor to head of a line.
  62. func PreviousLine(n uint) ANSI {
  63. if n == 0 {
  64. return empty
  65. }
  66. return newAnsi(fmt.Sprintf(esc+"%dF", n))
  67. }
  68. // Column set the cursor position to a given column.
  69. func Column(col uint) ANSI {
  70. return newAnsi(fmt.Sprintf(esc+"%dG", col))
  71. }
  72. // Position set the cursor position to a given absolute position.
  73. func Position(row, col uint) ANSI {
  74. return newAnsi(fmt.Sprintf(esc+"%d;%dH", row, col))
  75. }
  76. // EraseDisplay erases display by given EraseMode.
  77. func EraseDisplay(m EraseMode) ANSI {
  78. return newAnsi(fmt.Sprintf(esc+"%dJ", m))
  79. }
  80. // EraseLine erases lines by given EraseMode.
  81. func EraseLine(m EraseMode) ANSI {
  82. return newAnsi(fmt.Sprintf(esc+"%dK", m))
  83. }
  84. // ScrollUp scrolls up the page.
  85. func ScrollUp(n int) ANSI {
  86. if n == 0 {
  87. return empty
  88. }
  89. return newAnsi(fmt.Sprintf(esc+"%dS", n))
  90. }
  91. // ScrollDown scrolls down the page.
  92. func ScrollDown(n int) ANSI {
  93. if n == 0 {
  94. return empty
  95. }
  96. return newAnsi(fmt.Sprintf(esc+"%dT", n))
  97. }
  98. func init() {
  99. EraseModes = struct {
  100. All EraseMode
  101. Head EraseMode
  102. Tail EraseMode
  103. }{
  104. Tail: 0,
  105. Head: 1,
  106. All: 2,
  107. }
  108. Save = newAnsi(esc + "s")
  109. Restore = newAnsi(esc + "u")
  110. Hide = newAnsi(esc + "?25l")
  111. Show = newAnsi(esc + "?25h")
  112. Report = newAnsi(esc + "6n")
  113. }