Skip to content

Commit 164661a

Browse files
authored
Merge pull request #69 from jesseduffield/avoid-blank-line-at-end-of-view
2 parents c80ec09 + 3fa31d2 commit 164661a

File tree

2 files changed

+60
-26
lines changed

2 files changed

+60
-26
lines changed

view.go

+33-6
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ type View struct {
6666
// true and viewLines to nil
6767
viewLines []viewLine
6868

69+
// If the last character written was a newline, we don't write it but
70+
// instead set pendingNewline to true. If more text is written, we write the
71+
// newline then. This is to avoid having an extra blank at the end of the view.
72+
pendingNewline bool
73+
6974
// writeMutex protects locks the write process
7075
writeMutex sync.Mutex
7176

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

648653
v.wx = x
649654
v.wy = y
655+
656+
// Changing the write position makes a pending newline obsolete
657+
v.pendingNewline = false
650658
}
651659

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

768-
for _, r := range p {
776+
advanceToNextLine := func() {
777+
v.wx = 0
778+
v.wy++
779+
if v.wy >= len(v.lines) {
780+
v.lines = append(v.lines, nil)
781+
}
782+
}
783+
784+
if v.pendingNewline {
785+
advanceToNextLine()
786+
v.pendingNewline = false
787+
}
788+
789+
until := len(p)
790+
if until > 0 && p[until-1] == '\n' {
791+
v.pendingNewline = true
792+
until--
793+
}
794+
795+
for _, r := range p[:until] {
769796
switch r {
770797
case '\n':
771798
finishLine()
772-
v.wx = 0
773-
v.wy++
774-
if v.wy >= len(v.lines) {
775-
v.lines = append(v.lines, nil)
776-
}
799+
advanceToNextLine()
777800
case '\r':
778801
finishLine()
779802
v.wx = 0
@@ -792,6 +815,10 @@ func (v *View) writeRunes(p []rune) {
792815
}
793816
}
794817

818+
if v.pendingNewline {
819+
finishLine()
820+
}
821+
795822
v.updateSearchPositions()
796823
}
797824

view_test.go

+27-20
Original file line numberDiff line numberDiff line change
@@ -12,63 +12,68 @@ import (
1212

1313
func TestWriteRunes(t *testing.T) {
1414
tests := []struct {
15-
existingLines []string
16-
stringToWrite string
17-
expectedLines []string
15+
existingLines []string
16+
stringsToWrite []string
17+
expectedLines []string
1818
}{
1919
{
2020
[]string{},
21-
"",
2221
[]string{""},
22+
[]string{""},
23+
},
24+
{
25+
[]string{},
26+
[]string{"1\n"},
27+
[]string{"1\x00"},
2328
},
2429
{
2530
[]string{},
26-
"1\n",
27-
[]string{"1\x00", ""},
31+
[]string{"1\n", "2\n"},
32+
[]string{"1\x00", "2\x00"},
2833
},
2934
{
3035
[]string{"a"},
31-
"1\n",
32-
[]string{"1\x00", ""},
36+
[]string{"1\n"},
37+
[]string{"1\x00"},
3338
},
3439
{
3540
[]string{"a\x00"},
36-
"1\n",
37-
[]string{"1\x00", ""},
41+
[]string{"1\n"},
42+
[]string{"1\x00"},
3843
},
3944
{
4045
[]string{"ab"},
41-
"1\n",
42-
[]string{"1b", ""},
46+
[]string{"1\n"},
47+
[]string{"1b"},
4348
},
4449
{
4550
[]string{"abc"},
46-
"1\n",
47-
[]string{"1bc", ""},
51+
[]string{"1\n"},
52+
[]string{"1bc"},
4853
},
4954
{
5055
[]string{},
51-
"1\r",
56+
[]string{"1\r"},
5257
[]string{"1\x00"},
5358
},
5459
{
5560
[]string{"a"},
56-
"1\r",
61+
[]string{"1\r"},
5762
[]string{"1\x00"},
5863
},
5964
{
6065
[]string{"a\x00"},
61-
"1\r",
66+
[]string{"1\r"},
6267
[]string{"1\x00"},
6368
},
6469
{
6570
[]string{"ab"},
66-
"1\r",
71+
[]string{"1\r"},
6772
[]string{"1b"},
6873
},
6974
{
7075
[]string{"abc"},
71-
"1\r",
76+
[]string{"1\r"},
7277
[]string{"1bc"},
7378
},
7479
}
@@ -78,7 +83,9 @@ func TestWriteRunes(t *testing.T) {
7883
for _, l := range test.existingLines {
7984
v.lines = append(v.lines, stringToCells(l))
8085
}
81-
v.writeRunes([]rune(test.stringToWrite))
86+
for _, s := range test.stringsToWrite {
87+
v.writeRunes([]rune(s))
88+
}
8289
var resultingLines []string
8390
for _, l := range v.lines {
8491
resultingLines = append(resultingLines, cellsToString(l))

0 commit comments

Comments
 (0)