diff --git a/buffer.go b/buffer.go index f5e80d8..6726aaf 100644 --- a/buffer.go +++ b/buffer.go @@ -1,6 +1,7 @@ package buf2d import ( + "fmt" "strings" ) @@ -30,22 +31,27 @@ func NewBuffer(width, height int) *Buffer { } } +// Set sets the rune at position (x,y) to c func (b *Buffer) Set(x, y int, c rune) { b.data[y][x] = c } +// Get returns the rune at position (x,y) func (b *Buffer) Get(x, y int) rune { return b.data[y][x] } +// Size returns width and height of b func (b *Buffer) Size() (w, h int) { return b.width, b.height } +// Width returns the width of b func (b *Buffer) Width() int { return b.width } +// Height returns the height of b func (b *Buffer) Height() int { return b.height } @@ -85,11 +91,13 @@ func (b *Buffer) Sub(x, y, w, h int) *Buffer { // make slice references data := make([][]rune, h) - for dy := y; dy < y+h-1; dy++ { - col := b.data[dy] + for dy := 0; dy < h; dy++ { + col := b.data[y+dy] data[dy] = col[x : x+w] } + fmt.Println(data) + // make buffer return &Buffer{ data: data, diff --git a/buffer_sub_test.go b/buffer_test.go similarity index 87% rename from buffer_sub_test.go rename to buffer_test.go index df27cd1..dea498f 100644 --- a/buffer_sub_test.go +++ b/buffer_test.go @@ -11,6 +11,7 @@ func TestSub(t *testing.T) { s := b.Sub(1, 1, b.Width()-1, b.Height()-1) b.Set(5, 1, 'a') s.Set(5, 5, 'b') + b.WriteString("Hello world", 1, 2) fmt.Println(b) fmt.Println(strings.Repeat("-", 20)) diff --git a/write_string.go b/write_string.go new file mode 100644 index 0000000..8424846 --- /dev/null +++ b/write_string.go @@ -0,0 +1,15 @@ +package buf2d + +import "fmt" + +// WriteString writes a whole string to the buffer at position (x,y) +// no word wrap is applied at all. If the string does not fit, it will be truncated +func (b *Buffer) WriteString(str string, x, y int) { + for dx, r := range str { + if x+dx >= b.width { + return + } + fmt.Println(x+dx, y, string(r)) + b.Set(x+dx, y, r) + } +}