Update views example to use new 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 816428c · Browse files · 1 file·+39 added−51 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+39 added−51 removed
- other:+39 added−51 removed
Provenance
- Commit
7507f3d- Tree
924d1cd- Author
Christian Rocha <christian@rocha.is>- Committer
Christian Rocha <christian@rocha.is>- Parent
816428c
1 file changed+39 added−51 removed
modified:examples/views/main.go+39 added-51 removed
33 unmodified lines34 ramp = makeRamp("#B14FFF", "#00FFA3", progressBarWidth)35 )36 37 func main() {38- p := tea.NewProgram(39- initialize,40- update,41- view,42- )38+ initialModel := model{0, false, 10, 0, 0, false, false}39+ p := tea.NewProgram(initialModel)40 if err := p.Start(); err != nil {41 fmt.Println("could not start program:", err)42 }43 }44 48-// MESSAGES49-45 type tickMsg struct{}46 type frameMsg struct{}47 53-// MODEL54-55-type model struct {56- Choice int57- Chosen bool58- Ticks int59- Frames int60- Progress float6461- Loaded bool62- Quitting bool63-}64-65-func initialize() (tea.Model, tea.Cmd) {66- return model{0, false, 10, 0, 0, false, false}, tick()67-}68-69-// CMDS70-48 func tick() tea.Cmd {49 return tea.Tick(time.Second, func(time.Time) tea.Msg {50 return tickMsg{}51 })4 unmodified lines56 return frameMsg{}57 })58 }59 83-// UPDATE60+type model struct {61+ Choice int62+ Chosen bool63+ Ticks int64+ Frames int65+ Progress float6466+ Loaded bool67+ Quitting bool68+}69 85-// Main update function.86-func update(msg tea.Msg, mdl tea.Model) (tea.Model, tea.Cmd) {87- m, _ := mdl.(model)70+func (m model) Init() tea.Cmd {71+ return tick()72+}73 74+// Main update function.75+func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {76 // Make sure these keys always quit77 if msg, ok := msg.(tea.KeyMsg); ok {78 k := msg.String()79 if k == "q" || k == "esc" || k == "ctrl+c" {9 unmodified lines89 }90 return updateChosen(msg, m)91 }92 93+// The main view, which just calls the approprate sub-view94+func (m model) View() string {95+ var s string96+ if m.Quitting {97+ return "\n See you later!\n\n"98+ }99+ if !m.Chosen {100+ s = choicesView(m)101+ } else {102+ s = chosenView(m)103+ }104+ return indent.String("\n"+s+"\n\n", 2)105+}106+107+// Sub-update functions108+109 // Update loop for the first view where you're choosing a task.110 func updateChoices(msg tea.Msg, m model) (tea.Model, tea.Cmd) {111 switch msg := msg.(type) {112 56 unmodified lines169 170 return m, nil171 }172 170-// VIEW173+// Sub-views174 172-// The main view, which just calls the approprate sub-view173-func view(mdl tea.Model) string {174- m, _ := mdl.(model)175- var s string176- if m.Quitting {177- return "\n See you later!\n\n"178- }179- if !m.Chosen {180- s = choicesView(m)181- } else {182- s = chosenView(m)183- }184- return indent.String("\n"+s+"\n\n", 2)185-}186-175 // The first view, where you're choosing a task176 func choicesView(m model) string {177 c := m.Choice178 57 unmodified lines236 237 return fmt.Sprintf("%s%s %3.0f", fullCells, emptyCells, math.Round(percent*100))238 }239 252-// UTILS240+// Utils241 242 // Color a string's foreground with the given value.243 func colorFg(val, color string) string {244 return termenv.String(val).Foreground(term.Color(color)).String()30 unmodified lines275 }276 277 // Helper function for converting colors to hex. Assumes a value between 0 and278 // 1.291-func colorFloatToHex(f float64) string {292- result := strconv.FormatInt(int64(f*255), 16)293- if len(result) == 1 {294- result = "0" + result279+func colorFloatToHex(f float64) (s string) {280+ s = strconv.FormatInt(int64(f*255), 16)281+ if len(s) == 1 {282+ s = "0" + s283 }296- return result284+ return285 }