From 4664d8bf22d933ccc9141a01d665b03d29d27652 Mon Sep 17 00:00:00 2001 From: Oliver <480930+rivo@users.noreply.github.com> Date: Tue, 6 Sep 2022 21:45:28 +0200 Subject: [PATCH] Only moving the cursor up or down in the text area will preserve the desired column position. --- textarea.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/textarea.go b/textarea.go index aaf9077..816fa53 100644 --- a/textarea.go +++ b/textarea.go @@ -1220,7 +1220,13 @@ func (t *TextArea) truncateLines(fromLine int) { // indicating the lowest row in which searching should start. Set this to 0 if // you don't have any information where the cursor might be (but know that this // is expensive for long texts). +// +// The cursor's desired column will be set to its actual column. func (t *TextArea) findCursor(clamp bool, startRow int) { + defer func() { + t.cursor.column = t.cursor.actualColumn + }() + if !clamp && t.cursor.row >= 0 { return // Nothing to do. } @@ -1783,7 +1789,9 @@ func (t *TextArea) InputHandler() func(event *tcell.EventKey, setFocus func(p Pr case tcell.KeyDown: // Move one row down. if event.Modifiers()&tcell.ModAlt == 0 { // Regular movement. + column := t.cursor.column t.moveCursor(t.cursor.row+1, t.cursor.column) + t.cursor.column = column if event.Modifiers()&tcell.ModShift == 0 { t.selectionStart = t.cursor } @@ -1803,7 +1811,9 @@ func (t *TextArea) InputHandler() func(event *tcell.EventKey, setFocus func(p Pr case tcell.KeyUp: // Move one row up. if event.Modifiers()&tcell.ModAlt == 0 { // Regular movement. + column := t.cursor.column t.moveCursor(t.cursor.row-1, t.cursor.column) + t.cursor.column = column if event.Modifiers()&tcell.ModShift == 0 { t.selectionStart = t.cursor } @@ -1825,12 +1835,16 @@ func (t *TextArea) InputHandler() func(event *tcell.EventKey, setFocus func(p Pr t.selectionStart = t.cursor } case tcell.KeyPgDn, tcell.KeyCtrlF: // Move one page down. + column := t.cursor.column t.moveCursor(t.cursor.row+t.lastHeight, t.cursor.column) + t.cursor.column = column if event.Modifiers()&tcell.ModShift == 0 { t.selectionStart = t.cursor } case tcell.KeyPgUp, tcell.KeyCtrlB: // Move one page up. + column := t.cursor.column t.moveCursor(t.cursor.row-t.lastHeight, t.cursor.column) + t.cursor.column = column if event.Modifiers()&tcell.ModShift == 0 { t.selectionStart = t.cursor }