Skip to content
Myr

main.go

This file

Path

tutorials/commands/main.go

78 lines · 1.4 KiB · Go

Last changed by

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

History of this file

Take it

The walk stopped after 200 commits: lines older than 3dfbcd9 point at it.

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 1–8why, one step back
1package main
23import (4 "fmt"5 "net/http"6 "os"7 "time"8

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

line 9why, one step back
9 tea "charm.land/bubbletea/v2"

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 10–51why, one step back
10)
1112const url = "https://charm.sh/"1314type model struct {15 status int16 err error17}1819func checkServer() tea.Msg {20 c := &http.Client{Timeout: 10 * time.Second}21 res, err := c.Get(url)22 if err != nil {23 return errMsg{err}24 }25 defer res.Body.Close() // nolint:errcheck2627 return statusMsg(res.StatusCode)28}2930type statusMsg int3132type errMsg struct{ err error }3334// For messages that contain errors it's often handy to also implement the35// error interface on the message.36func (e errMsg) Error() string { return e.err.Error() }3738func (m model) Init() tea.Cmd {39 return checkServer40}4142func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {43 switch msg := msg.(type) {44 case statusMsg:45 m.status = int(msg)46 return m, tea.Quit4748 case errMsg:49 m.err = msg50 return m, tea.Quit51

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

lines 52–53why, one step back
52 case tea.KeyPressMsg:
53 if msg.Mod == tea.ModCtrl && msg.Code == 'c' {

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 54–60why, one step back
54 return m, tea.Quit
55 }56 }5758 return m, nil59}60

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

line 61why, one step back
61func (m model) View() tea.View {

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

line 62why, one step back
62 if m.err != nil {

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

line 63why, one step back
63 return tea.NewView(fmt.Sprintf("\nWe had some trouble: %v\n\n", m.err))

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 64–69why, one step back
64 }
6566 s := fmt.Sprintf("Checking %s ... ", url)67 if m.status > 0 {68 s += fmt.Sprintf("%d %s!", m.status, http.StatusText(m.status))69 }

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

line 70why, one step back
70 return tea.NewView("\n" + s + "\n\n")

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 71–78why, one step back
71}
7273func main() {74 if _, err := tea.NewProgram(model{}).Run(); err != nil {75 fmt.Printf("Uh oh, there was an error: %v\n", err)76 os.Exit(1)77 }78}