main.go
This file
Path
examples/send-msg/main.go
133 lines · 2.7 KiB · Go
Last changed by
Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97
History of this fileTake it
The walk stopped after 200 commits: lines older than 0ab3eac point at it.
chore(ci): sync golangci-lint configmeowgorithmbefore adoption0ab3eac
lines 1–12why, one step back1package main23// A simple example that shows how to send messages to a Bubble Tea program4// from outside the program using Program.Send(Msg).56import (7 "fmt"8 "math/rand"9 "os"10 "strings"11 "time"12Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97
lines 13–15why, one step back13 "charm.land/bubbles/v2/spinner"14 tea "charm.land/bubbletea/v2"15 "charm.land/lipgloss/v2"chore(ci): sync golangci-lint configmeowgorithmbefore adoption0ab3eac
lines 16–60why, one step back16)1718var (19 spinnerStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("63"))20 helpStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("241")).Margin(1, 0)21 dotStyle = helpStyle.UnsetMargins()22 durationStyle = dotStyle23 appStyle = lipgloss.NewStyle().Margin(1, 2, 0, 2)24)2526type resultMsg struct {27 duration time.Duration28 food string29}3031func (r resultMsg) String() string {32 if r.duration == 0 {33 return dotStyle.Render(strings.Repeat(".", 30))34 }35 return fmt.Sprintf("🍔 Ate %s %s", r.food,36 durationStyle.Render(r.duration.String()))37}3839type model struct {40 spinner spinner.Model41 results []resultMsg42 quitting bool43}4445func newModel() model {46 const numLastResults = 547 s := spinner.New()48 s.Style = spinnerStyle49 return model{50 spinner: s,51 results: make([]resultMsg, numLastResults),52 }53}5455func (m model) Init() tea.Cmd {56 return m.spinner.Tick57}5859func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {60 switch msg := msg.(type) {Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97
line 61why, one step back61 case tea.KeyPressMsg:chore(ci): sync golangci-lint configmeowgorithmbefore adoption0ab3eac
lines 62–75why, one step back62 m.quitting = true63 return m, tea.Quit64 case resultMsg:65 m.results = append(m.results[1:], msg)66 return m, nil67 case spinner.TickMsg:68 var cmd tea.Cmd69 m.spinner, cmd = m.spinner.Update(msg)70 return m, cmd71 default:72 return m, nil73 }74}75Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97
lines 76–77why, one step back76func (m model) View() tea.View {77 var b strings.Builderchore(ci): sync golangci-lint configmeowgorithmbefore adoption0ab3eac
lines 78–79why, one step back7879 if m.quitting {Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97
line 80why, one step back80 b.WriteString("That's all for today!")chore(ci): sync golangci-lint configmeowgorithmbefore adoption0ab3eac
line 81why, one step back81 } else {Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97
lines 82–83why, one step back82 b.WriteString(m.spinner.View())83 b.WriteString(" Eating food...")chore(ci): sync golangci-lint configmeowgorithmbefore adoption0ab3eac
lines 84–85why, one step back84 }85Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97
line 86why, one step back86 b.WriteString("\n\n")chore(ci): sync golangci-lint configmeowgorithmbefore adoption0ab3eac
lines 87–88why, one step back8788 for _, res := range m.results {Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97
lines 89–90why, one step back89 b.WriteString(res.String())90 b.WriteString("\n")chore(ci): sync golangci-lint configmeowgorithmbefore adoption0ab3eac
lines 91–93why, one step back91 }9293 if !m.quitting {Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97
line 94why, one step back94 b.WriteString(helpStyle.Render("Press any key to exit"))chore(ci): sync golangci-lint configmeowgorithmbefore adoption0ab3eac
lines 95–97why, one step back95 }9697 if m.quitting {Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97
line 98why, one step back98 b.WriteString("\n")chore(ci): sync golangci-lint configmeowgorithmbefore adoption0ab3eac
lines 99–100why, one step back99 }100Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97
line 101why, one step back101 return tea.NewView(appStyle.Render(b.String()))chore(ci): sync golangci-lint configmeowgorithmbefore adoption0ab3eac
lines 102–133why, one step back102}103104func main() {105 p := tea.NewProgram(newModel())106107 // Simulate activity108 go func() {109 for {110 pause := time.Duration(rand.Int63n(899)+100) * time.Millisecond // nolint:gosec111 time.Sleep(pause)112113 // Send the Bubble Tea program a message from outside the114 // tea.Program. This will block until it is ready to receive115 // messages.116 p.Send(resultMsg{food: randomFood(), duration: pause})117 }118 }()119120 if _, err := p.Run(); err != nil {121 fmt.Println("Error running program:", err)122 os.Exit(1)123 }124}125126func randomFood() string {127 food := []string{128 "an apple", "a pear", "a gherkin", "a party gherkin",129 "a kohlrabi", "some spaghetti", "tacos", "a currywurst", "some curry",130 "a sandwich", "some peanut butter", "some cashews", "some ramen",131 }132 return food[rand.Intn(len(food))] // nolint:gosec133}