Convert View.viewLines to []viewLine

This commit is contained in:
Roi Martin 2015-02-03 18:29:38 +01:00
parent 0814e8024f
commit b1d190d0d7

37
view.go
View File

@ -25,8 +25,8 @@ type View struct {
readOffset int readOffset int
readCache string readCache string
tainted bool // marks if the viewBuffer must be updated tainted bool // marks if the viewBuffer must be updated
viewBuffer [][]rune // internal representation of the view's buffer viewLines []viewLine // internal representation of the view's buffer
// BgColor and FgColor allow to configure the background and foreground // BgColor and FgColor allow to configure the background and foreground
// colors of the View. // colors of the View.
@ -60,6 +60,12 @@ type View struct {
Autoscroll bool Autoscroll bool
} }
type viewLine struct {
linesX, linesY int
wrapped bool
line []rune
}
// newView returns a new View object. // newView returns a new View object.
func newView(name string, x0, y0, x1, y1 int) *View { func newView(name string, x0, y0, x1, y1 int) *View {
v := &View{ v := &View{
@ -207,36 +213,41 @@ func (v *View) draw() error {
} }
if v.tainted { if v.tainted {
v.viewBuffer = nil v.viewLines = nil
for _, line := range v.lines { for i, line := range v.lines {
if v.Wrap { if v.Wrap {
if len(line) <= maxX { if len(line) <= maxX {
v.viewBuffer = append(v.viewBuffer, line) vline := viewLine{linesX: 0, linesY: i, line: line}
v.viewLines = append(v.viewLines, vline)
continue continue
} else { } else {
v.viewBuffer = append(v.viewBuffer, line[:maxX]) vline := viewLine{linesX: 0, linesY: i, line: line[:maxX]}
v.viewLines = append(v.viewLines, vline)
} }
// Append remaining lines with WrapPrefix // Append remaining lines with WrapPrefix
for n := maxX; n < len(line); n += maxX - len(v.WrapPrefix) { for n := maxX; n < len(line); n += maxX - len(v.WrapPrefix) {
wrappedLine := append(append([]rune(v.WrapPrefix), line[n:]...)) wrappedLine := append(append([]rune(v.WrapPrefix), line[n:]...))
if len(wrappedLine) <= maxX { if len(wrappedLine) <= maxX {
v.viewBuffer = append(v.viewBuffer, wrappedLine) vline := viewLine{linesX: n, linesY: i, wrapped: true, line: wrappedLine}
v.viewLines = append(v.viewLines, vline)
} else { } else {
v.viewBuffer = append(v.viewBuffer, wrappedLine[:maxX]) vline := viewLine{linesX: n, linesY: i, wrapped: true, line: wrappedLine[:maxX]}
v.viewLines = append(v.viewLines, vline)
} }
} }
} else { } else {
v.viewBuffer = append(v.viewBuffer, line) vline := viewLine{linesX: 0, linesY: i, line: line}
v.viewLines = append(v.viewLines, vline)
} }
} }
v.tainted = false v.tainted = false
} }
if v.Autoscroll && len(v.viewBuffer) > maxY { if v.Autoscroll && len(v.viewLines) > maxY {
v.oy = len(v.viewBuffer) - maxY v.oy = len(v.viewLines) - maxY
} }
y := 0 y := 0
for i, line := range v.viewBuffer { for i, vline := range v.viewLines {
if i < v.oy { if i < v.oy {
continue continue
} }
@ -244,7 +255,7 @@ func (v *View) draw() error {
break break
} }
x := 0 x := 0
for j, ch := range line { for j, ch := range vline.line {
if j < v.ox { if j < v.ox {
continue continue
} }