Skip to content

Avoid blank line at end of view #69

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 2 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: 33 additions & 6 deletions view.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ type View struct {
// true and viewLines to nil
viewLines []viewLine

// If the last character written was a newline, we don't write it but
// instead set pendingNewline to true. If more text is written, we write the
// newline then. This is to avoid having an extra blank at the end of the view.
pendingNewline bool

// writeMutex protects locks the write process
writeMutex sync.Mutex

Expand Down Expand Up @@ -647,6 +652,9 @@ func (v *View) SetWritePos(x, y int) {

v.wx = x
v.wy = y

// Changing the write position makes a pending newline obsolete
v.pendingNewline = false
}

// WritePos returns the current write position of the view's internal buffer.
Expand Down Expand Up @@ -765,15 +773,30 @@ func (v *View) writeRunes(p []rune) {
}
}

for _, r := range p {
advanceToNextLine := func() {
v.wx = 0
v.wy++
if v.wy >= len(v.lines) {
v.lines = append(v.lines, nil)
}
}

if v.pendingNewline {
advanceToNextLine()
v.pendingNewline = false
}

until := len(p)
if until > 0 && p[until-1] == '\n' {
v.pendingNewline = true
until--
}

for _, r := range p[:until] {
switch r {
case '\n':
finishLine()
v.wx = 0
v.wy++
if v.wy >= len(v.lines) {
v.lines = append(v.lines, nil)
}
advanceToNextLine()
case '\r':
finishLine()
v.wx = 0
Expand All @@ -792,6 +815,10 @@ func (v *View) writeRunes(p []rune) {
}
}

if v.pendingNewline {
finishLine()
}

v.updateSearchPositions()
}

Expand Down
47 changes: 27 additions & 20 deletions view_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,63 +12,68 @@ import (

func TestWriteRunes(t *testing.T) {
tests := []struct {
existingLines []string
stringToWrite string
expectedLines []string
existingLines []string
stringsToWrite []string
expectedLines []string
}{
{
[]string{},
"",
[]string{""},
[]string{""},
},
{
[]string{},
[]string{"1\n"},
[]string{"1\x00"},
},
{
[]string{},
"1\n",
[]string{"1\x00", ""},
[]string{"1\n", "2\n"},
[]string{"1\x00", "2\x00"},
},
{
[]string{"a"},
"1\n",
[]string{"1\x00", ""},
[]string{"1\n"},
[]string{"1\x00"},
},
{
[]string{"a\x00"},
"1\n",
[]string{"1\x00", ""},
[]string{"1\n"},
[]string{"1\x00"},
},
{
[]string{"ab"},
"1\n",
[]string{"1b", ""},
[]string{"1\n"},
[]string{"1b"},
},
{
[]string{"abc"},
"1\n",
[]string{"1bc", ""},
[]string{"1\n"},
[]string{"1bc"},
},
{
[]string{},
"1\r",
[]string{"1\r"},
[]string{"1\x00"},
},
{
[]string{"a"},
"1\r",
[]string{"1\r"},
[]string{"1\x00"},
},
{
[]string{"a\x00"},
"1\r",
[]string{"1\r"},
[]string{"1\x00"},
},
{
[]string{"ab"},
"1\r",
[]string{"1\r"},
[]string{"1b"},
},
{
[]string{"abc"},
"1\r",
[]string{"1\r"},
[]string{"1bc"},
},
}
Expand All @@ -78,7 +83,9 @@ func TestWriteRunes(t *testing.T) {
for _, l := range test.existingLines {
v.lines = append(v.lines, stringToCells(l))
}
v.writeRunes([]rune(test.stringToWrite))
for _, s := range test.stringsToWrite {
v.writeRunes([]rune(s))
}
var resultingLines []string
for _, l := range v.lines {
resultingLines = append(resultingLines, cellsToString(l))
Expand Down
Loading