buffer.go 7.2 KB

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