From 57a01e62e37b16cce8c8242436cecf5f73e65470 Mon Sep 17 00:00:00 2001 From: Maas Lalani Date: Tue, 9 Aug 2022 10:58:41 -0400 Subject: [PATCH] fix: cursor updating cursor remains static in Normal mode and blinks in insert mode. --- textarea/textarea.go | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/textarea/textarea.go b/textarea/textarea.go index 1e3bd25..6cf4e7c 100644 --- a/textarea/textarea.go +++ b/textarea/textarea.go @@ -697,9 +697,7 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) { cmds = append(cmds, cmd) newRow, newCol := m.cursorLineNumber(), m.col - if m.mode == ModeInsert { - m.Cursor, cmd = m.Cursor.Update(msg) - } + m.Cursor, cmd = m.Cursor.Update(msg) if m.mode == ModeInsert && (newRow != oldRow || newCol != oldCol) { m.Cursor.Blink = false cmd = m.Cursor.BlinkCmd() @@ -711,18 +709,31 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) { return m, tea.Batch(cmds...) } +// SetMode sets the mode of the textarea. +func (m *Model) SetMode(mode Mode) tea.Cmd { + switch mode { + case ModeInsert: + m.mode = ModeInsert + return m.Cursor.SetCursorMode(cursor.CursorBlink) + case ModeNormal: + m.mode = ModeNormal + return m.Cursor.SetCursorMode(cursor.CursorStatic) + } + return nil +} + func (m *Model) normalUpdate(msg tea.Msg) tea.Cmd { switch msg := msg.(type) { case tea.KeyMsg: switch msg.String() { case "i": - m.mode = ModeInsert + return m.SetMode(ModeInsert) case "I": m.CursorStart() - m.mode = ModeInsert + return m.SetMode(ModeInsert) case "A": m.CursorEnd() - m.mode = ModeInsert + return m.SetMode(ModeInsert) case "e": m.wordRight() case "w": @@ -757,7 +768,7 @@ func (m *Model) insertUpdate(msg tea.Msg) tea.Cmd { case tea.KeyMsg: switch msg.String() { case "esc": - m.mode = ModeNormal + return m.SetMode(ModeNormal) case "ctrl+k": m.col = clamp(m.col, 0, len(m.value[m.row])) if m.col >= len(m.value[m.row]) {