From 77656dc44a0187cd410cd6205c7aabf3be9b1ebd Mon Sep 17 00:00:00 2001 From: mjarkk Date: Thu, 25 Mar 2021 19:20:16 +0100 Subject: [PATCH 1/4] Less strict rules for SetCursor --- view.go | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/view.go b/view.go index 73d8fa7..4774040 100644 --- a/view.go +++ b/view.go @@ -232,13 +232,27 @@ func (v *View) setRune(x, y int, ch rune, fgColor, bgColor Attribute) error { // SetCursor sets the cursor position of the view at the given point, // // Rules: -// y < total lines && y > 0 -// (x < view width || x < y's line width) && x > 0 +// y >= 0 +// x >= 0 +// +// If the x or y are outside of the buffer this function will place the cursor on the nearest buffer location func (v *View) SetCursor(x, y int) error { - if x < 0 || y < 0 || (y >= len(v.lines) && y != 0) || (x > 0 && (len(v.lines) == 0 || len(v.lines[y]) < x)) { + if x < 0 || y < 0 { return ErrInvalidPoint } + if y >= len(v.lines) && y != 0 { + y = len(v.lines) - 1 + } + + if x > 0 && (len(v.lines) == 0 || len(v.lines[y]) < x) { + if len(v.lines) == 0 { + x = 0 + } else { + x = len(v.lines[y]) + } + } + v.cx = x v.cy = y return nil From 1738c85c035724eda58e3569f396b8d13e9cbc22 Mon Sep 17 00:00:00 2001 From: mjarkk Date: Thu, 25 Mar 2021 19:28:50 +0100 Subject: [PATCH 2/4] Add SetCursorUnrestricted --- view.go | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/view.go b/view.go index 4774040..abff610 100644 --- a/view.go +++ b/view.go @@ -229,18 +229,29 @@ func (v *View) setRune(x, y int, ch rune, fgColor, bgColor Attribute) error { return nil } -// SetCursor sets the cursor position of the view at the given point, +// SetCursorUnsafe sets the cursor position of the view at the given point +// This does NOT check if the x and y location are available in the buffer // // Rules: // y >= 0 // x >= 0 -// -// If the x or y are outside of the buffer this function will place the cursor on the nearest buffer location -func (v *View) SetCursor(x, y int) error { +func (v *View) SetCursorUnrestricted(x, y int) error { if x < 0 || y < 0 { return ErrInvalidPoint } + v.cx = x + v.cy = y + return nil +} + +// SetCursor tries sets the cursor position of the view at the given point +// If the x or y are outside of the buffer this function will place the cursor on the nearest buffer location +// +// Rules: +// y >= 0 +// x >= 0 +func (v *View) SetCursor(x, y int) error { if y >= len(v.lines) && y != 0 { y = len(v.lines) - 1 } @@ -253,9 +264,7 @@ func (v *View) SetCursor(x, y int) error { } } - v.cx = x - v.cy = y - return nil + return v.SetCursorUnrestricted(x, y) } // Cursor returns the cursor position of the view. From a1a650e7ff8b9bd10479c73a707d43ff80a613de Mon Sep 17 00:00:00 2001 From: mjarkk Date: Sun, 28 Mar 2021 17:47:54 +0200 Subject: [PATCH 3/4] Fix comment describing the wrong function name --- view.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/view.go b/view.go index abff610..258d6c9 100644 --- a/view.go +++ b/view.go @@ -229,7 +229,7 @@ func (v *View) setRune(x, y int, ch rune, fgColor, bgColor Attribute) error { return nil } -// SetCursorUnsafe sets the cursor position of the view at the given point +// SetCursorUnrestricted sets the cursor position of the view at the given point // This does NOT check if the x and y location are available in the buffer // // Rules: From 771d3983986840b5b9d840df74c6c7eab5617b05 Mon Sep 17 00:00:00 2001 From: mjarkk Date: Sun, 28 Mar 2021 17:52:01 +0200 Subject: [PATCH 4/4] Remove moveCursor from EditNewLine --- edit.go | 1 - 1 file changed, 1 deletion(-) diff --git a/edit.go b/edit.go index 85dd23c..7891dda 100644 --- a/edit.go +++ b/edit.go @@ -154,7 +154,6 @@ func (v *View) EditNewLine() { v.ox = 0 v.cy = v.cy + 1 v.cx = 0 - v.moveCursor(0, 1) } // MoveCursor mores the cursor relative from it's current possition