File tree 2 files changed +51
-21
lines changed
2 files changed +51
-21
lines changed Original file line number Diff line number Diff line change @@ -66,6 +66,11 @@ type View struct {
66
66
// true and viewLines to nil
67
67
viewLines []viewLine
68
68
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
+
69
74
// writeMutex protects locks the write process
70
75
writeMutex sync.Mutex
71
76
@@ -647,6 +652,9 @@ func (v *View) SetWritePos(x, y int) {
647
652
648
653
v .wx = x
649
654
v .wy = y
655
+
656
+ // Changing the write position makes a pending newline obsolete
657
+ v .pendingNewline = false
650
658
}
651
659
652
660
// WritePos returns the current write position of the view's internal buffer.
@@ -773,7 +781,18 @@ func (v *View) writeRunes(p []rune) {
773
781
}
774
782
}
775
783
776
- for _ , r := range p {
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 ] {
777
796
switch r {
778
797
case '\n' :
779
798
finishLine ()
@@ -796,6 +815,10 @@ func (v *View) writeRunes(p []rune) {
796
815
}
797
816
}
798
817
818
+ if v .pendingNewline {
819
+ finishLine ()
820
+ }
821
+
799
822
v .updateSearchPositions ()
800
823
}
801
824
Original file line number Diff line number Diff line change @@ -12,63 +12,68 @@ import (
12
12
13
13
func TestWriteRunes (t * testing.T ) {
14
14
tests := []struct {
15
- existingLines []string
16
- stringToWrite string
17
- expectedLines []string
15
+ existingLines []string
16
+ stringsToWrite [] string
17
+ expectedLines []string
18
18
}{
19
19
{
20
20
[]string {},
21
- "" ,
22
21
[]string {"" },
22
+ []string {"" },
23
+ },
24
+ {
25
+ []string {},
26
+ []string {"1\n " },
27
+ []string {"1\x00 " },
23
28
},
24
29
{
25
30
[]string {},
26
- "1\n " ,
27
- []string {"1\x00 " , "" },
31
+ [] string { "1\n " , "2 \n " } ,
32
+ []string {"1\x00 " , "2 \x00 " },
28
33
},
29
34
{
30
35
[]string {"a" },
31
- "1\n " ,
32
- []string {"1\x00 " , "" },
36
+ [] string { "1\n " } ,
37
+ []string {"1\x00 " },
33
38
},
34
39
{
35
40
[]string {"a\x00 " },
36
- "1\n " ,
37
- []string {"1\x00 " , "" },
41
+ [] string { "1\n " } ,
42
+ []string {"1\x00 " },
38
43
},
39
44
{
40
45
[]string {"ab" },
41
- "1\n " ,
42
- []string {"1b" , "" },
46
+ [] string { "1\n " } ,
47
+ []string {"1b" },
43
48
},
44
49
{
45
50
[]string {"abc" },
46
- "1\n " ,
47
- []string {"1bc" , "" },
51
+ [] string { "1\n " } ,
52
+ []string {"1bc" },
48
53
},
49
54
{
50
55
[]string {},
51
- "1\r " ,
56
+ [] string { "1\r " } ,
52
57
[]string {"1\x00 " },
53
58
},
54
59
{
55
60
[]string {"a" },
56
- "1\r " ,
61
+ [] string { "1\r " } ,
57
62
[]string {"1\x00 " },
58
63
},
59
64
{
60
65
[]string {"a\x00 " },
61
- "1\r " ,
66
+ [] string { "1\r " } ,
62
67
[]string {"1\x00 " },
63
68
},
64
69
{
65
70
[]string {"ab" },
66
- "1\r " ,
71
+ [] string { "1\r " } ,
67
72
[]string {"1b" },
68
73
},
69
74
{
70
75
[]string {"abc" },
71
- "1\r " ,
76
+ [] string { "1\r " } ,
72
77
[]string {"1bc" },
73
78
},
74
79
}
@@ -78,7 +83,9 @@ func TestWriteRunes(t *testing.T) {
78
83
for _ , l := range test .existingLines {
79
84
v .lines = append (v .lines , stringToCells (l ))
80
85
}
81
- v .writeRunes ([]rune (test .stringToWrite ))
86
+ for _ , s := range test .stringsToWrite {
87
+ v .writeRunes ([]rune (s ))
88
+ }
82
89
var resultingLines []string
83
90
for _ , l := range v .lines {
84
91
resultingLines = append (resultingLines , cellsToString (l ))
You can’t perform that action at this time.
0 commit comments