From 4b64a7cbde104f4d2864aa16b768af025891788e Mon Sep 17 00:00:00 2001 From: Christian Rocha Date: Fri, 17 Jan 2020 23:27:54 -0500 Subject: [PATCH] Blink cursor --- examples/input/main.go | 35 +++++++++++++++++++++-------------- input/input.go | 15 ++++++--------- 2 files changed, 27 insertions(+), 23 deletions(-) diff --git a/examples/input/main.go b/examples/input/main.go index 24ac4c9..69023cc 100644 --- a/examples/input/main.go +++ b/examples/input/main.go @@ -25,8 +25,16 @@ func main() { }, update, view, - nil, - //[]tea.Sub{input.Blink}, + []tea.Sub{ + // Just hand off the subscription to the input component + func(m tea.Model) tea.Msg { + if m, ok := m.(model); ok { + return input.Blink(m.input) + } + // TODO: return error + return nil + }, + }, ) if err := p.Start(); err != nil { @@ -36,7 +44,6 @@ func main() { func update(msg tea.Msg, mdl tea.Model) (tea.Model, tea.Cmd) { var cmd tea.Cmd - m, _ := mdl.(model) switch msg := msg.(type) { @@ -47,22 +54,22 @@ func update(msg tea.Msg, mdl tea.Model) (tea.Model, tea.Cmd) { case "esc": return m, tea.Quit } - - case input.CursorBlinkMsg: - return input.Update(msg, m.input) } m.input, cmd = input.Update(msg, m.input) return m, cmd } -func view(mdl tea.Model) string { - m, _ := mdl.(model) - help := "(esc to exit)" +func view(m tea.Model) string { + if m, ok := m.(model); ok { + help := "(esc to exit)" - return fmt.Sprintf( - "What’s your favorite Pokémon?\n\n%s\n\n%s", - input.View(m.input), - help, - ) + return fmt.Sprintf( + "What’s your favorite Pokémon?\n\n%s\n\n%s", + input.View(m.input), + help, + ) + } + // TODO: return error + return "" } diff --git a/input/input.go b/input/input.go index cb39e56..07f5b48 100644 --- a/input/input.go +++ b/input/input.go @@ -27,9 +27,7 @@ func DefaultModel() Model { } } -func Update(msg tea.Msg, model tea.Model) (Model, tea.Cmd) { - m, _ := model.(Model) - +func Update(msg tea.Msg, m Model) (Model, tea.Cmd) { switch msg := msg.(type) { case tea.KeyMsg: @@ -52,15 +50,14 @@ func Update(msg tea.Msg, model tea.Model) (Model, tea.Cmd) { func View(model tea.Model) string { m, _ := model.(Model) cursor := m.Cursor - //if m.Blink { - //cursor = m.HiddenCursor - //} + if m.Blink { + cursor = m.HiddenCursor + } return m.Prompt + m.Value + cursor } func Blink(model tea.Model) tea.Msg { - //m, _ := model.(Model) - //time.Sleep(m.BlinkSpeed) - time.Sleep(time.Second) + m, _ := model.(Model) + time.Sleep(m.BlinkSpeed) return CursorBlinkMsg{} }