Skip to content

Commit c80ec09

Browse files
authored
Merge pull request #68 from jesseduffield/fix-eol-handling
2 parents 9967d0e + deb0e10 commit c80ec09

File tree

2 files changed

+91
-25
lines changed

2 files changed

+91
-25
lines changed

view.go

+14-25
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,7 @@ func (v *View) makeWriteable(x, y int) {
690690
v.lines = append(v.lines, nil)
691691
}
692692
}
693-
// cell `x` must not be index-able (that's why `<`)
693+
// cell `x` need not be index-able (that's why `<`)
694694
// append should be used by `lines[y]` user if he wants to write beyond `x`
695695
for len(v.lines[y]) < x {
696696
if cap(v.lines[y]) > len(v.lines[y]) {
@@ -726,14 +726,6 @@ func (v *View) writeCells(x, y int, cells []cell) {
726726
v.lines[y] = line[:newLen]
727727
}
728728

729-
// readCell gets cell at specified location (x, y)
730-
func (v *View) readCell(x, y int) (cell, bool) {
731-
if y < 0 || y >= len(v.lines) || x < 0 || x >= len(v.lines[y]) {
732-
return cell{}, false
733-
}
734-
return v.lines[y][x], true
735-
}
736-
737729
// Write appends a byte slice into the view's internal buffer. Because
738730
// View implements the io.Writer interface, it can be passed as parameter
739731
// of functions like fmt.Fprintf, fmt.Fprintln, io.Copy, etc. Clear must
@@ -762,31 +754,28 @@ func (v *View) writeRunes(p []rune) {
762754
// Fill with empty cells, if writing outside current view buffer
763755
v.makeWriteable(v.wx, v.wy)
764756

757+
finishLine := func() {
758+
v.autoRenderHyperlinksInCurrentLine()
759+
if v.wx >= len(v.lines[v.wy]) {
760+
v.writeCells(v.wx, v.wy, []cell{{
761+
chr: 0,
762+
fgColor: 0,
763+
bgColor: 0,
764+
}})
765+
}
766+
}
767+
765768
for _, r := range p {
766769
switch r {
767770
case '\n':
768-
v.autoRenderHyperlinksInCurrentLine()
769-
if c, ok := v.readCell(v.wx+1, v.wy); !ok || c.chr == 0 {
770-
v.writeCells(v.wx, v.wy, []cell{{
771-
chr: 0,
772-
fgColor: 0,
773-
bgColor: 0,
774-
}})
775-
}
771+
finishLine()
776772
v.wx = 0
777773
v.wy++
778774
if v.wy >= len(v.lines) {
779775
v.lines = append(v.lines, nil)
780776
}
781777
case '\r':
782-
v.autoRenderHyperlinksInCurrentLine()
783-
if c, ok := v.readCell(v.wx, v.wy); !ok || c.chr == 0 {
784-
v.writeCells(v.wx, v.wy, []cell{{
785-
chr: 0,
786-
fgColor: 0,
787-
bgColor: 0,
788-
}})
789-
}
778+
finishLine()
790779
v.wx = 0
791780
default:
792781
truncateLine, cells := v.parseInput(r, v.wx, v.wy)

view_test.go

+77
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,83 @@ import (
1010
"github.com/stretchr/testify/assert"
1111
)
1212

13+
func TestWriteRunes(t *testing.T) {
14+
tests := []struct {
15+
existingLines []string
16+
stringToWrite string
17+
expectedLines []string
18+
}{
19+
{
20+
[]string{},
21+
"",
22+
[]string{""},
23+
},
24+
{
25+
[]string{},
26+
"1\n",
27+
[]string{"1\x00", ""},
28+
},
29+
{
30+
[]string{"a"},
31+
"1\n",
32+
[]string{"1\x00", ""},
33+
},
34+
{
35+
[]string{"a\x00"},
36+
"1\n",
37+
[]string{"1\x00", ""},
38+
},
39+
{
40+
[]string{"ab"},
41+
"1\n",
42+
[]string{"1b", ""},
43+
},
44+
{
45+
[]string{"abc"},
46+
"1\n",
47+
[]string{"1bc", ""},
48+
},
49+
{
50+
[]string{},
51+
"1\r",
52+
[]string{"1\x00"},
53+
},
54+
{
55+
[]string{"a"},
56+
"1\r",
57+
[]string{"1\x00"},
58+
},
59+
{
60+
[]string{"a\x00"},
61+
"1\r",
62+
[]string{"1\x00"},
63+
},
64+
{
65+
[]string{"ab"},
66+
"1\r",
67+
[]string{"1b"},
68+
},
69+
{
70+
[]string{"abc"},
71+
"1\r",
72+
[]string{"1bc"},
73+
},
74+
}
75+
76+
for _, test := range tests {
77+
v := NewView("name", 0, 0, 10, 10, OutputNormal)
78+
for _, l := range test.existingLines {
79+
v.lines = append(v.lines, stringToCells(l))
80+
}
81+
v.writeRunes([]rune(test.stringToWrite))
82+
var resultingLines []string
83+
for _, l := range v.lines {
84+
resultingLines = append(resultingLines, cellsToString(l))
85+
}
86+
assert.Equal(t, test.expectedLines, resultingLines)
87+
}
88+
}
89+
1390
func TestUpdatedCursorAndOrigin(t *testing.T) {
1491
tests := []struct {
1592
prevOrigin int

0 commit comments

Comments
 (0)