Skip to content
This repository was archived by the owner on Apr 19, 2024. It is now read-only.

Commit c5bc9bf

Browse files
correct cursor location when terminal wraps lines (#360)
Co-authored-by: Evan <cordell.evan@gmail.com>
1 parent 64b4e8e commit c5bc9bf

File tree

1 file changed

+29
-14
lines changed

1 file changed

+29
-14
lines changed

terminal/runereader.go

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,29 @@ func (rr *RuneReader) ReadLine(mask rune) ([]rune, error) {
4646
// we set the current location of the cursor once
4747
cursorCurrent, _ := cursor.Location(rr.Buffer())
4848

49+
increment := func() {
50+
if cursorCurrent.CursorIsAtLineEnd(terminalSize) {
51+
cursorCurrent.X = COORDINATE_SYSTEM_BEGIN
52+
cursorCurrent.Y++
53+
} else {
54+
cursorCurrent.X++
55+
}
56+
}
57+
decrement := func() {
58+
if cursorCurrent.CursorIsAtLineBegin() {
59+
cursorCurrent.X = terminalSize.X
60+
cursorCurrent.Y--
61+
} else {
62+
cursorCurrent.X--
63+
}
64+
}
65+
4966
for {
5067
// wait for some input
5168
r, _, err := rr.ReadRune()
5269
if err != nil {
5370
return line, err
5471
}
55-
// increment cursor location
56-
cursorCurrent.X++
5772

5873
// if the user pressed enter or some other newline/termination like ctrl+d
5974
if r == '\r' || r == '\n' || r == KeyEndTransmission {
@@ -63,13 +78,10 @@ func (rr *RuneReader) ReadLine(mask rune) ([]rune, error) {
6378
EraseLine(rr.stdio.Out, ERASE_LINE_END)
6479
cursor.PreviousLine(1)
6580
cursor.Forward(int(terminalSize.X))
66-
cursorCurrent.X = terminalSize.X
67-
cursorCurrent.Y--
68-
6981
} else {
7082
cursor.Back(1)
71-
cursorCurrent.X--
7283
}
84+
decrement()
7385
index--
7486
}
7587
// move the cursor the a new line
@@ -148,6 +160,7 @@ func (rr *RuneReader) ReadLine(mask rune) ([]rune, error) {
148160

149161
// decrement the index
150162
index--
163+
decrement()
151164
} else {
152165
// otherwise the user pressed backspace while at the beginning of the line
153166
soundBell(rr.stdio.Out)
@@ -170,6 +183,7 @@ func (rr *RuneReader) ReadLine(mask rune) ([]rune, error) {
170183
}
171184
//decrement the index
172185
index--
186+
decrement()
173187

174188
} else {
175189
// otherwise we are at the beginning of where we started reading lines
@@ -192,6 +206,7 @@ func (rr *RuneReader) ReadLine(mask rune) ([]rune, error) {
192206
cursor.Forward(runeWidth(line[index]))
193207
}
194208
index++
209+
increment()
195210

196211
} else {
197212
// otherwise we are at the end of the word and can't go past
@@ -208,12 +223,11 @@ func (rr *RuneReader) ReadLine(mask rune) ([]rune, error) {
208223
if cursorCurrent.CursorIsAtLineBegin() {
209224
cursor.PreviousLine(1)
210225
cursor.Forward(int(terminalSize.X))
211-
cursorCurrent.X = terminalSize.X
212226
cursorCurrent.Y--
213-
227+
cursorCurrent.X = terminalSize.X
214228
} else {
215229
cursor.Back(runeWidth(line[index-1]))
216-
cursorCurrent.X--
230+
cursorCurrent.X -= Short(runeWidth(line[index-1]))
217231
}
218232
index--
219233
}
@@ -223,12 +237,11 @@ func (rr *RuneReader) ReadLine(mask rune) ([]rune, error) {
223237
for index != len(line) {
224238
if cursorCurrent.CursorIsAtLineEnd(terminalSize) {
225239
cursor.NextLine(1)
226-
cursorCurrent.X = COORDINATE_SYSTEM_BEGIN
227240
cursorCurrent.Y++
228-
241+
cursorCurrent.X = COORDINATE_SYSTEM_BEGIN
229242
} else {
230243
cursor.Forward(runeWidth(line[index]))
231-
cursorCurrent.X++
244+
cursorCurrent.X += Short(runeWidth(line[index]))
232245
}
233246
index++
234247
}
@@ -277,6 +290,7 @@ func (rr *RuneReader) ReadLine(mask rune) ([]rune, error) {
277290
line = append(line, r)
278291
// save the location of the cursor
279292
index++
293+
increment()
280294
// print out the character
281295
rr.printChar(r, mask)
282296
} else {
@@ -293,7 +307,7 @@ func (rr *RuneReader) ReadLine(mask rune) ([]rune, error) {
293307
EraseLine(rr.stdio.Out, ERASE_LINE_END)
294308
// print out the character
295309
rr.printChar(char, mask)
296-
cursorCurrent.X++
310+
increment()
297311
}
298312
// if we are at the last line, we want to visually insert a new line and append to it.
299313
if cursorCurrent.CursorIsAtLineEnd(terminalSize) && cursorCurrent.Y == terminalSize.Y {
@@ -316,6 +330,7 @@ func (rr *RuneReader) ReadLine(mask rune) ([]rune, error) {
316330
}
317331
// increment the index
318332
index++
333+
increment()
319334

320335
}
321336
}
@@ -327,4 +342,4 @@ func runeWidth(r rune) int {
327342
return 2
328343
}
329344
return 1
330-
}
345+
}

0 commit comments

Comments
 (0)