1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
package vttest
import (
"strings"
"unicode/utf8"
"github.com/tonistiigi/vt100"
)
// FromLines generates a VT100 from content text.
// Each line must have the same number of runes.
func FromLines(s string) *vt100.VT100 {
return FromLinesAndFormats(s, nil)
}
// FromLinesAndFormats generates a *VT100 whose state is set according
// to s (for content) and a (for attributes).
//
// Dimensions are set to the width of s' first line and the height of the
// number of lines in s.
//
// If a is nil, the default attributes are used.
func FromLinesAndFormats(s string, a [][]vt100.Format) *vt100.VT100 {
lines := strings.Split(s, "\n")
v := vt100.NewVT100(len(lines), utf8.RuneCountInString(lines[0]))
for y := 0; y < v.Height; y++ {
x := 0
for _, r := range lines[y] {
v.Content[y][x] = r
if a != nil {
v.Format[y][x] = a[y][x]
}
x++
}
}
return v
}
|