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
Provenance
- Commit
847787e- Tree
f360961- Author
Christian Rocha <christian@rocha.is>- Committer
Christian Rocha <christian@rocha.is>- Parent
7530fb0
8 files changed+78 added−138 removed
modified:examples/fullscreen/main.go+5 added-8 removed
14 unmodified lines15 16 type tickMsg time.Time17 18 func main() {19- p := tea.NewProgram(initialize, update, view)19+ p := tea.NewProgram(model(5))20 21 p.EnterAltScreen()22 err := p.Start()23 p.ExitAltScreen()2 unmodified lines26 log.Fatal(err)27 }28 }29 30-func initialize() (tea.Model, tea.Cmd) {31- return model(5), tick()30+func (m model) Init() tea.Cmd {31+ return tick()32 }33 34-func update(message tea.Msg, mdl tea.Model) (tea.Model, tea.Cmd) {35- m, _ := mdl.(model)36-34+func (m model) Update(message tea.Msg) (tea.Model, tea.Cmd) {35 switch msg := message.(type) {36 37 case tea.KeyMsg:38 switch msg.String() {16 unmodified lines55 56 return m, nil57 }58 61-func view(mdl tea.Model) string {62- m, _ := mdl.(model)59+func (m model) View() string {60 return fmt.Sprintf("\n\n Hi. This program will exit in %d seconds...", m)61 }62 63 func tick() tea.Cmd {4 unmodified linesmodified:examples/http/main.go+11 added-15 removed
1 unmodified line2 3 // A simple program that makes a GET request and prints the response status.4 5 import (6- "errors"6 "fmt"7 "log"8 "net/http"9 "time"2 unmodified lines12 )13 14 const url = "https://charm.sh/"15 17-type Model struct {16+type model struct {17 status int18 err error19 }20 21 type statusMsg int23-type errMsg error22 23+type errMsg struct{ error }24+25+func (e errMsg) Error() string { return e.Error() }26+27 func 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 }33 32-func initialize() (tea.Model, tea.Cmd) {33- return Model{0, nil}, checkServer34+func (m model) Init() tea.Cmd {35+ return checkServer36 }37 36-func 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- }41-38+func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {39 switch msg := msg.(type) {40 41 case tea.KeyMsg:42 switch msg.String() {19 unmodified lines62 return m, nil63 }64 }65 69-func view(model tea.Model) string {70- m, _ := model.(Model)66+func (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 lines78 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 lines52 }53 defer f.Close()54 }55 56- p := tea.NewProgram(initialize(string(content)), update, view)56+ p := tea.NewProgram(model{content: string(content)})57 58 // Use the full size of the terminal in its "alternate screen buffer"59 p.EnterAltScreen()60 defer p.ExitAltScreen()13 unmodified lines74 ready bool75 viewport viewport.Model76 }77 78-func 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- }78+func (m model) Init() tea.Cmd {79+ return nil80 }81 88-func update(msg tea.Msg, mdl tea.Model) (tea.Model, tea.Cmd) {89- m, _ := mdl.(model)90-82+func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {83 var (84 cmd tea.Cmd85 cmds []tea.Cmd86 )46 unmodified lines133 134 return m, tea.Batch(cmds...)135 }136 145-func view(mdl tea.Model) string {146- m, _ := mdl.(model)147-137+func (m model) View() string {138 if !m.ready {139 return "\n Initalizing..."140 }141 17 unmodified linesmodified:examples/result/main.go+9 added-10 removed
27 unmodified lines28 result := make(chan string, 1)29 30 // 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 lines43 44 // 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.46-func initialize(choice chan string) func() (tea.Model, tea.Cmd) {47- return func() (tea.Model, tea.Cmd) {48- return model{cursor: 0, choice: choice}, nil49- }47+func initialModel(choice chan string) model {48+ return model{cursor: 0, choice: choice}49 }50 52-func update(msg tea.Msg, mdl tea.Model) (tea.Model, tea.Cmd) {53- m, _ := mdl.(model)51+func (m model) Init() tea.Cmd {52+ return nil53+}54 55+func (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 lines83 84 return m, nil85 }86 86-func view(mdl tea.Model) string {87- m, _ := mdl.(model)88-87+func (m model) View() string {88 s := strings.Builder{}89 s.WriteString("What kind of Bubble Tea would you like to order?\n\n")90 91 for i := 0; i < len(choices); i++ {12 unmodified linesmodified:examples/simple/main.go+4 added-4 removed
25 unmodified lines26 log.Fatal(err)27 }28 }29 30-// Messages are events that we respond to in our Update function. This31-// particular one indicates that the timer has ticked.32-type tickMsg time.Time33-30 // 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.33 type model int27 unmodified lines61 func (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 }64 65+// Messages are events that we respond to in our Update function. This66+// particular one indicates that the timer has ticked.67+type tickMsg time.Time68+69 func tick() tea.Msg {70 time.Sleep(time.Second)71 return tickMsg{}72 }modified:examples/spinner/main.go+13 added-23 removed
10 unmodified lines11 tea "github.com/charmbracelet/bubbletea"12 "github.com/muesli/termenv"13 )14 15-var (16- color = termenv.ColorProfile()15+var term = termenv.ColorProfile()17-)16 17+type errMsg error18+19-type Model struct {19+type model struct {20 spinner spinner.Model21 quitting bool22 err error23 }24 25-type errMsg error26-25 func 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 }32 35-func initialize() (tea.Model, tea.Cmd) {33+func initialModel() model {34 s := spinner.NewModel()35 s.Frames = spinner.Dot36+ return model{spinner: s}37+}38 39- return Model{40- spinner: s,41- }, spinner.Tick(s)39+func (m model) Init() tea.Cmd {40+ return spinner.Tick(m.spinner)41 }42 44-func update(msg tea.Msg, model tea.Model) (tea.Model, tea.Cmd) {45- m, ok := model.(Model)46- if !ok {47- return model, nil48- }49-43+func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {44 switch msg := msg.(type) {45 46 case tea.KeyMsg:47 switch msg.String() {19 unmodified lines67 }68 69 }70 77-func 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- }71+func (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 linesmodified:examples/textinput/main.go+18 added-34 removed
2 unmodified lines3 // A simple program demonstrating the text input component from the Bubbles4 // component library.5 6 import (7- "errors"7 "fmt"8 "log"9 10 input "github.com/charmbracelet/bubbles/textinput"11 tea "github.com/charmbracelet/bubbletea"12 )13 15-type Model struct {16- textInput input.Model17- err error18-}19-20-type tickMsg struct{}21-type errMsg error22-14 func main() {24- p := tea.NewProgram(25- initialize,26- update,27- view,28- )15+ p := tea.NewProgram(initialModel())16 17 if err := p.Start(); err != nil {18 log.Fatal(err)19 }20 }21 35-func initialize() (tea.Model, tea.Cmd) {22+type tickMsg struct{}23+type errMsg error24+25+type model struct {26+ textInput input.Model27+ err error28+}29+30+func initialModel() model {31 inputModel := input.NewModel()32 inputModel.Placeholder = "Pikachu"33 inputModel.Focus()34 40- return Model{35+ return model{36 textInput: inputModel,37 err: nil,43- }, input.Blink(inputModel)38+ }39 }40 46-func update(msg tea.Msg, model tea.Model) (tea.Model, tea.Cmd) {41+func (m model) Init() tea.Cmd {42+ return input.Blink(m.textInput)43+}44+45+func (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- }47 48 switch msg := msg.(type) {49 case tea.KeyMsg:50 switch msg.Type {14 unmodified lines65 m.textInput, cmd = input.Update(msg, m.textInput)66 return m, cmd67 }68 79-func 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- }69+func (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 linesmodified:examples/textinputs/main.go+13 added-29 removed
10 unmodified lines11 tea "github.com/charmbracelet/bubbletea"12 te "github.com/muesli/termenv"13 )14 15-const (16- focusedTextColor = "205"15+const focusedTextColor = "205"17-)16 17 var (18 color = te.ColorProfile().Color19 focusedPrompt = te.String("> ").Foreground(color("205")).String()2 unmodified lines22 blurredSubmitButton = "[ " + te.String("Submit").Foreground(color("240")).String() + " ]"23 )24 25 func 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 lines36 emailInput input.Model37 submitButton string38 }39 46-func initialize() (tea.Model, tea.Cmd) {40+func initialModel() model {41 name := input.NewModel()42 name.Placeholder = "Name"43 name.Focus()44 name.Prompt = focusedPrompt6 unmodified lines51 email := input.NewModel()52 email.Placeholder = "Email"53 email.Prompt = blurredPrompt54 61- 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}56 57 }58+func (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+}65 70-func 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- }75-66+func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {67 var cmd tea.Cmd68 69 switch msg := msg.(type) {79-70 case tea.KeyMsg:71 switch msg.String() {72 73 case "ctrl+c":82 unmodified lines156 157 return m, tea.Batch(cmds...)158 }159 170-func view(mdl tea.Model) string {171- m, ok := mdl.(model)172- if !ok {173- return "could not perform assertion on model"174- }175-160+func (m model) View() string {161 s := "\n"162 163 inputs := []string{164 input.View(m.nameInput),8 unmodified lines173 }174 }175 176 s += "\n\n" + m.submitButton + "\n"192-177 return s178 }