Skip to content

Commit

Permalink
fix(examples): cursor-style: cursor blink logic
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed Dec 9, 2024
1 parent a8ed961 commit 0f4cda0
Showing 1 changed file with 16 additions and 17 deletions.
33 changes: 16 additions & 17 deletions examples/cursor-style/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import (
)

type model struct {
style tea.CursorStyle
steady bool
style tea.CursorStyle
blink bool
}

func (m model) Init() (tea.Model, tea.Cmd) {
return m, tea.Batch(tea.ShowCursor, tea.SetCursorStyle(m.style, m.steady))
m.blink = true
return m, tea.Batch(tea.ShowCursor, tea.SetCursorStyle(m.style, m.blink))
}

func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
Expand All @@ -24,23 +25,23 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case "ctrl+q", "q":
return m, tea.Quit
case "h", "left":
if m.style == tea.CursorBlock && !m.steady {
if m.style == tea.CursorBlock && m.blink {
break
}
if !m.steady {
if m.blink {
m.style--
}
m.steady = !m.steady
cmd = tea.SetCursorStyle(m.style, m.steady)
m.blink = !m.blink
cmd = tea.SetCursorStyle(m.style, m.blink)
case "l", "right":
if m.style == tea.CursorBar && m.steady {
if m.style == tea.CursorBar && !m.blink {
break
}
if m.steady {
if !m.blink {
m.style++
}
m.steady = !m.steady
cmd = tea.SetCursorStyle(m.style, m.steady)
m.blink = !m.blink
cmd = tea.SetCursorStyle(m.style, m.blink)
}
}
return m, cmd
Expand All @@ -53,14 +54,12 @@ func (m model) View() string {
}

func (m model) describeCursor() string {
var (
adj, noun string
)
var adj, noun string

if m.steady {
adj = "steady"
} else {
if m.blink {
adj = "blinking"
} else {
adj = "steady"
}

switch m.style {
Expand Down

0 comments on commit 0f4cda0

Please sign in to comment.