Skip to content
Myr

Multi-view example with progress bar

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 edb6c58 · Browse files · 4 files+112 added−21 removed
This commit

State

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

Diff stats

4 files+112 added−21 removed

  • other:+112 added−21 removed

4 files+112 added−21 removed

modified:example/main.go+104 added-20 removed
1package main23import (4 "fmt"5 "math"6 "strings"7 "tea"8 "time"910 "github.com/fogleman/ease"11)1213// Model contains the data for our application.14type Model struct {11 Choice int12 Ticks int15 Choice int16 Chosen bool17 Ticks int18 Frames int19 Progress float6420}2115// TickMsg signals that the timer has ticked16type TickMsg struct{}22type tickMsg struct{}2324type frameMsg struct{}2526func main() {27 p := tea.NewProgram(20 Model{0, 10},28 Model{0, false, 10, 0, 0},29 update,30 view,23 []tea.Sub{tick},31 []tea.Sub{tick, frame},32 )33 if err := p.Start(); err != nil {34 fmt.Println("could not start program:", err)35 }36}3730// Update. Triggered when new messages arrive.38// SUBSCRIPTIONS3940func tick(model tea.Model) tea.Msg {41 time.Sleep(time.Second)42 return tickMsg{}43}4445func frame(model tea.Model) tea.Msg {46 time.Sleep(time.Second / 16)47 return frameMsg{}48}4950// UPDATES5152func update(msg tea.Msg, model tea.Model) (tea.Model, tea.Cmd) {53 m, _ := model.(Model)5455 if !m.Chosen {56 return updateChoices(msg, m)57 }58 return updateChosen(msg, m)59}6061func updateChoices(msg tea.Msg, m Model) (tea.Model, tea.Cmd) {62 switch msg := msg.(type) {6364 case tea.KeyPressMsg:65 switch msg {
10 unmodified lines
76 m.Choice -= 177 if m.Choice < 0 {78 m.Choice = 079 }80 case "enter":81 m.Chosen = true82 return m, nil83 case "q":84 fallthrough85 case "esc":86 fallthrough87 case "ctrl+c":88 return m, tea.Quit89 }9060 case TickMsg:91 case tickMsg:92 if m.Ticks == 0 {93 return m, tea.Quit94 }95 m.Ticks -= 1
1 unmodified line
9798 return m, nil99}10070// Subscription71func tick(_ tea.Model) tea.Msg {72 time.Sleep(time.Second)73 return TickMsg{}101func updateChosen(msg tea.Msg, m Model) (tea.Model, tea.Cmd) {102 switch msg := msg.(type) {103104 case tea.KeyPressMsg:105 switch msg {106 case "q":107 fallthrough108 case "esc":109 fallthrough110 case "ctrl+c":111 return m, tea.Quit112 }113114 case frameMsg:115 m.Frames += 1116 m.Progress = ease.OutBounce(float64(m.Frames) / float64(120))117 if m.Progress > 1 {118 m.Progress = 1119 }120 return m, nil121122 }123124 return m, nil125}12676// View template77const tpl = `What to do today?127// VIEWS128129func view(model tea.Model) string {130 m, _ := model.(Model)131 if !m.Chosen {132 return choicesView(m)133 }134 return chosenView(m)135}136137const choicesTpl = `What to do today?138139%s140141Program quits in %d seconds.14283(press j/k or up/down to select, q or esc to quit)`143(press j/k or up/down to select, enter to choose, and q or esc to quit)`14485// View function. Called after an Update().86func view(model tea.Model) string {145func choicesView(m Model) string {87 m, _ := model.(Model)146 c := m.Choice147148 choices := fmt.Sprintf(149 "%s\n%s\n%s\n%s",
2 unmodified lines
152 checkbox("Read something", c == 2),153 checkbox("See friends", c == 3),154 )15598 return fmt.Sprintf(tpl, choices, m.Ticks)156 return fmt.Sprintf(choicesTpl, choices, m.Ticks)157}158101// Checkbox widget159func chosenView(m Model) string {160 var msg string161162 switch m.Choice {163 case 0:164 msg = "Carrot planting?\n\nCool, we'll need libgarden and vegeutils..."165 case 1:166 msg = "A trip to the market?\n\nOkay, then we should install marketkit and libshopping..."167 case 2:168 msg = "Reading time?\n\nOkay, cool, then we’ll need a library. Yes, a literal library..."169 default:170 msg = "It’s always good to see friends.\n\nFetching social-skills and conversationutils..."171 }172173 return "\n" + msg + "\n\n\n\n\n Downloading...\n" + progressbar(80, m.Progress) + "%"174}175176func checkbox(label string, checked bool) string {177 check := " "178 if checked {179 check = "x"180 }181 return fmt.Sprintf("[%s] %s", check, label)182}183184func progressbar(width int, percent float64) string {185 metaChars := 7186 w := float64(width - metaChars)187 fullSize := int(math.Round(w * percent))188 emptySize := int(w) - fullSize189 fullCells := strings.Repeat("#", fullSize)190 emptyCells := strings.Repeat(".", emptySize)191 return fmt.Sprintf("|%s%s| %3.0f", fullCells, emptyCells, math.Round(percent*100))192}
modified:go.mod+1 added
1 unmodified line
23go 1.1345require (6 github.com/fogleman/ease v0.0.0-20170301025033-8da417bf17767 github.com/pkg/term v0.0.0-20190109203006-aa71e9d9e9428 github.com/tj/go-terminput v1.0.09)
modified:go.sum+2 added
1github.com/fogleman/ease v0.0.0-20170301025033-8da417bf1776 h1:VRIbnDWRmAh5yBdz+J6yFMF5vso1It6vn+WmM/5l7MA=2github.com/fogleman/ease v0.0.0-20170301025033-8da417bf1776/go.mod h1:9wvnDu3YOfxzWM9Cst40msBF1C2UdQgDv962oTxSuMs=3github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=4github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=5github.com/pkg/term v0.0.0-20190109203006-aa71e9d9e942 h1:A7GG7zcGjl3jqAqGPmcNjd/D9hzL95SuoOQAaFNdLU0=6github.com/pkg/term v0.0.0-20190109203006-aa71e9d9e942/go.mod h1:eCbImbZ95eXtAUIbLAuAVnBnwf83mjf6QIVH8SHYwqQ=
9 unmodified lines
modified:key.go+5 added-1 removed
9 unmodified lines
10type KeyPressMsg string1112// Control keys13const (14 keyETX = 3 // ctrl+c14 keyETX = 3 // break, ctrl+c15 keyLF = 9 // line-feed, \n16 keyCR = 13 // carriage return, \r17 keyESC = 2718 keyUS = 3119)2021var controlKeyNames = map[int]string{22 keyETX: "ctrl+c",23 keyLF: "enter",24 keyCR: "enter",25 keyESC: "esc",26 keyUS: "us",27}28
39 unmodified lines