Skip to content

Fix handling of \n wrt inserting null cells #68

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 14 additions & 25 deletions view.go
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,7 @@ func (v *View) makeWriteable(x, y int) {
v.lines = append(v.lines, nil)
}
}
// cell `x` must not be index-able (that's why `<`)
// cell `x` need not be index-able (that's why `<`)
// append should be used by `lines[y]` user if he wants to write beyond `x`
for len(v.lines[y]) < x {
if cap(v.lines[y]) > len(v.lines[y]) {
Expand Down Expand Up @@ -726,14 +726,6 @@ func (v *View) writeCells(x, y int, cells []cell) {
v.lines[y] = line[:newLen]
}

// readCell gets cell at specified location (x, y)
func (v *View) readCell(x, y int) (cell, bool) {
if y < 0 || y >= len(v.lines) || x < 0 || x >= len(v.lines[y]) {
return cell{}, false
}
return v.lines[y][x], true
}

// Write appends a byte slice into the view's internal buffer. Because
// View implements the io.Writer interface, it can be passed as parameter
// of functions like fmt.Fprintf, fmt.Fprintln, io.Copy, etc. Clear must
Expand Down Expand Up @@ -762,31 +754,28 @@ func (v *View) writeRunes(p []rune) {
// Fill with empty cells, if writing outside current view buffer
v.makeWriteable(v.wx, v.wy)

finishLine := func() {
v.autoRenderHyperlinksInCurrentLine()
if v.wx >= len(v.lines[v.wy]) {
v.writeCells(v.wx, v.wy, []cell{{
chr: 0,
fgColor: 0,
bgColor: 0,
}})
}
}

for _, r := range p {
switch r {
case '\n':
v.autoRenderHyperlinksInCurrentLine()
if c, ok := v.readCell(v.wx+1, v.wy); !ok || c.chr == 0 {
v.writeCells(v.wx, v.wy, []cell{{
chr: 0,
fgColor: 0,
bgColor: 0,
}})
}
finishLine()
v.wx = 0
v.wy++
if v.wy >= len(v.lines) {
v.lines = append(v.lines, nil)
}
case '\r':
v.autoRenderHyperlinksInCurrentLine()
if c, ok := v.readCell(v.wx, v.wy); !ok || c.chr == 0 {
v.writeCells(v.wx, v.wy, []cell{{
chr: 0,
fgColor: 0,
bgColor: 0,
}})
}
finishLine()
v.wx = 0
default:
truncateLine, cells := v.parseInput(r, v.wx, v.wy)
Expand Down
77 changes: 77 additions & 0 deletions view_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,83 @@ import (
"github.com/stretchr/testify/assert"
)

func TestWriteRunes(t *testing.T) {
tests := []struct {
existingLines []string
stringToWrite string
expectedLines []string
}{
{
[]string{},
"",
[]string{""},
},
{
[]string{},
"1\n",
[]string{"1\x00", ""},
},
{
[]string{"a"},
"1\n",
[]string{"1\x00", ""},
},
{
[]string{"a\x00"},
"1\n",
[]string{"1\x00", ""},
},
{
[]string{"ab"},
"1\n",
[]string{"1b", ""},
},
{
[]string{"abc"},
"1\n",
[]string{"1bc", ""},
},
{
[]string{},
"1\r",
[]string{"1\x00"},
},
{
[]string{"a"},
"1\r",
[]string{"1\x00"},
},
{
[]string{"a\x00"},
"1\r",
[]string{"1\x00"},
},
{
[]string{"ab"},
"1\r",
[]string{"1b"},
},
{
[]string{"abc"},
"1\r",
[]string{"1bc"},
},
}

for _, test := range tests {
v := NewView("name", 0, 0, 10, 10, OutputNormal)
for _, l := range test.existingLines {
v.lines = append(v.lines, stringToCells(l))
}
v.writeRunes([]rune(test.stringToWrite))
var resultingLines []string
for _, l := range v.lines {
resultingLines = append(resultingLines, cellsToString(l))
}
assert.Equal(t, test.expectedLines, resultingLines)
}
}

func TestUpdatedCursorAndOrigin(t *testing.T) {
tests := []struct {
prevOrigin int
Expand Down
Loading