main.go
This file
Path
examples/http/main.go
82 lines · 1.3 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 3dfbcd9 point at it.
feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9
lines 1–10why, one step back1package main23// A simple program that makes a GET request and prints the response status.45import (6 "fmt"7 "log"8 "net/http"9 "time"10Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97
line 11why, one step back11 tea "charm.land/bubbletea/v2"feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9
lines 12–39why, one step back12)1314const url = "https://charm.sh/"1516type model struct {17 status int18 err error19}2021type statusMsg int2223type errMsg struct{ error }2425func (e errMsg) Error() string { return e.error.Error() }2627func main() {28 p := tea.NewProgram(model{})29 if _, err := p.Run(); err != nil {30 log.Fatal(err)31 }32}3334func (m model) Init() tea.Cmd {35 return checkServer36}3738func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {39 switch msg := msg.(type) {Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97
line 40why, one step back40 case tea.KeyPressMsg:feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9
lines 41–60why, one step back41 switch msg.String() {42 case "q", "ctrl+c", "esc":43 return m, tea.Quit44 default:45 return m, nil46 }4748 case statusMsg:49 m.status = int(msg)50 return m, tea.Quit5152 case errMsg:53 m.err = msg54 return m, nil5556 default:57 return m, nil58 }59}60Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97
line 61why, one step back61func (m model) View() tea.View {feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9
lines 62–67why, one step back62 s := fmt.Sprintf("Checking %s...", url)63 if m.err != nil {64 s += fmt.Sprintf("something went wrong: %s", m.err)65 } else if m.status != 0 {66 s += fmt.Sprintf("%d %s", m.status, http.StatusText(m.status))67 }Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97
line 68why, one step back68 return tea.NewView(s + "\n")feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9
lines 69–82why, one step back69}7071func checkServer() tea.Msg {72 c := &http.Client{73 Timeout: 10 * time.Second,74 }75 res, err := c.Get(url)76 if err != nil {77 return errMsg{err}78 }79 defer res.Body.Close() // nolint:errcheck8081 return statusMsg(res.StatusCode)82}