buffer.go 7.1 KB

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