8/2024 - Forking this to try and add line-handling to it so's I can slap together a mini text editor.
This package provides a Gap Buffer data structure implementation for Go.
Gap Buffer data structure allows efficient insertion and deletion operations clustered near the same location. Gap buffers are especially common in text editors, where most changes to the text occur at or near the current location of the cursor. The text is stored in a large buffer in two contiguous segments, with a gap between them for inserting new text. Moving the cursor involves copying text from one side of the gap to the other (sometimes copying is delayed until the next operation that changes the text). Insertion adds new text at the end of the first segment; deletion deletes it.
go get github.com/neelanjan00/gap-bufferimport gapbuffer "github.com/neelanjan00/gap-buffer"The gap buffer implementation provides the following operations:
SetString returns the text stored in the buffer.
GetString returns the text stored in the buffer.
MoveCursorRight moves the cursor position to the right by one step.
MoveCursorLeft moves the cursor position to the left by one step.
Delete deletes a character immediately after the cursor.
Backspace deletes a character immediately before the cursor.
Insert inserts a single character at the cursor position.
Run this example in Go Playground
package main
import (
"fmt"
gapbuffer "github.com/neelanjan00/gap-buffer"
)
func main() {
// create a GapBuffer struct variable
gb := new(gapbuffer.GapBuffer)
// initialize a string to the buffer
gb.SetString("A Buffer")
// display the text from buffer
fmt.Println(gb.GetString())
// move the cursor to right
gb.MoveCursorRight()
// insert character
gb.Insert(' ')
gb.Insert('G')
gb.Insert('a')
gb.Insert('p')
fmt.Println(gb.GetString())
// move cursor to left
gb.MoveCursorLeft()
gb.MoveCursorLeft()
gb.MoveCursorLeft()
// backspace
gb.Backspace()
fmt.Println(gb.GetString())
gb.MoveCursorLeft()
// delete
gb.Delete()
fmt.Println(gb.GetString())
}The GIF example shown above is created using skeeto/gap-buffer-animator
