main.go
This file
Path
examples/sequence/main.go
71 lines · 1.4 KiB · Go
Last changed by
examples/sequence: fix milisecond typo in SleepPrintlnmakoto-developerbefore adoption6fb1f47
History of this fileTake it
1package main23// A simple example illustrating how to run a series of commands in order.45import (6 "fmt"7 "os"8 "time"910 tea "charm.land/bubbletea/v2"11)1213type model struct{}1415func (m model) Init() tea.Cmd {16 // A tea.Sequence is a command that runs a series of commands in17 // order. Contrast this with tea.Batch, which runs a series of commands18 // concurrently, with no order guarantees.19 return tea.Sequence(20 tea.Batch(21 tea.Sequence(22 SleepPrintln("1-1-1", 1000),23 SleepPrintln("1-1-2", 1000),24 ),25 tea.Batch(26 SleepPrintln("1-2-1", 1500),27 SleepPrintln("1-2-2", 1250),28 ),29 ),30 tea.Println("2"),31 tea.Sequence(32 tea.Batch(33 SleepPrintln("3-1-1", 500),34 SleepPrintln("3-1-2", 1000),35 ),36 tea.Sequence(37 SleepPrintln("3-2-1", 750),38 SleepPrintln("3-2-2", 500),39 ),40 ),41 tea.Quit,42 )43}4445// print string after stopping for a certain period of time46func SleepPrintln(s string, millisecond int) tea.Cmd {47 printCmd := tea.Println(s)48 return func() tea.Msg {49 time.Sleep(time.Duration(millisecond) * time.Millisecond)50 return printCmd()51 }52}5354func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {55 switch msg.(type) {56 case tea.KeyPressMsg:57 return m, tea.Quit58 }59 return m, nil60}6162func (m model) View() tea.View {63 return tea.NewView("")64}6566func main() {67 if _, err := tea.NewProgram(model{}).Run(); err != nil {68 fmt.Println("Uh oh:", err)69 os.Exit(1)70 }71}