cursor.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // +build !windows
  2. package terminal
  3. import (
  4. "bufio"
  5. "bytes"
  6. "fmt"
  7. "regexp"
  8. "strconv"
  9. )
  10. var COORDINATE_SYSTEM_BEGIN Short = 1
  11. var dsrPattern = regexp.MustCompile(`\x1b\[(\d+);(\d+)R$`)
  12. type Cursor struct {
  13. In FileReader
  14. Out FileWriter
  15. }
  16. // Up moves the cursor n cells to up.
  17. func (c *Cursor) Up(n int) {
  18. fmt.Fprintf(c.Out, "\x1b[%dA", n)
  19. }
  20. // Down moves the cursor n cells to down.
  21. func (c *Cursor) Down(n int) {
  22. fmt.Fprintf(c.Out, "\x1b[%dB", n)
  23. }
  24. // Forward moves the cursor n cells to right.
  25. func (c *Cursor) Forward(n int) {
  26. fmt.Fprintf(c.Out, "\x1b[%dC", n)
  27. }
  28. // Back moves the cursor n cells to left.
  29. func (c *Cursor) Back(n int) {
  30. fmt.Fprintf(c.Out, "\x1b[%dD", n)
  31. }
  32. // NextLine moves cursor to beginning of the line n lines down.
  33. func (c *Cursor) NextLine(n int) {
  34. fmt.Fprintf(c.Out, "\x1b[%dE", n)
  35. }
  36. // PreviousLine moves cursor to beginning of the line n lines up.
  37. func (c *Cursor) PreviousLine(n int) {
  38. fmt.Fprintf(c.Out, "\x1b[%dF", n)
  39. }
  40. // HorizontalAbsolute moves cursor horizontally to x.
  41. func (c *Cursor) HorizontalAbsolute(x int) {
  42. fmt.Fprintf(c.Out, "\x1b[%dG", x)
  43. }
  44. // Show shows the cursor.
  45. func (c *Cursor) Show() {
  46. fmt.Fprint(c.Out, "\x1b[?25h")
  47. }
  48. // Hide hide the cursor.
  49. func (c *Cursor) Hide() {
  50. fmt.Fprint(c.Out, "\x1b[?25l")
  51. }
  52. // Move moves the cursor to a specific x,y location.
  53. func (c *Cursor) Move(x int, y int) {
  54. fmt.Fprintf(c.Out, "\x1b[%d;%df", x, y)
  55. }
  56. // Save saves the current position
  57. func (c *Cursor) Save() {
  58. fmt.Fprint(c.Out, "\x1b7")
  59. }
  60. // Restore restores the saved position of the cursor
  61. func (c *Cursor) Restore() {
  62. fmt.Fprint(c.Out, "\x1b8")
  63. }
  64. // for comparability purposes between windows
  65. // in unix we need to print out a new line on some terminals
  66. func (c *Cursor) MoveNextLine(cur *Coord, terminalSize *Coord) {
  67. if cur.Y == terminalSize.Y {
  68. fmt.Fprintln(c.Out)
  69. }
  70. c.NextLine(1)
  71. }
  72. // Location returns the current location of the cursor in the terminal
  73. func (c *Cursor) Location(buf *bytes.Buffer) (*Coord, error) {
  74. // ANSI escape sequence for DSR - Device Status Report
  75. // https://en.wikipedia.org/wiki/ANSI_escape_code#CSI_sequences
  76. fmt.Fprint(c.Out, "\x1b[6n")
  77. // There may be input in Stdin prior to CursorLocation so make sure we don't
  78. // drop those bytes.
  79. var loc []int
  80. var match string
  81. for loc == nil {
  82. // Reports the cursor position (CPR) to the application as (as though typed at
  83. // the keyboard) ESC[n;mR, where n is the row and m is the column.
  84. reader := bufio.NewReader(c.In)
  85. text, err := reader.ReadSlice('R')
  86. if err != nil {
  87. return nil, err
  88. }
  89. loc = dsrPattern.FindStringIndex(string(text))
  90. if loc == nil {
  91. // Stdin contains R that doesn't match DSR.
  92. buf.Write(text)
  93. } else {
  94. buf.Write(text[:loc[0]])
  95. match = string(text[loc[0]:loc[1]])
  96. }
  97. }
  98. matches := dsrPattern.FindStringSubmatch(string(match))
  99. if len(matches) != 3 {
  100. return nil, fmt.Errorf("incorrect number of matches: %d", len(matches))
  101. }
  102. col, err := strconv.Atoi(matches[2])
  103. if err != nil {
  104. return nil, err
  105. }
  106. row, err := strconv.Atoi(matches[1])
  107. if err != nil {
  108. return nil, err
  109. }
  110. return &Coord{Short(col), Short(row)}, nil
  111. }
  112. func (cur Coord) CursorIsAtLineEnd(size *Coord) bool {
  113. return cur.X == size.X
  114. }
  115. func (cur Coord) CursorIsAtLineBegin() bool {
  116. return cur.X == COORDINATE_SYSTEM_BEGIN
  117. }
  118. // Size returns the height and width of the terminal.
  119. func (c *Cursor) Size(buf *bytes.Buffer) (*Coord, error) {
  120. // the general approach here is to move the cursor to the very bottom
  121. // of the terminal, ask for the current location and then move the
  122. // cursor back where we started
  123. // hide the cursor (so it doesn't blink when getting the size of the terminal)
  124. c.Hide()
  125. // save the current location of the cursor
  126. c.Save()
  127. // move the cursor to the very bottom of the terminal
  128. c.Move(999, 999)
  129. // ask for the current location
  130. bottom, err := c.Location(buf)
  131. if err != nil {
  132. return nil, err
  133. }
  134. // move back where we began
  135. c.Restore()
  136. // show the cursor
  137. c.Show()
  138. // sice the bottom was calcuated in the lower right corner, it
  139. // is the dimensions we are looking for
  140. return bottom, nil
  141. }