main.go
This file
Path
tutorials/basics/main.go
90 lines · 1.6 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–6why, one step back1package main23import (4 "fmt"5 "os"6Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97
line 7why, one step back7 tea "charm.land/bubbletea/v2"feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9
lines 8–17why, one step back8)910type model struct {11 cursor int12 choices []string13 selected map[int]struct{}14}1516func initialModel() model {17 return model{Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97
line 18why, one step back18 // Our to-do list is a grocery listfeat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9
lines 19–21why, one step back19 choices: []string{"Buy carrots", "Buy celery", "Buy kohlrabi"},2021 // A map which indicates which choices are selected. We're usingMerge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97
line 22why, one step back22 // the map like a mathematical set. The keys refer to the indexesfeat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9
lines 23–28why, one step back23 // of the `choices` slice, above.24 selected: make(map[int]struct{}),25 }26}2728func (m model) Init() tea.Cmd {Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97
line 29why, one step back29 return nilfeat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9
lines 30–33why, one step back30}3132func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {33 switch msg := msg.(type) {Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97
line 34why, one step back34 case tea.KeyPressMsg:feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9
lines 35–45why, one step back35 switch msg.String() {36 case "ctrl+c", "q":37 return m, tea.Quit38 case "up", "k":39 if m.cursor > 0 {40 m.cursor--41 }42 case "down", "j":43 if m.cursor < len(m.choices)-1 {44 m.cursor++45 }Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97
line 46why, one step back46 case "enter", "space":feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9
lines 47–58why, one step back47 _, ok := m.selected[m.cursor]48 if ok {49 delete(m.selected, m.cursor)50 } else {51 m.selected[m.cursor] = struct{}{}52 }53 }54 }5556 return m, nil57}58Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97
line 59why, one step back59func (m model) View() tea.View {feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9
lines 60–77why, one step back60 s := "What should we buy at the market?\n\n"6162 for i, choice := range m.choices {63 cursor := " "64 if m.cursor == i {65 cursor = ">"66 }6768 checked := " "69 if _, ok := m.selected[i]; ok {70 checked = "x"71 }7273 s += fmt.Sprintf("%s [%s] %s\n", cursor, checked, choice)74 }7576 s += "\nPress q to quit.\n"77Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97
lines 78–81why, one step back78 v := tea.NewView(s)79 v.WindowTitle = "Grocery List"8081 return vfeat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9
lines 82–90why, one step back82}8384func main() {85 p := tea.NewProgram(initialModel())86 if _, err := p.Run(); err != nil {87 fmt.Printf("Alas, there's been an error: %v", err)88 os.Exit(1)89 }90}