Fix trailing \x00 bug in edition mode

This commit is contained in:
Roi Martin 2017-08-18 00:19:02 +02:00
parent c64aff6dc2
commit 2677ad0445
2 changed files with 24 additions and 31 deletions

34
edit.go
View File

@ -108,16 +108,9 @@ func (v *View) EditDelete(back bool) {
// EditNewLine inserts a new line under the cursor.
func (v *View) EditNewLine() {
v.breakLine(v.cx, v.cy)
y := v.oy + v.cy
if y >= len(v.viewLines) || (y >= 0 && y < len(v.viewLines) &&
!(v.Wrap && v.cx == 0 && v.viewLines[y].linesX > 0)) {
// new line at the end of the buffer or
// cursor is not at the beginning of a wrapped line
v.ox = 0
v.cx = 0
v.MoveCursor(0, 1, true)
}
v.ox = 0
v.cx = 0
v.MoveCursor(0, 1, true)
}
// MoveCursor moves the cursor taking into account the width of the line/view,
@ -252,23 +245,26 @@ func (v *View) writeRune(x, y int, ch rune) error {
v.lines = append(v.lines, s...)
}
olen := len(v.lines[y])
if x >= len(v.lines[y]) {
s := make([]cell, x-len(v.lines[y])+1)
v.lines[y] = append(v.lines[y], s...)
}
c := cell{
fgColor: v.FgColor,
bgColor: v.BgColor,
chr: ch,
}
if !v.Overwrite || (v.Overwrite && x >= olen-1) {
c.chr = '\x00'
olen := len(v.lines[y])
if x >= len(v.lines[y]) {
s := make([]cell, x-len(v.lines[y])+1)
v.lines[y] = append(v.lines[y], s...)
} else if !v.Overwrite {
v.lines[y] = append(v.lines[y], c)
}
if !v.Overwrite || (v.Overwrite && x >= olen-1) {
copy(v.lines[y][x+1:], v.lines[y][x:])
}
c.chr = ch
v.lines[y][x] = c
return nil
}

21
view.go
View File

@ -298,22 +298,19 @@ func (v *View) draw() error {
v.viewLines = nil
for i, line := range v.lines {
if v.Wrap {
if len(line) <= maxX {
if len(line) < maxX {
vline := viewLine{linesX: 0, linesY: i, line: line}
v.viewLines = append(v.viewLines, vline)
continue
} else {
vline := viewLine{linesX: 0, linesY: i, line: line[:maxX]}
v.viewLines = append(v.viewLines, vline)
}
// Append remaining lines
for n := maxX; n < len(line); n += maxX {
if len(line[n:]) <= maxX {
vline := viewLine{linesX: n, linesY: i, line: line[n:]}
v.viewLines = append(v.viewLines, vline)
} else {
vline := viewLine{linesX: n, linesY: i, line: line[n : n+maxX]}
v.viewLines = append(v.viewLines, vline)
for n := 0; n <= len(line); n += maxX {
if len(line[n:]) <= maxX {
vline := viewLine{linesX: n, linesY: i, line: line[n:]}
v.viewLines = append(v.viewLines, vline)
} else {
vline := viewLine{linesX: n, linesY: i, line: line[n : n+maxX]}
v.viewLines = append(v.viewLines, vline)
}
}
}
} else {