Whoops, fix framerate in example so it running at 60fps
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 c7f1302 · Browse files · 2 files·+208 added−208 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+208 added−208 removed
- other:+208 added−208 removed
Provenance
- Commit
7f3506e- Tree
27561e9- Author
Christian Rocha <christian@rocha.is>- Committer
Christian Rocha <christian@rocha.is>- Parent
c7f1302
2 files changed+208 added−208 removed
deleted:example/main.go-208 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 {15- Choice int16- Chosen bool17- Ticks int18- Frames int19- Progress float6420- Loaded bool21-}22-23-type tickMsg struct{}24-25-type frameMsg struct{}26-27-func main() {28- p := tea.NewProgram(29- Model{0, false, 10, 0, 0, false},30- update,31- view,32- []tea.Sub{tick, frame},33- )34- if err := p.Start(); err != nil {35- fmt.Println("could not start program:", err)36- }37-}38-39-// SUBSCRIPTIONS40-41-func tick(model tea.Model) tea.Msg {42- time.Sleep(time.Second)43- return tickMsg{}44-}45-46-func frame(model tea.Model) tea.Msg {47- time.Sleep(time.Second / 16)48- return frameMsg{}49-}50-51-// UPDATES52-53-func update(msg tea.Msg, model tea.Model) (tea.Model, tea.Cmd) {54- m, _ := model.(Model)55-56- if !m.Chosen {57- return updateChoices(msg, m)58- }59- return updateChosen(msg, m)60-}61-62-func updateChoices(msg tea.Msg, m Model) (tea.Model, tea.Cmd) {63- switch msg := msg.(type) {64-65- case tea.KeyPressMsg:66- switch msg {67- case "j":68- fallthrough69- case "down":70- m.Choice += 171- if m.Choice > 3 {72- m.Choice = 373- }74- case "k":75- fallthrough76- case "up":77- m.Choice -= 178- if m.Choice < 0 {79- m.Choice = 080- }81- case "enter":82- m.Chosen = true83- return m, nil84- case "q":85- fallthrough86- case "esc":87- fallthrough88- case "ctrl+c":89- return m, tea.Quit90- }91-92- case tickMsg:93- if m.Ticks == 0 {94- return m, tea.Quit95- }96- m.Ticks -= 197- }98-99- return m, nil100-}101-102-func updateChosen(msg tea.Msg, m Model) (tea.Model, tea.Cmd) {103- switch msg := msg.(type) {104-105- case tea.KeyPressMsg:106- switch msg {107- case "q":108- fallthrough109- case "esc":110- fallthrough111- case "ctrl+c":112- return m, tea.Quit113- }114-115- case frameMsg:116- if !m.Loaded {117- m.Frames += 1118- m.Progress = ease.OutBounce(float64(m.Frames) / float64(60))119- if m.Progress >= 1 {120- m.Progress = 1121- m.Loaded = true122- m.Ticks = 3123- }124- }125-126- case tickMsg:127- if m.Loaded {128- if m.Ticks == 0 {129- return m, tea.Quit130- }131- m.Ticks -= 1132- }133- }134-135- return m, nil136-}137-138-// VIEWS139-140-func view(model tea.Model) string {141- m, _ := model.(Model)142- if !m.Chosen {143- return choicesView(m)144- }145- return chosenView(m)146-}147-148-const choicesTpl = `What to do today?149-150-%s151-152-Program quits in %d seconds.153-154-(press j/k or up/down to select, enter to choose, and q or esc to quit)`155-156-func choicesView(m Model) string {157- c := m.Choice158-159- choices := fmt.Sprintf(160- "%s\n%s\n%s\n%s",161- checkbox("Plant carrots", c == 0),162- checkbox("Go to the market", c == 1),163- checkbox("Read something", c == 2),164- checkbox("See friends", c == 3),165- )166-167- return fmt.Sprintf(choicesTpl, choices, m.Ticks)168-}169-170-func chosenView(m Model) string {171- var msg string172-173- switch m.Choice {174- case 0:175- msg = "Carrot planting?\n\nCool, we'll need libgarden and vegeutils..."176- case 1:177- msg = "A trip to the market?\n\nOkay, then we should install marketkit and libshopping..."178- case 2:179- msg = "Reading time?\n\nOkay, cool, then we’ll need a library. Yes, an actual library."180- default:181- msg = "It’s always good to see friends.\n\nFetching social-skills and conversationutils..."182- }183-184- label := "Downloading..."185- if m.Loaded {186- label = fmt.Sprintf("Downloaded. Exiting in %d...", m.Ticks)187- }188-189- return msg + "\n\n\n\n\n\n " + label + "\n" + progressbar(80, m.Progress) + "%"190-}191-192-func checkbox(label string, checked bool) string {193- check := " "194- if checked {195- check = "x"196- }197- return fmt.Sprintf("[%s] %s", check, label)198-}199-200-func progressbar(width int, percent float64) string {201- metaChars := 7202- w := float64(width - metaChars)203- fullSize := int(math.Round(w * percent))204- emptySize := int(w) - fullSize205- fullCells := strings.Repeat("#", fullSize)206- emptyCells := strings.Repeat(".", emptySize)207- return fmt.Sprintf("|%s%s| %3.0f", fullCells, emptyCells, math.Round(percent*100))208-}added:examples/views/main.go+208 added
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 {15+ Choice int16+ Chosen bool17+ Ticks int18+ Frames int19+ Progress float6420+ Loaded bool21+}22+23+type tickMsg struct{}24+25+type frameMsg struct{}26+27+func main() {28+ p := tea.NewProgram(29+ Model{0, false, 10, 0, 0, false},30+ update,31+ view,32+ []tea.Sub{tick, frame},33+ )34+ if err := p.Start(); err != nil {35+ fmt.Println("could not start program:", err)36+ }37+}38+39+// SUBSCRIPTIONS40+41+func tick(model tea.Model) tea.Msg {42+ time.Sleep(time.Second)43+ return tickMsg{}44+}45+46+func frame(model tea.Model) tea.Msg {47+ time.Sleep(time.Second / 60)48+ return frameMsg{}49+}50+51+// UPDATES52+53+func update(msg tea.Msg, model tea.Model) (tea.Model, tea.Cmd) {54+ m, _ := model.(Model)55+56+ if !m.Chosen {57+ return updateChoices(msg, m)58+ }59+ return updateChosen(msg, m)60+}61+62+func updateChoices(msg tea.Msg, m Model) (tea.Model, tea.Cmd) {63+ switch msg := msg.(type) {64+65+ case tea.KeyPressMsg:66+ switch msg {67+ case "j":68+ fallthrough69+ case "down":70+ m.Choice += 171+ if m.Choice > 3 {72+ m.Choice = 373+ }74+ case "k":75+ fallthrough76+ case "up":77+ m.Choice -= 178+ if m.Choice < 0 {79+ m.Choice = 080+ }81+ case "enter":82+ m.Chosen = true83+ return m, nil84+ case "q":85+ fallthrough86+ case "esc":87+ fallthrough88+ case "ctrl+c":89+ return m, tea.Quit90+ }91+92+ case tickMsg:93+ if m.Ticks == 0 {94+ return m, tea.Quit95+ }96+ m.Ticks -= 197+ }98+99+ return m, nil100+}101+102+func updateChosen(msg tea.Msg, m Model) (tea.Model, tea.Cmd) {103+ switch msg := msg.(type) {104+105+ case tea.KeyPressMsg:106+ switch msg {107+ case "q":108+ fallthrough109+ case "esc":110+ fallthrough111+ case "ctrl+c":112+ return m, tea.Quit113+ }114+115+ case frameMsg:116+ if !m.Loaded {117+ m.Frames += 1118+ m.Progress = ease.OutBounce(float64(m.Frames) / float64(120))119+ if m.Progress >= 1 {120+ m.Progress = 1121+ m.Loaded = true122+ m.Ticks = 3123+ }124+ }125+126+ case tickMsg:127+ if m.Loaded {128+ if m.Ticks == 0 {129+ return m, tea.Quit130+ }131+ m.Ticks -= 1132+ }133+ }134+135+ return m, nil136+}137+138+// VIEWS139+140+func view(model tea.Model) string {141+ m, _ := model.(Model)142+ if !m.Chosen {143+ return choicesView(m)144+ }145+ return chosenView(m)146+}147+148+const choicesTpl = `What to do today?149+150+%s151+152+Program quits in %d seconds.153+154+(press j/k or up/down to select, enter to choose, and q or esc to quit)`155+156+func choicesView(m Model) string {157+ c := m.Choice158+159+ choices := fmt.Sprintf(160+ "%s\n%s\n%s\n%s",161+ checkbox("Plant carrots", c == 0),162+ checkbox("Go to the market", c == 1),163+ checkbox("Read something", c == 2),164+ checkbox("See friends", c == 3),165+ )166+167+ return fmt.Sprintf(choicesTpl, choices, m.Ticks)168+}169+170+func chosenView(m Model) string {171+ var msg string172+173+ switch m.Choice {174+ case 0:175+ msg = "Carrot planting?\n\nCool, we'll need libgarden and vegeutils..."176+ case 1:177+ msg = "A trip to the market?\n\nOkay, then we should install marketkit and libshopping..."178+ case 2:179+ msg = "Reading time?\n\nOkay, cool, then we’ll need a library. Yes, an actual library."180+ default:181+ msg = "It’s always good to see friends.\n\nFetching social-skills and conversationutils..."182+ }183+184+ label := "Downloading..."185+ if m.Loaded {186+ label = fmt.Sprintf("Downloaded. Exiting in %d...", m.Ticks)187+ }188+189+ return msg + "\n\n\n\n\n\n " + label + "\n" + progressbar(80, m.Progress) + "%"190+}191+192+func checkbox(label string, checked bool) string {193+ check := " "194+ if checked {195+ check = "x"196+ }197+ return fmt.Sprintf("[%s] %s", check, label)198+}199+200+func progressbar(width int, percent float64) string {201+ metaChars := 7202+ w := float64(width - metaChars)203+ fullSize := int(math.Round(w * percent))204+ emptySize := int(w) - fullSize205+ fullCells := strings.Repeat("#", fullSize)206+ emptyCells := strings.Repeat(".", emptySize)207+ return fmt.Sprintf("|%s%s| %3.0f", fullCells, emptyCells, math.Round(percent*100))208+}