Blink cursor

This commit is contained in:
Christian Rocha 2020-01-17 23:27:54 -05:00
parent 923390de7a
commit 4b64a7cbde
No known key found for this signature in database
GPG Key ID: D6CC7A16E5878018
2 changed files with 27 additions and 23 deletions

View File

@ -25,8 +25,16 @@ func main() {
}, },
update, update,
view, view,
nil, []tea.Sub{
//[]tea.Sub{input.Blink}, // 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 { if err := p.Start(); err != nil {
@ -36,7 +44,6 @@ func main() {
func update(msg tea.Msg, mdl tea.Model) (tea.Model, tea.Cmd) { func update(msg tea.Msg, mdl tea.Model) (tea.Model, tea.Cmd) {
var cmd tea.Cmd var cmd tea.Cmd
m, _ := mdl.(model) m, _ := mdl.(model)
switch msg := msg.(type) { switch msg := msg.(type) {
@ -47,17 +54,14 @@ func update(msg tea.Msg, mdl tea.Model) (tea.Model, tea.Cmd) {
case "esc": case "esc":
return m, tea.Quit return m, tea.Quit
} }
case input.CursorBlinkMsg:
return input.Update(msg, m.input)
} }
m.input, cmd = input.Update(msg, m.input) m.input, cmd = input.Update(msg, m.input)
return m, cmd return m, cmd
} }
func view(mdl tea.Model) string { func view(m tea.Model) string {
m, _ := mdl.(model) if m, ok := m.(model); ok {
help := "(esc to exit)" help := "(esc to exit)"
return fmt.Sprintf( return fmt.Sprintf(
@ -66,3 +70,6 @@ func view(mdl tea.Model) string {
help, help,
) )
} }
// TODO: return error
return ""
}

View File

@ -27,9 +27,7 @@ func DefaultModel() Model {
} }
} }
func Update(msg tea.Msg, model tea.Model) (Model, tea.Cmd) { func Update(msg tea.Msg, m Model) (Model, tea.Cmd) {
m, _ := model.(Model)
switch msg := msg.(type) { switch msg := msg.(type) {
case tea.KeyMsg: case tea.KeyMsg:
@ -52,15 +50,14 @@ func Update(msg tea.Msg, model tea.Model) (Model, tea.Cmd) {
func View(model tea.Model) string { func View(model tea.Model) string {
m, _ := model.(Model) m, _ := model.(Model)
cursor := m.Cursor cursor := m.Cursor
//if m.Blink { if m.Blink {
//cursor = m.HiddenCursor cursor = m.HiddenCursor
//} }
return m.Prompt + m.Value + cursor return m.Prompt + m.Value + cursor
} }
func Blink(model tea.Model) tea.Msg { func Blink(model tea.Model) tea.Msg {
//m, _ := model.(Model) m, _ := model.(Model)
//time.Sleep(m.BlinkSpeed) time.Sleep(m.BlinkSpeed)
time.Sleep(time.Second)
return CursorBlinkMsg{} return CursorBlinkMsg{}
} }