Skip to content

Commit

Permalink
Fix trailing \x00 bug in edition mode
Browse files Browse the repository at this point in the history
  • Loading branch information
jroimartin committed Aug 17, 2017
1 parent c64aff6 commit 2677ad0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 29 deletions.
30 changes: 13 additions & 17 deletions edit.go
Expand Up @@ -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,
Expand Down Expand Up @@ -252,23 +245,26 @@ func (v *View) writeRune(x, y int, ch rune) error {
v.lines = append(v.lines, s...)
}

c := cell{
fgColor: v.FgColor,
bgColor: v.BgColor,
chr: ch,
}

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)
}

c := cell{
fgColor: v.FgColor,
bgColor: v.BgColor,
}
if !v.Overwrite || (v.Overwrite && x >= olen-1) {
c.chr = '\x00'
v.lines[y] = append(v.lines[y], c)
copy(v.lines[y][x+1:], v.lines[y][x:])
}
c.chr = ch
v.lines[y][x] = c

return nil
}

Expand Down
21 changes: 9 additions & 12 deletions view.go
Expand Up @@ -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 {
Expand Down

0 comments on commit 2677ad0

Please sign in to comment.