cursor.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // +build !windows
  2. package terminal
  3. import (
  4. "bufio"
  5. "fmt"
  6. "os"
  7. "regexp"
  8. "strconv"
  9. "strings"
  10. )
  11. // CursorUp moves the cursor n cells to up.
  12. func CursorUp(n int) {
  13. fmt.Printf("\x1b[%dA", n)
  14. }
  15. // CursorDown moves the cursor n cells to down.
  16. func CursorDown(n int) {
  17. fmt.Printf("\x1b[%dB", n)
  18. }
  19. // CursorForward moves the cursor n cells to right.
  20. func CursorForward(n int) {
  21. fmt.Printf("\x1b[%dC", n)
  22. }
  23. // CursorBack moves the cursor n cells to left.
  24. func CursorBack(n int) {
  25. fmt.Printf("\x1b[%dD", n)
  26. }
  27. // CursorNextLine moves cursor to beginning of the line n lines down.
  28. func CursorNextLine(n int) {
  29. fmt.Printf("\x1b[%dE", n)
  30. }
  31. // CursorPreviousLine moves cursor to beginning of the line n lines up.
  32. func CursorPreviousLine(n int) {
  33. fmt.Printf("\x1b[%dF", n)
  34. }
  35. // CursorHorizontalAbsolute moves cursor horizontally to x.
  36. func CursorHorizontalAbsolute(x int) {
  37. fmt.Printf("\x1b[%dG", x)
  38. }
  39. // CursorShow shows the cursor.
  40. func CursorShow() {
  41. fmt.Print("\x1b[?25h")
  42. }
  43. // CursorHide hide the cursor.
  44. func CursorHide() {
  45. fmt.Print("\x1b[?25l")
  46. }
  47. // CursorMove moves the cursor to a specific x,y location.
  48. func CursorMove(x int, y int) {
  49. fmt.Printf("\x1b[%d;%df", x, y)
  50. }
  51. // CursorLocation returns the current location of the cursor in the terminal
  52. func CursorLocation() (*Coord, error) {
  53. // print the escape sequence to receive the position in our stdin
  54. fmt.Print("\x1b[6n")
  55. // read from stdin to get the response
  56. reader := bufio.NewReader(os.Stdin)
  57. // spec says we read 'til R, so do that
  58. text, err := reader.ReadSlice('R')
  59. if err != nil {
  60. return nil, err
  61. }
  62. // spec also says they're split by ;, so do that too
  63. if strings.Contains(string(text), ";") {
  64. // a regex to parse the output of the ansi code
  65. re := regexp.MustCompile(`\d+;\d+`)
  66. line := re.FindString(string(text))
  67. // find the column and rows embedded in the string
  68. coords := strings.Split(line, ";")
  69. // try to cast the col number to an int
  70. col, err := strconv.Atoi(coords[1])
  71. if err != nil {
  72. return nil, err
  73. }
  74. // try to cast the row number to an int
  75. row, err := strconv.Atoi(coords[0])
  76. if err != nil {
  77. return nil, err
  78. }
  79. // return the coordinate object with the col and row we calculated
  80. return &Coord{Short(col), Short(row)}, nil
  81. }
  82. // it didn't work so return an error
  83. return nil, fmt.Errorf("could not compute the cursor position using ascii escape sequences")
  84. }
  85. // Size returns the height and width of the terminal.
  86. func Size() (*Coord, error) {
  87. // the general approach here is to move the cursor to the very bottom
  88. // of the terminal, ask for the current location and then move the
  89. // cursor back where we started
  90. // save the current location of the cursor
  91. origin, err := CursorLocation()
  92. if err != nil {
  93. return nil, err
  94. }
  95. // move the cursor to the very bottom of the terminal
  96. CursorMove(999, 999)
  97. // ask for the current location
  98. bottom, err := CursorLocation()
  99. if err != nil {
  100. return nil, err
  101. }
  102. // move back where we began
  103. CursorUp(int(bottom.Y - origin.Y))
  104. CursorHorizontalAbsolute(int(origin.X))
  105. // sice the bottom was calcuated in the lower right corner, it
  106. // is the dimensions we are looking for
  107. return bottom, nil
  108. }