runereader.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. package terminal
  2. import (
  3. "fmt"
  4. "unicode"
  5. )
  6. type RuneReader struct {
  7. stdio Stdio
  8. cursor *Cursor
  9. state runeReaderState
  10. }
  11. func NewRuneReader(stdio Stdio) *RuneReader {
  12. return &RuneReader{
  13. stdio: stdio,
  14. state: newRuneReaderState(stdio.In),
  15. }
  16. }
  17. func (rr *RuneReader) printChar(char rune, mask rune) {
  18. // if we don't need to mask the input
  19. if mask == 0 {
  20. // just print the character the user pressed
  21. fmt.Fprintf(rr.stdio.Out, "%c", char)
  22. } else {
  23. // otherwise print the mask we were given
  24. fmt.Fprintf(rr.stdio.Out, "%c", mask)
  25. }
  26. }
  27. func (rr *RuneReader) ReadLine(mask rune) ([]rune, error) {
  28. line := []rune{}
  29. // we only care about horizontal displacements from the origin so start counting at 0
  30. index := 0
  31. cursor := &Cursor{
  32. In: rr.stdio.In,
  33. Out: rr.stdio.Out,
  34. }
  35. // we get the terminal width and height (if resized after this point the property might become invalid)
  36. terminalSize, _ := cursor.Size(rr.Buffer())
  37. for {
  38. // wait for some input
  39. r, _, err := rr.ReadRune()
  40. if err != nil {
  41. return line, err
  42. }
  43. // we set the current location of the cursor and update it after every key press
  44. cursorCurrent, err := cursor.Location(rr.Buffer())
  45. // if the user pressed enter or some other newline/termination like ctrl+d
  46. if r == '\r' || r == '\n' || r == KeyEndTransmission {
  47. // delete what's printed out on the console screen (cleanup)
  48. for index > 0 {
  49. if cursorCurrent.CursorIsAtLineBegin() {
  50. EraseLine(rr.stdio.Out, ERASE_LINE_END)
  51. cursor.PreviousLine(1)
  52. cursor.Forward(int(terminalSize.X))
  53. cursorCurrent.X = terminalSize.X
  54. cursorCurrent.Y--
  55. } else {
  56. cursor.Back(1)
  57. cursorCurrent.X--
  58. }
  59. index--
  60. }
  61. // move the cursor the a new line
  62. cursor.MoveNextLine(cursorCurrent, terminalSize)
  63. // we're done processing the input
  64. return line, nil
  65. }
  66. // if the user interrupts (ie with ctrl+c)
  67. if r == KeyInterrupt {
  68. // go to the beginning of the next line
  69. fmt.Fprint(rr.stdio.Out, "\r\n")
  70. // we're done processing the input, and treat interrupt like an error
  71. return line, InterruptErr
  72. }
  73. // allow for backspace/delete editing of inputs
  74. if r == KeyBackspace || r == KeyDelete {
  75. // and we're not at the beginning of the line
  76. if index > 0 && len(line) > 0 {
  77. // if we are at the end of the word
  78. if index == len(line) {
  79. // just remove the last letter from the internal representation
  80. line = line[:len(line)-1]
  81. // go back one
  82. if cursorCurrent.X == 1 {
  83. cursor.PreviousLine(1)
  84. cursor.Forward(int(terminalSize.X))
  85. } else {
  86. cursor.Back(1)
  87. }
  88. // clear the rest of the line
  89. EraseLine(rr.stdio.Out, ERASE_LINE_END)
  90. } else {
  91. // we need to remove a character from the middle of the word
  92. // remove the current index from the list
  93. line = append(line[:index-1], line[index:]...)
  94. // save the current position of the cursor, as we have to move the cursor one back to erase the current symbol
  95. // and then move the cursor for each symbol in line[index-1:] to print it out, afterwards we want to restore
  96. // the cursor to its previous location.
  97. cursor.Save()
  98. // clear the rest of the line
  99. cursor.Back(1)
  100. // print what comes after
  101. for _, char := range line[index-1:] {
  102. //Erase symbols which are left over from older print
  103. EraseLine(rr.stdio.Out, ERASE_LINE_END)
  104. // print characters to the new line appropriately
  105. rr.printChar(char, mask)
  106. }
  107. // erase what's left over from last print
  108. if cursorCurrent.Y < terminalSize.Y {
  109. cursor.NextLine(1)
  110. EraseLine(rr.stdio.Out, ERASE_LINE_END)
  111. }
  112. // restore cursor
  113. cursor.Restore()
  114. if cursorCurrent.CursorIsAtLineBegin() {
  115. cursor.PreviousLine(1)
  116. cursor.Forward(int(terminalSize.X))
  117. } else {
  118. cursor.Back(1)
  119. }
  120. }
  121. // decrement the index
  122. index--
  123. } else {
  124. // otherwise the user pressed backspace while at the beginning of the line
  125. soundBell(rr.stdio.Out)
  126. }
  127. // we're done processing this key
  128. continue
  129. }
  130. // if the left arrow is pressed
  131. if r == KeyArrowLeft {
  132. // if we have space to the left
  133. if index > 0 {
  134. //move the cursor to the prev line if necessary
  135. if cursorCurrent.CursorIsAtLineBegin() {
  136. cursor.PreviousLine(1)
  137. cursor.Forward(int(terminalSize.X))
  138. } else {
  139. cursor.Back(1)
  140. }
  141. //decrement the index
  142. index--
  143. } else {
  144. // otherwise we are at the beginning of where we started reading lines
  145. // sound the bell
  146. soundBell(rr.stdio.Out)
  147. }
  148. // we're done processing this key press
  149. continue
  150. }
  151. // if the right arrow is pressed
  152. if r == KeyArrowRight {
  153. // if we have space to the right
  154. if index < len(line) {
  155. // move the cursor to the next line if necessary
  156. if cursorCurrent.CursorIsAtLineEnd(terminalSize) {
  157. cursor.NextLine(1)
  158. } else {
  159. cursor.Forward(1)
  160. }
  161. index++
  162. } else {
  163. // otherwise we are at the end of the word and can't go past
  164. // sound the bell
  165. soundBell(rr.stdio.Out)
  166. }
  167. // we're done processing this key press
  168. continue
  169. }
  170. // the user pressed one of the special keys
  171. if r == SpecialKeyHome {
  172. for index > 0 {
  173. if cursorCurrent.CursorIsAtLineBegin() {
  174. cursor.PreviousLine(1)
  175. cursor.Forward(int(terminalSize.X))
  176. cursorCurrent.X = terminalSize.X
  177. cursorCurrent.Y--
  178. } else {
  179. cursor.Back(1)
  180. cursorCurrent.X--
  181. }
  182. index--
  183. }
  184. continue
  185. // user pressed end
  186. } else if r == SpecialKeyEnd {
  187. for index != len(line) {
  188. if cursorCurrent.CursorIsAtLineEnd(terminalSize) {
  189. cursor.NextLine(1)
  190. cursorCurrent.X = COORDINATE_SYSTEM_BEGIN
  191. cursorCurrent.Y++
  192. } else {
  193. cursor.Forward(1)
  194. cursorCurrent.X++
  195. }
  196. index++
  197. }
  198. continue
  199. // user pressed forward delete key
  200. } else if r == SpecialKeyDelete {
  201. // if index at the end of the line nothing to delete
  202. if index != len(line) {
  203. // save the current position of the cursor, as we have to erase the current symbol
  204. // and then move the cursor for each symbol in line[index:] to print it out, afterwards we want to restore
  205. // the cursor to its previous location.
  206. cursor.Save()
  207. // remove the symbol after the cursor
  208. line = append(line[:index], line[index+1:]...)
  209. // print the updated line
  210. for _, char := range line[index:] {
  211. EraseLine(rr.stdio.Out, ERASE_LINE_END)
  212. // print out the character
  213. rr.printChar(char, mask)
  214. }
  215. // erase what's left on last line
  216. if cursorCurrent.Y < terminalSize.Y {
  217. cursor.NextLine(1)
  218. EraseLine(rr.stdio.Out, ERASE_LINE_END)
  219. }
  220. // restore cursor
  221. cursor.Restore()
  222. if len(line) == 0 || index == len(line) {
  223. EraseLine(rr.stdio.Out, ERASE_LINE_END)
  224. }
  225. }
  226. continue
  227. }
  228. // if the letter is another escape sequence
  229. if unicode.IsControl(r) || r == IgnoreKey {
  230. // ignore it
  231. continue
  232. }
  233. // the user pressed a regular key
  234. // if we are at the end of the line
  235. if index == len(line) {
  236. // just append the character at the end of the line
  237. line = append(line, r)
  238. // save the location of the cursor
  239. index++
  240. // print out the character
  241. rr.printChar(r, mask)
  242. } else {
  243. // we are in the middle of the word so we need to insert the character the user pressed
  244. line = append(line[:index], append([]rune{r}, line[index:]...)...)
  245. // save the current position of the cursor, as we have to move the cursor back to erase the current symbol
  246. // and then move for each symbol in line[index:] to print it out, afterwards we want to restore
  247. // cursor's location to its previous one.
  248. cursor.Save()
  249. EraseLine(rr.stdio.Out, ERASE_LINE_END)
  250. // remove the symbol after the cursor
  251. // print the updated line
  252. for _, char := range line[index:] {
  253. EraseLine(rr.stdio.Out, ERASE_LINE_END)
  254. // print out the character
  255. rr.printChar(char, mask)
  256. cursorCurrent.X++
  257. }
  258. // if we are at the last line, we want to visually insert a new line and append to it.
  259. if cursorCurrent.CursorIsAtLineEnd(terminalSize) && cursorCurrent.Y == terminalSize.Y {
  260. // add a new line to the terminal
  261. fmt.Fprintln(rr.stdio.Out)
  262. // restore the position of the cursor horizontally
  263. cursor.Restore()
  264. // restore the position of the cursor vertically
  265. cursor.Up(1)
  266. } else {
  267. // restore cursor
  268. cursor.Restore()
  269. }
  270. // check if cursor needs to move to next line
  271. cursorCurrent, _ = cursor.Location(rr.Buffer())
  272. if cursorCurrent.CursorIsAtLineEnd(terminalSize) {
  273. cursor.NextLine(1)
  274. } else {
  275. cursor.Forward(1)
  276. }
  277. // increment the index
  278. index++
  279. }
  280. }
  281. }