Skip to content
Myr

Convert remaining examples to Model interface-based structure

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Christian Rocha · committed by Christian Rocha · parent 7530fb0 · Browse files · 8 files+78 added−138 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

8 files+78 added−138 removed

  • other:+78 added−138 removed

8 files+78 added−138 removed

modified:examples/fullscreen/main.go+5 added-8 removed
14 unmodified lines
1516type tickMsg time.Time1718func main() {19 p := tea.NewProgram(initialize, update, view)19 p := tea.NewProgram(model(5))2021 p.EnterAltScreen()22 err := p.Start()23 p.ExitAltScreen()
2 unmodified lines
26 log.Fatal(err)27 }28}2930func initialize() (tea.Model, tea.Cmd) {31 return model(5), tick()30func (m model) Init() tea.Cmd {31 return tick()32}3334func update(message tea.Msg, mdl tea.Model) (tea.Model, tea.Cmd) {35 m, _ := mdl.(model)3634func (m model) Update(message tea.Msg) (tea.Model, tea.Cmd) {35 switch msg := message.(type) {3637 case tea.KeyMsg:38 switch msg.String() {
16 unmodified lines
5556 return m, nil57}5861func view(mdl tea.Model) string {62 m, _ := mdl.(model)59func (m model) View() string {60 return fmt.Sprintf("\n\n Hi. This program will exit in %d seconds...", m)61}6263func tick() tea.Cmd {
4 unmodified lines
modified:examples/http/main.go+11 added-15 removed
1 unmodified line
23// A simple program that makes a GET request and prints the response status.45import (6 "errors"6 "fmt"7 "log"8 "net/http"9 "time"
2 unmodified lines
12)1314const url = "https://charm.sh/"1517type Model struct {16type model struct {17 status int18 err error19}2021type statusMsg int23type errMsg error2223type errMsg struct{ error }2425func (e errMsg) Error() string { return e.Error() }2627func main() {26 p := tea.NewProgram(initialize, update, view)28 p := tea.NewProgram(model{})29 if err := p.Start(); err != nil {30 log.Fatal(err)31 }32}3332func initialize() (tea.Model, tea.Cmd) {33 return Model{0, nil}, checkServer34func (m model) Init() tea.Cmd {35 return checkServer36}3736func update(msg tea.Msg, model tea.Model) (tea.Model, tea.Cmd) {37 m, ok := model.(Model)38 if !ok {39 return Model{err: errors.New("could not perform assertion on model during update")}, nil40 }4138func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {39 switch msg := msg.(type) {4041 case tea.KeyMsg:42 switch msg.String() {
19 unmodified lines
62 return m, nil63 }64}6569func view(model tea.Model) string {70 m, _ := model.(Model)66func (m model) View() string {67 s := fmt.Sprintf("Checking %s...", url)68 if m.err != nil {69 s += fmt.Sprintf("something went wrong: %s", m.err)70 } else if m.status != 0 {
7 unmodified lines
78 Timeout: 10 * time.Second,79 }80 res, err := c.Get(url)81 if err != nil {86 return errMsg(err)82 return errMsg{err}83 }84 return statusMsg(res.StatusCode)85}
modified:examples/pager/main.go+5 added-15 removed
51 unmodified lines
52 }53 defer f.Close()54 }5556 p := tea.NewProgram(initialize(string(content)), update, view)56 p := tea.NewProgram(model{content: string(content)})5758 // Use the full size of the terminal in its "alternate screen buffer"59 p.EnterAltScreen()60 defer p.ExitAltScreen()
13 unmodified lines
74 ready bool75 viewport viewport.Model76}7778func initialize(content string) func() (tea.Model, tea.Cmd) {79 return func() (tea.Model, tea.Cmd) {80 return model{81 // Store content in the model so we can hand it off to the viewport82 // later.83 content: content,84 }, nil85 }78func (m model) Init() tea.Cmd {79 return nil80}8188func update(msg tea.Msg, mdl tea.Model) (tea.Model, tea.Cmd) {89 m, _ := mdl.(model)9082func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {83 var (84 cmd tea.Cmd85 cmds []tea.Cmd86 )
46 unmodified lines
133134 return m, tea.Batch(cmds...)135}136145func view(mdl tea.Model) string {146 m, _ := mdl.(model)147137func (m model) View() string {138 if !m.ready {139 return "\n Initalizing..."140 }141
17 unmodified lines
modified:examples/result/main.go+9 added-10 removed
27 unmodified lines
28 result := make(chan string, 1)2930 // Pass the channel to the initialize function so our Bubble Tea program31 // can send the final choice along when the time comes.32 if err := tea.NewProgram(initialize(result), update, view).Start(); err != nil {32 p := tea.NewProgram(model{cursor: 0, choice: result})33 if err := p.Start(); err != nil {34 fmt.Println("Oh no:", err)35 os.Exit(1)36 }37
5 unmodified lines
4344// Pass a channel to the model to listen to the result value. This is a45// function that returns the initialize function and is typically how you would46// pass arguments to a tea.Init function.46func initialize(choice chan string) func() (tea.Model, tea.Cmd) {47 return func() (tea.Model, tea.Cmd) {48 return model{cursor: 0, choice: choice}, nil49 }47func initialModel(choice chan string) model {48 return model{cursor: 0, choice: choice}49}5052func update(msg tea.Msg, mdl tea.Model) (tea.Model, tea.Cmd) {53 m, _ := mdl.(model)51func (m model) Init() tea.Cmd {52 return nil53}5455func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {56 switch msg := msg.(type) {57 case tea.KeyMsg:58 switch msg.String() {59
23 unmodified lines
8384 return m, nil85}8686func view(mdl tea.Model) string {87 m, _ := mdl.(model)8887func (m model) View() string {88 s := strings.Builder{}89 s.WriteString("What kind of Bubble Tea would you like to order?\n\n")9091 for i := 0; i < len(choices); i++ {
12 unmodified lines
modified:examples/simple/main.go+4 added-4 removed
25 unmodified lines
26 log.Fatal(err)27 }28}2930// Messages are events that we respond to in our Update function. This31// particular one indicates that the timer has ticked.32type tickMsg time.Time3330// A model can be more or less any type of data. It holds all the data for a31// program, so often it's a struct. For this simple example, however, all32// we'll need is a simple integer.33type model int
27 unmodified lines
61func (m model) View() string {62 return fmt.Sprintf("Hi. This program will exit in %d seconds. To quit sooner press any key.\n", m)63}6465// Messages are events that we respond to in our Update function. This66// particular one indicates that the timer has ticked.67type tickMsg time.Time6869func tick() tea.Msg {70 time.Sleep(time.Second)71 return tickMsg{}72}
modified:examples/spinner/main.go+13 added-23 removed
10 unmodified lines
11 tea "github.com/charmbracelet/bubbletea"12 "github.com/muesli/termenv"13)1415var (16 color = termenv.ColorProfile()15var term = termenv.ColorProfile()17)1617type errMsg error1819type Model struct {19type model struct {20 spinner spinner.Model21 quitting bool22 err error23}2425type errMsg error2625func main() {28 p := tea.NewProgram(initialize, update, view)26 p := tea.NewProgram(initialModel())27 if err := p.Start(); err != nil {28 fmt.Println(err)29 os.Exit(1)30 }31}3235func initialize() (tea.Model, tea.Cmd) {33func initialModel() model {34 s := spinner.NewModel()35 s.Frames = spinner.Dot36 return model{spinner: s}37}3839 return Model{40 spinner: s,41 }, spinner.Tick(s)39func (m model) Init() tea.Cmd {40 return spinner.Tick(m.spinner)41}4244func update(msg tea.Msg, model tea.Model) (tea.Model, tea.Cmd) {45 m, ok := model.(Model)46 if !ok {47 return model, nil48 }4943func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {44 switch msg := msg.(type) {4546 case tea.KeyMsg:47 switch msg.String() {
19 unmodified lines
67 }6869}7077func view(model tea.Model) string {78 m, ok := model.(Model)79 if !ok {80 return "could not perform assertion on model in view\n"81 }71func (m model) View() string {72 if m.err != nil {73 return m.err.Error()74 }75 s := termenv.76 String(spinner.View(m.spinner)).87 Foreground(color.Color("205")).77 Foreground(term.Color("205")).78 String()79 str := fmt.Sprintf("\n\n %s Loading forever...press q to quit\n\n", s)80 if m.quitting {81 return str + "\n"
3 unmodified lines
modified:examples/textinput/main.go+18 added-34 removed
2 unmodified lines
3// A simple program demonstrating the text input component from the Bubbles4// component library.56import (7 "errors"7 "fmt"8 "log"910 input "github.com/charmbracelet/bubbles/textinput"11 tea "github.com/charmbracelet/bubbletea"12)1315type Model struct {16 textInput input.Model17 err error18}1920type tickMsg struct{}21type errMsg error2214func main() {24 p := tea.NewProgram(25 initialize,26 update,27 view,28 )15 p := tea.NewProgram(initialModel())1617 if err := p.Start(); err != nil {18 log.Fatal(err)19 }20}2135func initialize() (tea.Model, tea.Cmd) {22type tickMsg struct{}23type errMsg error2425type model struct {26 textInput input.Model27 err error28}2930func initialModel() model {31 inputModel := input.NewModel()32 inputModel.Placeholder = "Pikachu"33 inputModel.Focus()3440 return Model{35 return model{36 textInput: inputModel,37 err: nil,43 }, input.Blink(inputModel)38 }39}4046func update(msg tea.Msg, model tea.Model) (tea.Model, tea.Cmd) {41func (m model) Init() tea.Cmd {42 return input.Blink(m.textInput)43}4445func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {46 var cmd tea.Cmd48 m, ok := model.(Model)49 if !ok {50 // When we encounter errors in Update we simply add the error to the51 // model so we can handle it in the view. We could also return a command52 // that does something else with the error, like logs it via IO.53 return Model{54 err: errors.New("could not perform assertion on model in update"),55 }, nil56 }4748 switch msg := msg.(type) {49 case tea.KeyMsg:50 switch msg.Type {
14 unmodified lines
65 m.textInput, cmd = input.Update(msg, m.textInput)66 return m, cmd67}6879func view(model tea.Model) string {80 m, ok := model.(Model)81 if !ok {82 return "Oh no: could not perform assertion on model."83 } else if m.err != nil {84 return fmt.Sprintf("Uh oh: %s", m.err)85 }69func (m model) View() string {70 return fmt.Sprintf(71 "What’s your favorite Pokémon?\n\n%s\n\n%s",72 input.View(m.textInput),73 "(esc to quit)",
2 unmodified lines
modified:examples/textinputs/main.go+13 added-29 removed
10 unmodified lines
11 tea "github.com/charmbracelet/bubbletea"12 te "github.com/muesli/termenv"13)1415const (16 focusedTextColor = "205"15const focusedTextColor = "205"17)1617var (18 color = te.ColorProfile().Color19 focusedPrompt = te.String("> ").Foreground(color("205")).String()
2 unmodified lines
22 blurredSubmitButton = "[ " + te.String("Submit").Foreground(color("240")).String() + " ]"23)2425func main() {28 if err := tea.NewProgram(29 initialize,30 update,31 view,32 ).Start(); err != nil {26 if err := tea.NewProgram(initialModel()).Start(); err != nil {27 fmt.Printf("could not start program: %s\n", err)28 os.Exit(1)29 }30}
5 unmodified lines
36 emailInput input.Model37 submitButton string38}3946func initialize() (tea.Model, tea.Cmd) {40func initialModel() model {41 name := input.NewModel()42 name.Placeholder = "Name"43 name.Focus()44 name.Prompt = focusedPrompt
6 unmodified lines
51 email := input.NewModel()52 email.Placeholder = "Email"53 email.Prompt = blurredPrompt5461 return model{0, name, nickName, email, blurredSubmitButton},62 tea.Batch(63 input.Blink(name),64 input.Blink(nickName),65 input.Blink(email),66 )55 return model{0, name, nickName, email, blurredSubmitButton}5657}58func (m model) Init() tea.Cmd {59 return tea.Batch(60 input.Blink(m.nameInput),61 input.Blink(m.nickNameInput),62 input.Blink(m.emailInput),63 )64}6570func update(msg tea.Msg, mdl tea.Model) (tea.Model, tea.Cmd) {71 m, ok := mdl.(model)72 if !ok {73 panic("could not perform assertion on model")74 }7566func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {67 var cmd tea.Cmd6869 switch msg := msg.(type) {7970 case tea.KeyMsg:71 switch msg.String() {7273 case "ctrl+c":
82 unmodified lines
156157 return m, tea.Batch(cmds...)158}159170func view(mdl tea.Model) string {171 m, ok := mdl.(model)172 if !ok {173 return "could not perform assertion on model"174 }175160func (m model) View() string {161 s := "\n"162163 inputs := []string{164 input.View(m.nameInput),
8 unmodified lines
173 }174 }175176 s += "\n\n" + m.submitButton + "\n"192177 return s178}