Update views example to use new Model interface-based structure

This commit is contained in:
Christian Rocha 2020-10-15 16:30:34 -04:00
parent 816428c135
commit 7507f3d805
No known key found for this signature in database
GPG Key ID: D6CC7A16E5878018
1 changed files with 39 additions and 51 deletions

View File

@ -35,39 +35,16 @@ var (
) )
func main() { func main() {
p := tea.NewProgram( initialModel := model{0, false, 10, 0, 0, false, false}
initialize, p := tea.NewProgram(initialModel)
update,
view,
)
if err := p.Start(); err != nil { if err := p.Start(); err != nil {
fmt.Println("could not start program:", err) fmt.Println("could not start program:", err)
} }
} }
// MESSAGES
type tickMsg struct{} type tickMsg struct{}
type frameMsg struct{} type frameMsg struct{}
// MODEL
type model struct {
Choice int
Chosen bool
Ticks int
Frames int
Progress float64
Loaded bool
Quitting bool
}
func initialize() (tea.Model, tea.Cmd) {
return model{0, false, 10, 0, 0, false, false}, tick()
}
// CMDS
func tick() tea.Cmd { func tick() tea.Cmd {
return tea.Tick(time.Second, func(time.Time) tea.Msg { return tea.Tick(time.Second, func(time.Time) tea.Msg {
return tickMsg{} return tickMsg{}
@ -80,12 +57,22 @@ func frame() tea.Cmd {
}) })
} }
// UPDATE type model struct {
Choice int
Chosen bool
Ticks int
Frames int
Progress float64
Loaded bool
Quitting bool
}
func (m model) Init() tea.Cmd {
return tick()
}
// Main update function. // Main update function.
func update(msg tea.Msg, mdl tea.Model) (tea.Model, tea.Cmd) { func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m, _ := mdl.(model)
// Make sure these keys always quit // Make sure these keys always quit
if msg, ok := msg.(tea.KeyMsg); ok { if msg, ok := msg.(tea.KeyMsg); ok {
k := msg.String() k := msg.String()
@ -103,6 +90,22 @@ func update(msg tea.Msg, mdl tea.Model) (tea.Model, tea.Cmd) {
return updateChosen(msg, m) return updateChosen(msg, m)
} }
// The main view, which just calls the approprate sub-view
func (m model) View() string {
var s string
if m.Quitting {
return "\n See you later!\n\n"
}
if !m.Chosen {
s = choicesView(m)
} else {
s = chosenView(m)
}
return indent.String("\n"+s+"\n\n", 2)
}
// Sub-update functions
// Update loop for the first view where you're choosing a task. // Update loop for the first view where you're choosing a task.
func updateChoices(msg tea.Msg, m model) (tea.Model, tea.Cmd) { func updateChoices(msg tea.Msg, m model) (tea.Model, tea.Cmd) {
switch msg := msg.(type) { switch msg := msg.(type) {
@ -167,22 +170,7 @@ func updateChosen(msg tea.Msg, m model) (tea.Model, tea.Cmd) {
return m, nil return m, nil
} }
// VIEW // Sub-views
// The main view, which just calls the approprate sub-view
func view(mdl tea.Model) string {
m, _ := mdl.(model)
var s string
if m.Quitting {
return "\n See you later!\n\n"
}
if !m.Chosen {
s = choicesView(m)
} else {
s = chosenView(m)
}
return indent.String("\n"+s+"\n\n", 2)
}
// The first view, where you're choosing a task // The first view, where you're choosing a task
func choicesView(m model) string { func choicesView(m model) string {
@ -249,7 +237,7 @@ func progressbar(width int, percent float64) string {
return fmt.Sprintf("%s%s %3.0f", fullCells, emptyCells, math.Round(percent*100)) return fmt.Sprintf("%s%s %3.0f", fullCells, emptyCells, math.Round(percent*100))
} }
// UTILS // Utils
// Color a string's foreground with the given value. // Color a string's foreground with the given value.
func colorFg(val, color string) string { func colorFg(val, color string) string {
@ -288,10 +276,10 @@ func colorToHex(c colorful.Color) string {
// Helper function for converting colors to hex. Assumes a value between 0 and // Helper function for converting colors to hex. Assumes a value between 0 and
// 1. // 1.
func colorFloatToHex(f float64) string { func colorFloatToHex(f float64) (s string) {
result := strconv.FormatInt(int64(f*255), 16) s = strconv.FormatInt(int64(f*255), 16)
if len(result) == 1 { if len(s) == 1 {
result = "0" + result s = "0" + s
} }
return result return
} }