buffer.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. package readline
  2. import (
  3. "fmt"
  4. "os"
  5. "github.com/emirpasic/gods/lists/arraylist"
  6. "golang.org/x/term"
  7. )
  8. type Buffer struct {
  9. Pos int
  10. Buf *arraylist.List
  11. Prompt *Prompt
  12. LineWidth int
  13. Width int
  14. Height int
  15. }
  16. func NewBuffer(prompt *Prompt) (*Buffer, error) {
  17. fd := int(os.Stdout.Fd())
  18. width, height, err := term.GetSize(fd)
  19. if err != nil {
  20. fmt.Println("Error getting size:", err)
  21. return nil, err
  22. }
  23. lwidth := width - len(prompt.prompt())
  24. b := &Buffer{
  25. Pos: 0,
  26. Buf: arraylist.New(),
  27. Prompt: prompt,
  28. Width: width,
  29. Height: height,
  30. LineWidth: lwidth,
  31. }
  32. return b, nil
  33. }
  34. func (b *Buffer) MoveLeft() {
  35. if b.Pos > 0 {
  36. if b.Pos%b.LineWidth == 0 {
  37. fmt.Printf(CursorUp + CursorBOL + cursorRightN(b.Width))
  38. } else {
  39. fmt.Print(CursorLeft)
  40. }
  41. b.Pos -= 1
  42. }
  43. }
  44. func (b *Buffer) MoveLeftWord() {
  45. if b.Pos > 0 {
  46. var foundNonspace bool
  47. for {
  48. v, _ := b.Buf.Get(b.Pos - 1)
  49. if v == ' ' {
  50. if foundNonspace {
  51. break
  52. }
  53. } else {
  54. foundNonspace = true
  55. }
  56. b.MoveLeft()
  57. if b.Pos == 0 {
  58. break
  59. }
  60. }
  61. }
  62. }
  63. func (b *Buffer) MoveRight() {
  64. if b.Pos < b.Size() {
  65. b.Pos += 1
  66. if b.Pos%b.LineWidth == 0 {
  67. fmt.Printf(CursorDown + CursorBOL + cursorRightN(len(b.Prompt.prompt())))
  68. } else {
  69. fmt.Print(CursorRight)
  70. }
  71. }
  72. }
  73. func (b *Buffer) MoveRightWord() {
  74. if b.Pos < b.Size() {
  75. for {
  76. b.MoveRight()
  77. v, _ := b.Buf.Get(b.Pos)
  78. if v == ' ' {
  79. break
  80. }
  81. if b.Pos == b.Size() {
  82. break
  83. }
  84. }
  85. }
  86. }
  87. func (b *Buffer) MoveToStart() {
  88. if b.Pos > 0 {
  89. currLine := b.Pos / b.LineWidth
  90. if currLine > 0 {
  91. for cnt := 0; cnt < currLine; cnt++ {
  92. fmt.Print(CursorUp)
  93. }
  94. }
  95. fmt.Printf(CursorBOL + cursorRightN(len(b.Prompt.prompt())))
  96. b.Pos = 0
  97. }
  98. }
  99. func (b *Buffer) MoveToEnd() {
  100. if b.Pos < b.Size() {
  101. currLine := b.Pos / b.LineWidth
  102. totalLines := b.Size() / b.LineWidth
  103. if currLine < totalLines {
  104. for cnt := 0; cnt < totalLines-currLine; cnt++ {
  105. fmt.Print(CursorDown)
  106. }
  107. remainder := b.Size() % b.LineWidth
  108. fmt.Printf(CursorBOL + cursorRightN(len(b.Prompt.prompt())+remainder))
  109. } else {
  110. fmt.Print(cursorRightN(b.Size() - b.Pos))
  111. }
  112. b.Pos = b.Size()
  113. }
  114. }
  115. func (b *Buffer) Size() int {
  116. return b.Buf.Size()
  117. }
  118. func (b *Buffer) Add(r rune) {
  119. if b.Pos == b.Buf.Size() {
  120. fmt.Printf("%c", r)
  121. b.Buf.Add(r)
  122. b.Pos += 1
  123. if b.Pos > 0 && b.Pos%b.LineWidth == 0 {
  124. fmt.Printf("\n%s", b.Prompt.AltPrompt)
  125. }
  126. } else {
  127. fmt.Printf("%c", r)
  128. b.Buf.Insert(b.Pos, r)
  129. b.Pos += 1
  130. if b.Pos > 0 && b.Pos%b.LineWidth == 0 {
  131. fmt.Printf("\n%s", b.Prompt.AltPrompt)
  132. }
  133. b.drawRemaining()
  134. }
  135. }
  136. func (b *Buffer) drawRemaining() {
  137. var place int
  138. remainingText := b.StringN(b.Pos)
  139. if b.Pos > 0 {
  140. place = b.Pos % b.LineWidth
  141. }
  142. fmt.Print(CursorHide)
  143. // render the rest of the current line
  144. currLine := remainingText[:min(b.LineWidth-place, len(remainingText))]
  145. if len(currLine) > 0 {
  146. fmt.Printf(ClearToEOL + currLine)
  147. fmt.Print(cursorLeftN(len(currLine)))
  148. } else {
  149. fmt.Print(ClearToEOL)
  150. }
  151. // render the other lines
  152. if len(remainingText) > len(currLine) {
  153. remaining := []rune(remainingText[len(currLine):])
  154. var totalLines int
  155. for i, c := range remaining {
  156. if i%b.LineWidth == 0 {
  157. fmt.Printf("\n%s", b.Prompt.AltPrompt)
  158. totalLines += 1
  159. }
  160. fmt.Printf("%c", c)
  161. }
  162. fmt.Print(ClearToEOL)
  163. fmt.Print(cursorUpN(totalLines))
  164. fmt.Printf(CursorBOL + cursorRightN(b.Width-len(currLine)))
  165. }
  166. fmt.Print(CursorShow)
  167. }
  168. func (b *Buffer) Remove() {
  169. if b.Buf.Size() > 0 && b.Pos > 0 {
  170. if b.Pos%b.LineWidth == 0 {
  171. // if the user backspaces over the word boundary, do this magic to clear the line
  172. // and move to the end of the previous line
  173. fmt.Printf(CursorBOL + ClearToEOL)
  174. fmt.Printf(CursorUp + CursorBOL + cursorRightN(b.Width) + " " + CursorLeft)
  175. } else {
  176. fmt.Printf(CursorLeft + " " + CursorLeft)
  177. }
  178. var eraseExtraLine bool
  179. if (b.Size()-1)%b.LineWidth == 0 {
  180. eraseExtraLine = true
  181. }
  182. b.Pos -= 1
  183. b.Buf.Remove(b.Pos)
  184. if b.Pos < b.Size() {
  185. b.drawRemaining()
  186. // this erases a line which is left over when backspacing in the middle of a line and there
  187. // are trailing characters which go over the line width boundary
  188. if eraseExtraLine {
  189. remainingLines := (b.Size() - b.Pos) / b.LineWidth
  190. fmt.Printf(cursorDownN(remainingLines+1) + CursorBOL + ClearToEOL)
  191. place := b.Pos % b.LineWidth
  192. fmt.Printf(cursorUpN(remainingLines+1) + cursorRightN(place+len(b.Prompt.prompt())))
  193. }
  194. }
  195. }
  196. }
  197. func (b *Buffer) Delete() {
  198. if b.Size() > 0 && b.Pos < b.Size() {
  199. b.Buf.Remove(b.Pos)
  200. b.drawRemaining()
  201. if b.Size()%b.LineWidth == 0 {
  202. if b.Pos != b.Size() {
  203. remainingLines := (b.Size() - b.Pos) / b.LineWidth
  204. fmt.Printf(cursorDownN(remainingLines) + CursorBOL + ClearToEOL)
  205. place := b.Pos % b.LineWidth
  206. fmt.Printf(cursorUpN(remainingLines) + cursorRightN(place+len(b.Prompt.prompt())))
  207. }
  208. }
  209. }
  210. }
  211. func (b *Buffer) DeleteBefore() {
  212. if b.Pos > 0 {
  213. for cnt := b.Pos - 1; cnt >= 0; cnt-- {
  214. b.Remove()
  215. }
  216. }
  217. }
  218. func (b *Buffer) DeleteRemaining() {
  219. if b.Size() > 0 && b.Pos < b.Size() {
  220. charsToDel := b.Size() - b.Pos
  221. for cnt := 0; cnt < charsToDel; cnt++ {
  222. b.Delete()
  223. }
  224. }
  225. }
  226. func (b *Buffer) DeleteWord() {
  227. if b.Buf.Size() > 0 && b.Pos > 0 {
  228. var foundNonspace bool
  229. for {
  230. v, _ := b.Buf.Get(b.Pos - 1)
  231. if v == ' ' {
  232. if !foundNonspace {
  233. b.Remove()
  234. } else {
  235. break
  236. }
  237. } else {
  238. foundNonspace = true
  239. b.Remove()
  240. }
  241. if b.Pos == 0 {
  242. break
  243. }
  244. }
  245. }
  246. }
  247. func (b *Buffer) ClearScreen() {
  248. fmt.Printf(ClearScreen + CursorReset + b.Prompt.prompt())
  249. if b.IsEmpty() {
  250. ph := b.Prompt.placeholder()
  251. fmt.Printf(ColorGrey + ph + cursorLeftN(len(ph)) + ColorDefault)
  252. } else {
  253. currPos := b.Pos
  254. b.Pos = 0
  255. b.drawRemaining()
  256. fmt.Printf(CursorReset + cursorRightN(len(b.Prompt.prompt())))
  257. if currPos > 0 {
  258. targetLine := currPos / b.LineWidth
  259. if targetLine > 0 {
  260. for cnt := 0; cnt < targetLine; cnt++ {
  261. fmt.Print(CursorDown)
  262. }
  263. }
  264. remainder := currPos % b.LineWidth
  265. if remainder > 0 {
  266. fmt.Print(cursorRightN(remainder))
  267. }
  268. if currPos%b.LineWidth == 0 {
  269. fmt.Printf(CursorBOL + b.Prompt.AltPrompt)
  270. }
  271. }
  272. b.Pos = currPos
  273. }
  274. }
  275. func (b *Buffer) IsEmpty() bool {
  276. return b.Buf.Empty()
  277. }
  278. func (b *Buffer) Replace(r []rune) {
  279. b.Pos = 0
  280. b.Buf.Clear()
  281. fmt.Printf(ClearLine + CursorBOL + b.Prompt.prompt())
  282. for _, c := range r {
  283. b.Add(c)
  284. }
  285. }
  286. func (b *Buffer) String() string {
  287. return b.StringN(0)
  288. }
  289. func (b *Buffer) StringN(n int) string {
  290. return b.StringNM(n, 0)
  291. }
  292. func (b *Buffer) StringNM(n, m int) string {
  293. var s string
  294. if m == 0 {
  295. m = b.Size()
  296. }
  297. for cnt := n; cnt < m; cnt++ {
  298. c, _ := b.Buf.Get(cnt)
  299. s += string(c.(rune))
  300. }
  301. return s
  302. }
  303. func cursorLeftN(n int) string {
  304. return fmt.Sprintf(CursorLeftN, n)
  305. }
  306. func cursorRightN(n int) string {
  307. return fmt.Sprintf(CursorRightN, n)
  308. }
  309. func cursorUpN(n int) string {
  310. return fmt.Sprintf(CursorUpN, n)
  311. }
  312. func cursorDownN(n int) string {
  313. return fmt.Sprintf(CursorDownN, n)
  314. }