mirror of
https://github.com/jroimartin/gocui.git
synced 2025-04-24 13:48:51 +08:00
Do not move the cursor beyond EOF
This commit is contained in:
parent
a4d0b30476
commit
75a7ad2750
32
edit.go
32
edit.go
@ -148,11 +148,13 @@ func (v *View) MoveCursor(dx, dy int, writeMode bool) {
|
|||||||
// adjust cursor's x position and view's x origin
|
// adjust cursor's x position and view's x origin
|
||||||
if x > curLineWidth { // move to next line
|
if x > curLineWidth { // move to next line
|
||||||
if dx > 0 { // horizontal movement
|
if dx > 0 { // horizontal movement
|
||||||
if !v.Wrap {
|
|
||||||
v.ox = 0
|
|
||||||
}
|
|
||||||
v.cx = 0
|
|
||||||
cy++
|
cy++
|
||||||
|
if writeMode || v.oy+cy < len(v.viewLines) {
|
||||||
|
if !v.Wrap {
|
||||||
|
v.ox = 0
|
||||||
|
}
|
||||||
|
v.cx = 0
|
||||||
|
}
|
||||||
} else { // vertical movement
|
} else { // vertical movement
|
||||||
if curLineWidth > 0 { // move cursor to the EOL
|
if curLineWidth > 0 { // move cursor to the EOL
|
||||||
if v.Wrap {
|
if v.Wrap {
|
||||||
@ -170,10 +172,12 @@ func (v *View) MoveCursor(dx, dy int, writeMode bool) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if !v.Wrap {
|
if writeMode || v.oy+cy < len(v.viewLines) {
|
||||||
v.ox = 0
|
if !v.Wrap {
|
||||||
|
v.ox = 0
|
||||||
|
}
|
||||||
|
v.cx = 0
|
||||||
}
|
}
|
||||||
v.cx = 0
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if cx < 0 {
|
} else if cx < 0 {
|
||||||
@ -181,6 +185,7 @@ func (v *View) MoveCursor(dx, dy int, writeMode bool) {
|
|||||||
v.ox += cx
|
v.ox += cx
|
||||||
v.cx = 0
|
v.cx = 0
|
||||||
} else { // move to previous line
|
} else { // move to previous line
|
||||||
|
cy--
|
||||||
if prevLineWidth > 0 {
|
if prevLineWidth > 0 {
|
||||||
if !v.Wrap { // set origin so the EOL is visible
|
if !v.Wrap { // set origin so the EOL is visible
|
||||||
nox := prevLineWidth - maxX + 1
|
nox := prevLineWidth - maxX + 1
|
||||||
@ -197,7 +202,6 @@ func (v *View) MoveCursor(dx, dy int, writeMode bool) {
|
|||||||
}
|
}
|
||||||
v.cx = 0
|
v.cx = 0
|
||||||
}
|
}
|
||||||
cy--
|
|
||||||
}
|
}
|
||||||
} else { // stay on the same line
|
} else { // stay on the same line
|
||||||
if v.Wrap {
|
if v.Wrap {
|
||||||
@ -213,14 +217,16 @@ func (v *View) MoveCursor(dx, dy int, writeMode bool) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// adjust cursor's y position and view's y origin
|
// adjust cursor's y position and view's y origin
|
||||||
if cy >= maxY {
|
if cy < 0 {
|
||||||
v.oy++
|
|
||||||
} else if cy < 0 {
|
|
||||||
if v.oy > 0 {
|
if v.oy > 0 {
|
||||||
v.oy--
|
v.oy--
|
||||||
}
|
}
|
||||||
} else {
|
} else if writeMode || v.oy+cy < len(v.viewLines) {
|
||||||
v.cy = cy
|
if cy >= maxY {
|
||||||
|
v.oy++
|
||||||
|
} else {
|
||||||
|
v.cy = cy
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user