2020-01-15 16:44:11 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
// A simple program that counts down from 5 and then exits.
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2020-06-16 22:31:37 -04:00
|
|
|
"os"
|
2020-01-15 16:44:11 -05:00
|
|
|
"time"
|
2020-01-18 11:15:55 -05:00
|
|
|
|
2020-05-27 14:47:31 -04:00
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
2020-01-15 16:44:11 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2021-05-01 09:28:58 -04:00
|
|
|
// Log to a file. Useful in debugging since you can't really log to stdout.
|
|
|
|
// Not required.
|
2020-10-15 16:08:42 -04:00
|
|
|
logfilePath := os.Getenv("BUBBLETEA_LOG")
|
2020-06-16 22:31:37 -04:00
|
|
|
if logfilePath != "" {
|
|
|
|
if _, err := tea.LogToFile(logfilePath, "simple"); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-18 11:42:19 -05:00
|
|
|
// Initialize our program
|
2020-10-15 16:08:42 -04:00
|
|
|
p := tea.NewProgram(model(5))
|
2020-01-18 11:42:19 -05:00
|
|
|
if err := p.Start(); err != nil {
|
2020-01-15 16:44:11 -05:00
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-15 16:08:42 -04:00
|
|
|
// A model can be more or less any type of data. It holds all the data for a
|
|
|
|
// program, so often it's a struct. For this simple example, however, all
|
|
|
|
// we'll need is a simple integer.
|
|
|
|
type model int
|
|
|
|
|
2020-10-15 20:51:00 -04:00
|
|
|
// Init optionally returns an initial command we should run. In this case we
|
2020-10-15 16:19:13 -04:00
|
|
|
// want to start the timer.
|
2020-10-15 16:08:42 -04:00
|
|
|
func (m model) Init() tea.Cmd {
|
|
|
|
return tick
|
2020-01-18 22:18:19 -05:00
|
|
|
}
|
|
|
|
|
2021-03-11 20:11:26 -05:00
|
|
|
// Update is called when messages are received. The idea is that you inspect the
|
2020-10-15 20:51:00 -04:00
|
|
|
// message and send back an updated model accordingly. You can also return
|
2021-03-11 20:11:26 -05:00
|
|
|
// a command, which is a function that peforms I/O and returns a message.
|
2020-10-15 16:08:42 -04:00
|
|
|
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
2020-01-15 16:44:11 -05:00
|
|
|
switch msg.(type) {
|
2020-05-25 19:26:40 -04:00
|
|
|
case tea.KeyMsg:
|
|
|
|
return m, tea.Quit
|
2020-05-05 14:26:06 -04:00
|
|
|
case tickMsg:
|
2020-01-15 16:44:11 -05:00
|
|
|
m -= 1
|
|
|
|
if m <= 0 {
|
2020-05-25 19:26:40 -04:00
|
|
|
return m, tea.Quit
|
2020-01-15 16:44:11 -05:00
|
|
|
}
|
2020-05-12 17:56:30 -04:00
|
|
|
return m, tick
|
2020-01-15 16:44:11 -05:00
|
|
|
}
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
2020-10-15 20:51:00 -04:00
|
|
|
// Views return a string based on data in the model. That string which will be
|
|
|
|
// rendered to the terminal.
|
2020-10-15 16:08:42 -04:00
|
|
|
func (m model) View() string {
|
2020-05-12 17:05:16 -04:00
|
|
|
return fmt.Sprintf("Hi. This program will exit in %d seconds. To quit sooner press any key.\n", m)
|
2020-01-15 16:44:11 -05:00
|
|
|
}
|
|
|
|
|
2020-10-15 19:48:42 -04:00
|
|
|
// Messages are events that we respond to in our Update function. This
|
|
|
|
// particular one indicates that the timer has ticked.
|
|
|
|
type tickMsg time.Time
|
|
|
|
|
2020-05-25 19:26:40 -04:00
|
|
|
func tick() tea.Msg {
|
2020-05-12 17:56:30 -04:00
|
|
|
time.Sleep(time.Second)
|
|
|
|
return tickMsg{}
|
2020-01-15 16:44:11 -05:00
|
|
|
}
|