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

Commit

Permalink
correct cursor location when terminal wraps lines (#360)
Browse files Browse the repository at this point in the history
Co-authored-by: Evan <[email protected]>
  • Loading branch information
AlecAivazis and ecordell authored Jun 28, 2021
1 parent 64b4e8e commit c5bc9bf
Showing 1 changed file with 29 additions and 14 deletions.
43 changes: 29 additions & 14 deletions terminal/runereader.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,29 @@ func (rr *RuneReader) ReadLine(mask rune) ([]rune, error) {
// we set the current location of the cursor once
cursorCurrent, _ := cursor.Location(rr.Buffer())

increment := func() {
if cursorCurrent.CursorIsAtLineEnd(terminalSize) {
cursorCurrent.X = COORDINATE_SYSTEM_BEGIN
cursorCurrent.Y++
} else {
cursorCurrent.X++
}
}
decrement := func() {
if cursorCurrent.CursorIsAtLineBegin() {
cursorCurrent.X = terminalSize.X
cursorCurrent.Y--
} else {
cursorCurrent.X--
}
}

for {
// wait for some input
r, _, err := rr.ReadRune()
if err != nil {
return line, err
}
// increment cursor location
cursorCurrent.X++

// if the user pressed enter or some other newline/termination like ctrl+d
if r == '\r' || r == '\n' || r == KeyEndTransmission {
Expand All @@ -63,13 +78,10 @@ func (rr *RuneReader) ReadLine(mask rune) ([]rune, error) {
EraseLine(rr.stdio.Out, ERASE_LINE_END)
cursor.PreviousLine(1)
cursor.Forward(int(terminalSize.X))
cursorCurrent.X = terminalSize.X
cursorCurrent.Y--

} else {
cursor.Back(1)
cursorCurrent.X--
}
decrement()
index--
}
// move the cursor the a new line
Expand Down Expand Up @@ -148,6 +160,7 @@ func (rr *RuneReader) ReadLine(mask rune) ([]rune, error) {

// decrement the index
index--
decrement()
} else {
// otherwise the user pressed backspace while at the beginning of the line
soundBell(rr.stdio.Out)
Expand All @@ -170,6 +183,7 @@ func (rr *RuneReader) ReadLine(mask rune) ([]rune, error) {
}
//decrement the index
index--
decrement()

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

} else {
// otherwise we are at the end of the word and can't go past
Expand All @@ -208,12 +223,11 @@ func (rr *RuneReader) ReadLine(mask rune) ([]rune, error) {
if cursorCurrent.CursorIsAtLineBegin() {
cursor.PreviousLine(1)
cursor.Forward(int(terminalSize.X))
cursorCurrent.X = terminalSize.X
cursorCurrent.Y--

cursorCurrent.X = terminalSize.X
} else {
cursor.Back(runeWidth(line[index-1]))
cursorCurrent.X--
cursorCurrent.X -= Short(runeWidth(line[index-1]))
}
index--
}
Expand All @@ -223,12 +237,11 @@ func (rr *RuneReader) ReadLine(mask rune) ([]rune, error) {
for index != len(line) {
if cursorCurrent.CursorIsAtLineEnd(terminalSize) {
cursor.NextLine(1)
cursorCurrent.X = COORDINATE_SYSTEM_BEGIN
cursorCurrent.Y++

cursorCurrent.X = COORDINATE_SYSTEM_BEGIN
} else {
cursor.Forward(runeWidth(line[index]))
cursorCurrent.X++
cursorCurrent.X += Short(runeWidth(line[index]))
}
index++
}
Expand Down Expand Up @@ -277,6 +290,7 @@ func (rr *RuneReader) ReadLine(mask rune) ([]rune, error) {
line = append(line, r)
// save the location of the cursor
index++
increment()
// print out the character
rr.printChar(r, mask)
} else {
Expand All @@ -293,7 +307,7 @@ func (rr *RuneReader) ReadLine(mask rune) ([]rune, error) {
EraseLine(rr.stdio.Out, ERASE_LINE_END)
// print out the character
rr.printChar(char, mask)
cursorCurrent.X++
increment()
}
// if we are at the last line, we want to visually insert a new line and append to it.
if cursorCurrent.CursorIsAtLineEnd(terminalSize) && cursorCurrent.Y == terminalSize.Y {
Expand All @@ -316,6 +330,7 @@ func (rr *RuneReader) ReadLine(mask rune) ([]rune, error) {
}
// increment the index
index++
increment()

}
}
Expand All @@ -327,4 +342,4 @@ func runeWidth(r rune) int {
return 2
}
return 1
}
}

0 comments on commit c5bc9bf

Please sign in to comment.