main.go
This file
Path
examples/list-default/main.go
88 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 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
lines 7–9why, one step back7 "charm.land/bubbles/v2/list"8 tea "charm.land/bubbletea/v2"9 "charm.land/lipgloss/v2"feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9
lines 10–31why, one step back10)1112var docStyle = lipgloss.NewStyle().Margin(1, 2)1314type item struct {15 title, desc string16}1718func (i item) Title() string { return i.title }19func (i item) Description() string { return i.desc }20func (i item) FilterValue() string { return i.title }2122type model struct {23 list list.Model24}2526func (m model) Init() tea.Cmd {27 return nil28}2930func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {31 switch msg := msg.(type) {Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97
line 32why, one step back32 case tea.KeyPressMsg:feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9
lines 33–45why, one step back33 if msg.String() == "ctrl+c" {34 return m, tea.Quit35 }36 case tea.WindowSizeMsg:37 h, v := docStyle.GetFrameSize()38 m.list.SetSize(msg.Width-h, msg.Height-v)39 }4041 var cmd tea.Cmd42 m.list, cmd = m.list.Update(msg)43 return m, cmd44}45Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97
lines 46–49why, one step back46func (m model) View() tea.View {47 v := tea.NewView(docStyle.Render(m.list.View()))48 v.AltScreen = true49 return vfeat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9
lines 50–81why, one step back50}5152func main() {53 items := []list.Item{54 item{title: "Raspberry Pi’s", desc: "I have ’em all over my house"},55 item{title: "Nutella", desc: "It's good on toast"},56 item{title: "Bitter melon", desc: "It cools you down"},57 item{title: "Nice socks", desc: "And by that I mean socks without holes"},58 item{title: "Eight hours of sleep", desc: "I had this once"},59 item{title: "Cats", desc: "Usually"},60 item{title: "Plantasia, the album", desc: "My plants love it too"},61 item{title: "Pour over coffee", desc: "It takes forever to make though"},62 item{title: "VR", desc: "Virtual reality...what is there to say?"},63 item{title: "Noguchi Lamps", desc: "Such pleasing organic forms"},64 item{title: "Linux", desc: "Pretty much the best OS"},65 item{title: "Business school", desc: "Just kidding"},66 item{title: "Pottery", desc: "Wet clay is a great feeling"},67 item{title: "Shampoo", desc: "Nothing like clean hair"},68 item{title: "Table tennis", desc: "It’s surprisingly exhausting"},69 item{title: "Milk crates", desc: "Great for packing in your extra stuff"},70 item{title: "Afternoon tea", desc: "Especially the tea sandwich part"},71 item{title: "Stickers", desc: "The thicker the vinyl the better"},72 item{title: "20° Weather", desc: "Celsius, not Fahrenheit"},73 item{title: "Warm light", desc: "Like around 2700 Kelvin"},74 item{title: "The vernal equinox", desc: "The autumnal equinox is pretty good too"},75 item{title: "Gaffer’s tape", desc: "Basically sticky fabric"},76 item{title: "Terrycloth", desc: "In other words, towel fabric"},77 }7879 m := model{list: list.New(items, list.NewDefaultDelegate(), 0, 0)}80 m.list.Title = "My Fave Things"81Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97
line 82why, one step back82 p := tea.NewProgram(m)feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9
lines 83–88why, one step back8384 if _, err := p.Run(); err != nil {85 fmt.Println("Error running program:", err)86 os.Exit(1)87 }88}