chore!(cursor): improve naming around 'blinking'

This commit is contained in:
Christian Rocha 2025-05-26 08:13:36 -04:00
parent 0f113d10c4
commit e89dc94c85
4 changed files with 24 additions and 27 deletions

View File

@ -73,10 +73,9 @@ type Model struct {
// unless [CursorMode] is not set to [CursorBlink].
BlinkSpeed time.Duration
// Blink is the state of the cursor blink. When true, the cursor is hidden.
//
// TODO: rename to Blinking.
Blink bool
// IsBlinked is the state of the cursor blink. When true, the cursor is
// hidden.
IsBlinked bool
// char is the character under the cursor
char string
@ -102,7 +101,7 @@ func New() Model {
return Model{
id: nextID(),
BlinkSpeed: defaultBlinkSpeed,
Blink: true,
IsBlinked: true,
mode: CursorBlink,
blinkCtx: &blinkCtx{
@ -121,7 +120,7 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
return m, nil
}
cmd := m.BlinkCmd()
cmd := m.Blink()
return m, cmd
case tea.FocusMsg:
@ -147,8 +146,8 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
var cmd tea.Cmd
if m.mode == CursorBlink {
m.Blink = !m.Blink
cmd = m.BlinkCmd()
m.IsBlinked = !m.IsBlinked
cmd = m.Blink()
}
return m, cmd
@ -173,17 +172,15 @@ func (m *Model) SetMode(mode Mode) tea.Cmd {
return nil
}
m.mode = mode
m.Blink = m.mode == CursorHide || !m.focus
m.IsBlinked = m.mode == CursorHide || !m.focus
if mode == CursorBlink {
return Blink
}
return nil
}
// BlinkCmd is a command used to manage cursor blinking.
//
// TODO: Rename to Blink.
func (m *Model) BlinkCmd() tea.Cmd {
// Blink is a command used to manage cursor blinking.
func (m *Model) Blink() tea.Cmd {
if m.mode != CursorBlink {
return nil
}
@ -216,10 +213,10 @@ func Blink() tea.Msg {
// Focus focuses the cursor to allow it to blink if desired.
func (m *Model) Focus() tea.Cmd {
m.focus = true
m.Blink = m.mode == CursorHide // show the cursor unless we've explicitly hidden it
m.IsBlinked = m.mode == CursorHide // show the cursor unless we've explicitly hidden it
if m.mode == CursorBlink && m.focus {
return m.BlinkCmd()
return m.Blink()
}
return nil
}
@ -227,7 +224,7 @@ func (m *Model) Focus() tea.Cmd {
// Blur blurs the cursor.
func (m *Model) Blur() {
m.focus = false
m.Blink = true
m.IsBlinked = true
}
// SetChar sets the character under the cursor.
@ -237,7 +234,7 @@ func (m *Model) SetChar(char string) {
// View displays the cursor.
func (m Model) View() string {
if m.Blink {
if m.IsBlinked {
return m.TextStyle.Inline(true).Render(m.char)
}
return m.Style.Inline(true).Reverse(true).Render(m.char)

View File

@ -8,7 +8,7 @@ import (
// TestBlinkCmdDataRace tests for a race on [Cursor.blinkTag].
//
// The original [Model.BlinkCmd] implementation returned a closure over the pointer receiver:
// The original [Model.Blink] implementation returned a closure over the pointer receiver:
//
// return func() tea.Msg {
// defer cancel()
@ -20,12 +20,12 @@ import (
// }
//
// A race on “m.blinkTag” will occur if:
// 1. [Model.BlinkCmd] is called e.g. by calling [Model.Focus] from
// 1. [Model.Blink] is called e.g. by calling [Model.Focus] from
// ["github.com/charmbracelet/bubbletea".Model.Update];
// 2. ["github.com/charmbracelet/bubbletea".handleCommands] is kept sufficiently busy that it does not recieve and
// execute the [Model.BlinkCmd] e.g. by other long running command or commands;
// execute the [Model.Blink] e.g. by other long running command or commands;
// 3. at least [Mode.BlinkSpeed] time elapses;
// 4. [Model.BlinkCmd] is called again;
// 4. [Model.Blink] is called again;
// 5. ["github.com/charmbracelet/bubbletea".handleCommands] gets around to receiving and executing the original
// closure.
//
@ -33,7 +33,7 @@ import (
// current value rather than the value at the time the closure was created).
func TestBlinkCmdDataRace(t *testing.T) {
m := New()
cmd := m.BlinkCmd()
cmd := m.Blink()
var wg sync.WaitGroup
wg.Add(2)
go func() {
@ -44,7 +44,7 @@ func TestBlinkCmdDataRace(t *testing.T) {
go func() {
defer wg.Done()
time.Sleep(m.BlinkSpeed * 2)
m.BlinkCmd()
m.Blink()
}()
wg.Wait()
}

View File

@ -1204,8 +1204,8 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
// nuance that makes cursor movement obvious and feel snappy.
newRow, newCol := m.cursorLineNumber(), m.col
if (newRow != oldRow || newCol != oldCol) && m.virtualCursor.Mode() == cursor.CursorBlink {
m.virtualCursor.Blink = false
cmd = m.virtualCursor.BlinkCmd()
m.virtualCursor.IsBlinked = false
cmd = m.virtualCursor.Blink()
}
cmds = append(cmds, cmd)
}

View File

@ -671,8 +671,8 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
// If the cursor position changed, reset the blink state. This is a
// small UX nuance that makes cursor movement obvious and feel snappy.
if oldPos != m.pos && m.virtualCursor.Mode() == cursor.CursorBlink {
m.virtualCursor.Blink = false
cmds = append(cmds, m.virtualCursor.BlinkCmd())
m.virtualCursor.IsBlinked = false
cmds = append(cmds, m.virtualCursor.Blink())
}
}