fix: cursor updating

cursor remains static in Normal mode and blinks in insert mode.
This commit is contained in:
Maas Lalani 2022-08-09 10:58:41 -04:00
parent f5b74d002c
commit 57a01e62e3
No known key found for this signature in database
GPG Key ID: 5A6ED5CBF1A0A000

View File

@ -697,9 +697,7 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
cmds = append(cmds, cmd) cmds = append(cmds, cmd)
newRow, newCol := m.cursorLineNumber(), m.col 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) { if m.mode == ModeInsert && (newRow != oldRow || newCol != oldCol) {
m.Cursor.Blink = false m.Cursor.Blink = false
cmd = m.Cursor.BlinkCmd() cmd = m.Cursor.BlinkCmd()
@ -711,18 +709,31 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
return m, tea.Batch(cmds...) 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 { func (m *Model) normalUpdate(msg tea.Msg) tea.Cmd {
switch msg := msg.(type) { switch msg := msg.(type) {
case tea.KeyMsg: case tea.KeyMsg:
switch msg.String() { switch msg.String() {
case "i": case "i":
m.mode = ModeInsert return m.SetMode(ModeInsert)
case "I": case "I":
m.CursorStart() m.CursorStart()
m.mode = ModeInsert return m.SetMode(ModeInsert)
case "A": case "A":
m.CursorEnd() m.CursorEnd()
m.mode = ModeInsert return m.SetMode(ModeInsert)
case "e": case "e":
m.wordRight() m.wordRight()
case "w": case "w":
@ -757,7 +768,7 @@ func (m *Model) insertUpdate(msg tea.Msg) tea.Cmd {
case tea.KeyMsg: case tea.KeyMsg:
switch msg.String() { switch msg.String() {
case "esc": case "esc":
m.mode = ModeNormal return m.SetMode(ModeNormal)
case "ctrl+k": case "ctrl+k":
m.col = clamp(m.col, 0, len(m.value[m.row])) m.col = clamp(m.col, 0, len(m.value[m.row]))
if m.col >= len(m.value[m.row]) { if m.col >= len(m.value[m.row]) {