From c15b863c39ed57ff9d917cc61432866ad1487a6b Mon Sep 17 00:00:00 2001 From: Christian Rocha Date: Wed, 15 Jan 2020 16:44:11 -0500 Subject: [PATCH] Add simple, minimal example --- examples/simple/main.go | 44 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 examples/simple/main.go diff --git a/examples/simple/main.go b/examples/simple/main.go new file mode 100644 index 0000000..5653db5 --- /dev/null +++ b/examples/simple/main.go @@ -0,0 +1,44 @@ +package main + +// A simple program that counts down from 5 and then exits. + +import ( + "fmt" + "log" + "tea" + "time" +) + +type model int + +type tickMsg struct{} + +func main() { + err := tea.NewProgram(model(5), update, view, []tea.Sub{tick}).Start() + if err != nil { + log.Fatal(err) + } +} + +func update(msg tea.Msg, mdl tea.Model) (tea.Model, tea.Cmd) { + m, _ := mdl.(model) + + switch msg.(type) { + case tickMsg: + m -= 1 + if m <= 0 { + return m, tea.Quit + } + } + return m, nil +} + +func view(mdl tea.Model) string { + m, _ := mdl.(model) + return fmt.Sprintf("Hi. This program will exit in %d seconds...\n", m) +} + +func tick(_ tea.Model) tea.Msg { + time.Sleep(time.Second) + return tickMsg{} +}