buffer.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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 min(n, m int) int {
  119. if n > m {
  120. return m
  121. }
  122. return n
  123. }
  124. func (b *Buffer) Add(r rune) {
  125. if b.Pos == b.Buf.Size() {
  126. fmt.Printf("%c", r)
  127. b.Buf.Add(r)
  128. b.Pos += 1
  129. if b.Pos > 0 && b.Pos%b.LineWidth == 0 {
  130. fmt.Printf("\n%s", b.Prompt.AltPrompt)
  131. }
  132. } else {
  133. fmt.Printf("%c", r)
  134. b.Buf.Insert(b.Pos, r)
  135. b.Pos += 1
  136. if b.Pos > 0 && b.Pos%b.LineWidth == 0 {
  137. fmt.Printf("\n%s", b.Prompt.AltPrompt)
  138. }
  139. b.drawRemaining()
  140. }
  141. }
  142. func (b *Buffer) drawRemaining() {
  143. var place int
  144. remainingText := b.StringN(b.Pos)
  145. if b.Pos > 0 {
  146. place = b.Pos % b.LineWidth
  147. }
  148. fmt.Print(CursorHide)
  149. // render the rest of the current line
  150. currLine := remainingText[:min(b.LineWidth-place, len(remainingText))]
  151. if len(currLine) > 0 {
  152. fmt.Printf(ClearToEOL + currLine)
  153. fmt.Print(cursorLeftN(len(currLine)))
  154. } else {
  155. fmt.Print(ClearToEOL)
  156. }
  157. // render the other lines
  158. if len(remainingText) > len(currLine) {
  159. remaining := []rune(remainingText[len(currLine):])
  160. var totalLines int
  161. for i, c := range remaining {
  162. if i%b.LineWidth == 0 {
  163. fmt.Printf("\n%s", b.Prompt.AltPrompt)
  164. totalLines += 1
  165. }
  166. fmt.Printf("%c", c)
  167. }
  168. fmt.Print(ClearToEOL)
  169. fmt.Print(cursorUpN(totalLines))
  170. fmt.Printf(CursorBOL + cursorRightN(b.Width-len(currLine)))
  171. }
  172. fmt.Print(CursorShow)
  173. }
  174. func (b *Buffer) Remove() {
  175. if b.Buf.Size() > 0 && b.Pos > 0 {
  176. if b.Pos%b.LineWidth == 0 {
  177. // if the user backspaces over the word boundary, do this magic to clear the line
  178. // and move to the end of the previous line
  179. fmt.Printf(CursorBOL + ClearToEOL)
  180. fmt.Printf(CursorUp + CursorBOL + cursorRightN(b.Width) + " " + CursorLeft)
  181. } else {
  182. fmt.Printf(CursorLeft + " " + CursorLeft)
  183. }
  184. var eraseExtraLine bool
  185. if (b.Size()-1)%b.LineWidth == 0 {
  186. eraseExtraLine = true
  187. }
  188. b.Pos -= 1
  189. b.Buf.Remove(b.Pos)
  190. if b.Pos < b.Size() {
  191. b.drawRemaining()
  192. // this erases a line which is left over when backspacing in the middle of a line and there
  193. // are trailing characters which go over the line width boundary
  194. if eraseExtraLine {
  195. remainingLines := (b.Size() - b.Pos) / b.LineWidth
  196. fmt.Printf(cursorDownN(remainingLines+1) + CursorBOL + ClearToEOL)
  197. place := b.Pos % b.LineWidth
  198. fmt.Printf(cursorUpN(remainingLines+1) + cursorRightN(place+len(b.Prompt.prompt())))
  199. }
  200. }
  201. }
  202. }
  203. func (b *Buffer) Delete() {
  204. if b.Size() > 0 && b.Pos < b.Size() {
  205. b.Buf.Remove(b.Pos)
  206. b.drawRemaining()
  207. if b.Size()%b.LineWidth == 0 {
  208. if b.Pos != b.Size() {
  209. remainingLines := (b.Size() - b.Pos) / b.LineWidth
  210. fmt.Printf(cursorDownN(remainingLines) + CursorBOL + ClearToEOL)
  211. place := b.Pos % b.LineWidth
  212. fmt.Printf(cursorUpN(remainingLines) + cursorRightN(place+len(b.Prompt.prompt())))
  213. }
  214. }
  215. }
  216. }
  217. func (b *Buffer) DeleteBefore() {
  218. if b.Pos > 0 {
  219. for cnt := b.Pos - 1; cnt >= 0; cnt-- {
  220. b.Remove()
  221. }
  222. }
  223. }
  224. func (b *Buffer) DeleteRemaining() {
  225. if b.Size() > 0 && b.Pos < b.Size() {
  226. charsToDel := b.Size() - b.Pos
  227. for cnt := 0; cnt < charsToDel; cnt++ {
  228. b.Delete()
  229. }
  230. }
  231. }
  232. func (b *Buffer) DeleteWord() {
  233. if b.Buf.Size() > 0 && b.Pos > 0 {
  234. var foundNonspace bool
  235. for {
  236. v, _ := b.Buf.Get(b.Pos - 1)
  237. if v == ' ' {
  238. if !foundNonspace {
  239. b.Remove()
  240. } else {
  241. break
  242. }
  243. } else {
  244. foundNonspace = true
  245. b.Remove()
  246. }
  247. if b.Pos == 0 {
  248. break
  249. }
  250. }
  251. }
  252. }
  253. func (b *Buffer) ClearScreen() {
  254. fmt.Printf(ClearScreen + CursorReset + b.Prompt.prompt())
  255. if b.IsEmpty() {
  256. ph := b.Prompt.placeholder()
  257. fmt.Printf(ColorGrey + ph + cursorLeftN(len(ph)) + ColorDefault)
  258. } else {
  259. currPos := b.Pos
  260. b.Pos = 0
  261. b.drawRemaining()
  262. fmt.Printf(CursorReset + cursorRightN(len(b.Prompt.prompt())))
  263. if currPos > 0 {
  264. targetLine := currPos / b.LineWidth
  265. if targetLine > 0 {
  266. for cnt := 0; cnt < targetLine; cnt++ {
  267. fmt.Print(CursorDown)
  268. }
  269. }
  270. remainder := currPos % b.LineWidth
  271. if remainder > 0 {
  272. fmt.Print(cursorRightN(remainder))
  273. }
  274. if currPos%b.LineWidth == 0 {
  275. fmt.Printf(CursorBOL + b.Prompt.AltPrompt)
  276. }
  277. }
  278. b.Pos = currPos
  279. }
  280. }
  281. func (b *Buffer) IsEmpty() bool {
  282. return b.Buf.Empty()
  283. }
  284. func (b *Buffer) Replace(r []rune) {
  285. b.Pos = 0
  286. b.Buf.Clear()
  287. fmt.Printf(ClearLine + CursorBOL + b.Prompt.prompt())
  288. for _, c := range r {
  289. b.Add(c)
  290. }
  291. }
  292. func (b *Buffer) String() string {
  293. return b.StringN(0)
  294. }
  295. func (b *Buffer) StringN(n int) string {
  296. return b.StringNM(n, 0)
  297. }
  298. func (b *Buffer) StringNM(n, m int) string {
  299. var s string
  300. if m == 0 {
  301. m = b.Size()
  302. }
  303. for cnt := n; cnt < m; cnt++ {
  304. c, _ := b.Buf.Get(cnt)
  305. s += string(c.(rune))
  306. }
  307. return s
  308. }
  309. func cursorLeftN(n int) string {
  310. return fmt.Sprintf(CursorLeftN, n)
  311. }
  312. func cursorRightN(n int) string {
  313. return fmt.Sprintf(CursorRightN, n)
  314. }
  315. func cursorUpN(n int) string {
  316. return fmt.Sprintf(CursorUpN, n)
  317. }
  318. func cursorDownN(n int) string {
  319. return fmt.Sprintf(CursorDownN, n)
  320. }