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

28
edit.go
View File

@ -108,17 +108,10 @@ func (v *View) EditDelete(back bool) {
// EditNewLine inserts a new line under the cursor. // EditNewLine inserts a new line under the cursor.
func (v *View) EditNewLine() { func (v *View) EditNewLine() {
v.breakLine(v.cx, v.cy) 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.ox = 0
v.cx = 0 v.cx = 0
v.MoveCursor(0, 1, true) v.MoveCursor(0, 1, true)
} }
}
// MoveCursor moves the cursor taking into account the width of the line/view, // MoveCursor moves the cursor taking into account the width of the line/view,
// displacing the origin if necessary. // displacing the origin if necessary.
@ -252,23 +245,26 @@ func (v *View) writeRune(x, y int, ch rune) error {
v.lines = append(v.lines, s...) 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{ c := cell{
fgColor: v.FgColor, fgColor: v.FgColor,
bgColor: v.BgColor, 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) 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:]) copy(v.lines[y][x+1:], v.lines[y][x:])
} }
c.chr = ch
v.lines[y][x] = c v.lines[y][x] = c
return nil return nil
} }

View File

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