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
Provenance
- Commit
b6c8792- Tree
a9c2f9a- Author
Christian Rocha <christian@rocha.is>- Committer
Christian Rocha <christian@rocha.is>- Parent
edb6c58
4 files changed+112 added−21 removed
modified:example/main.go+104 added-20 removed
1 package main2 3 import (4 "fmt"5+ "math"6+ "strings"7 "tea"8 "time"9+10+ "github.com/fogleman/ease"11 )12 13 // Model contains the data for our application.14 type Model struct {11- Choice int12- Ticks int15+ Choice int16+ Chosen bool17+ Ticks int18+ Frames int19+ Progress float6420 }21 15-// TickMsg signals that the timer has ticked16-type TickMsg struct{}22+type tickMsg struct{}23 24+type frameMsg struct{}25+26 func 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 }37 30-// Update. Triggered when new messages arrive.38+// SUBSCRIPTIONS39+40+func tick(model tea.Model) tea.Msg {41+ time.Sleep(time.Second)42+ return tickMsg{}43+}44+45+func frame(model tea.Model) tea.Msg {46+ time.Sleep(time.Second / 16)47+ return frameMsg{}48+}49+50+// UPDATES51+52 func update(msg tea.Msg, model tea.Model) (tea.Model, tea.Cmd) {53 m, _ := model.(Model)54 55+ if !m.Chosen {56+ return updateChoices(msg, m)57+ }58+ return updateChosen(msg, m)59+}60+61+func updateChoices(msg tea.Msg, m Model) (tea.Model, tea.Cmd) {62 switch msg := msg.(type) {63 64 case tea.KeyPressMsg:65 switch msg {10 unmodified lines76 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 }90 60- case TickMsg:91+ case tickMsg:92 if m.Ticks == 0 {93 return m, tea.Quit94 }95 m.Ticks -= 11 unmodified line97 98 return m, nil99 }100 70-// Subscription71-func tick(_ tea.Model) tea.Msg {72- time.Sleep(time.Second)73- return TickMsg{}101+func updateChosen(msg tea.Msg, m Model) (tea.Model, tea.Cmd) {102+ switch msg := msg.(type) {103+104+ case tea.KeyPressMsg:105+ switch msg {106+ case "q":107+ fallthrough108+ case "esc":109+ fallthrough110+ case "ctrl+c":111+ return m, tea.Quit112+ }113+114+ 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, nil121+122+ }123+124+ return m, nil125 }126 76-// View template77-const tpl = `What to do today?127+// VIEWS128 129+func view(model tea.Model) string {130+ m, _ := model.(Model)131+ if !m.Chosen {132+ return choicesView(m)133+ }134+ return chosenView(m)135+}136+137+const choicesTpl = `What to do today?138+139 %s140 141 Program quits in %d seconds.142 83-(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)`144 85-// View function. Called after an Update().86-func view(model tea.Model) string {145+func choicesView(m Model) string {87- m, _ := model.(Model)146 c := m.Choice147 148 choices := fmt.Sprintf(149 "%s\n%s\n%s\n%s",2 unmodified lines152 checkbox("Read something", c == 2),153 checkbox("See friends", c == 3),154 )155 98- return fmt.Sprintf(tpl, choices, m.Ticks)156+ return fmt.Sprintf(choicesTpl, choices, m.Ticks)157 }158 101-// Checkbox widget159+func chosenView(m Model) string {160+ var msg string161+162+ 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+ }172+173+ return "\n" + msg + "\n\n\n\n\n Downloading...\n" + progressbar(80, m.Progress) + "%"174+}175+176 func checkbox(label string, checked bool) string {177 check := " "178 if checked {179 check = "x"180 }181 return fmt.Sprintf("[%s] %s", check, label)182 }183+184+func 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 line2 3 go 1.134 5 require (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
1+github.com/fogleman/ease v0.0.0-20170301025033-8da417bf1776 h1:VRIbnDWRmAh5yBdz+J6yFMF5vso1It6vn+WmM/5l7MA=2+github.com/fogleman/ease v0.0.0-20170301025033-8da417bf1776/go.mod h1:9wvnDu3YOfxzWM9Cst40msBF1C2UdQgDv962oTxSuMs=3 github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=4 github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=5 github.com/pkg/term v0.0.0-20190109203006-aa71e9d9e942 h1:A7GG7zcGjl3jqAqGPmcNjd/D9hzL95SuoOQAaFNdLU0=6 github.com/pkg/term v0.0.0-20190109203006-aa71e9d9e942/go.mod h1:eCbImbZ95eXtAUIbLAuAVnBnwf83mjf6QIVH8SHYwqQ=9 unmodified linesmodified:key.go+5 added-1 removed
9 unmodified lines10 type KeyPressMsg string11 12 // Control keys13 const (14- keyETX = 3 // ctrl+c14+ keyETX = 3 // break, ctrl+c15+ keyLF = 9 // line-feed, \n16+ keyCR = 13 // carriage return, \r17 keyESC = 2718 keyUS = 3119 )20 21 var 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