Skip to content
Myr

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

1 file+39 added−51 removed

modified:examples/views/main.go+39 added-51 removed
33 unmodified lines
34 ramp = makeRamp("#B14FFF", "#00FFA3", progressBarWidth)35)3637func 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}4448// MESSAGES4945type tickMsg struct{}46type frameMsg struct{}4753// MODEL5455type model struct {56 Choice int57 Chosen bool58 Ticks int59 Frames int60 Progress float6461 Loaded bool62 Quitting bool63}6465func initialize() (tea.Model, tea.Cmd) {66 return model{0, false, 10, 0, 0, false, false}, tick()67}6869// CMDS7048func tick() tea.Cmd {49 return tea.Tick(time.Second, func(time.Time) tea.Msg {50 return tickMsg{}51 })
4 unmodified lines
56 return frameMsg{}57 })58}5983// UPDATE60type model struct {61 Choice int62 Chosen bool63 Ticks int64 Frames int65 Progress float6466 Loaded bool67 Quitting bool68}6985// Main update function.86func update(msg tea.Msg, mdl tea.Model) (tea.Model, tea.Cmd) {87 m, _ := mdl.(model)70func (m model) Init() tea.Cmd {71 return tick()72}7374// Main update function.75func (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 lines
89 }90 return updateChosen(msg, m)91}9293// The main view, which just calls the approprate sub-view94func (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}106107// Sub-update functions108109// Update loop for the first view where you're choosing a task.110func updateChoices(msg tea.Msg, m model) (tea.Model, tea.Cmd) {111 switch msg := msg.(type) {112
56 unmodified lines
169170 return m, nil171}172170// VIEW173// Sub-views174172// The main view, which just calls the approprate sub-view173func 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}186175// The first view, where you're choosing a task176func choicesView(m model) string {177 c := m.Choice178
57 unmodified lines
236237 return fmt.Sprintf("%s%s %3.0f", fullCells, emptyCells, math.Round(percent*100))238}239252// UTILS240// Utils241242// Color a string's foreground with the given value.243func colorFg(val, color string) string {244 return termenv.String(val).Foreground(term.Color(color)).String()
30 unmodified lines
275}276277// Helper function for converting colors to hex. Assumes a value between 0 and278// 1.291func colorFloatToHex(f float64) string {292 result := strconv.FormatInt(int64(f*255), 16)293 if len(result) == 1 {294 result = "0" + result279func 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}