WriteString method added
This commit is contained in:
parent
060c430690
commit
85dbadcaff
12
buffer.go
12
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,
|
||||
|
@ -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))
|
15
write_string.go
Normal file
15
write_string.go
Normal file
@ -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)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user