Skip to content
Myr

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

2 files+208 added−208 removed

deleted:example/main.go-208 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 {15 Choice int16 Chosen bool17 Ticks int18 Frames int19 Progress float6420 Loaded bool21}2223type tickMsg struct{}2425type frameMsg struct{}2627func 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}3839// SUBSCRIPTIONS4041func tick(model tea.Model) tea.Msg {42 time.Sleep(time.Second)43 return tickMsg{}44}4546func frame(model tea.Model) tea.Msg {47 time.Sleep(time.Second / 16)48 return frameMsg{}49}5051// UPDATES5253func update(msg tea.Msg, model tea.Model) (tea.Model, tea.Cmd) {54 m, _ := model.(Model)5556 if !m.Chosen {57 return updateChoices(msg, m)58 }59 return updateChosen(msg, m)60}6162func updateChoices(msg tea.Msg, m Model) (tea.Model, tea.Cmd) {63 switch msg := msg.(type) {6465 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 }9192 case tickMsg:93 if m.Ticks == 0 {94 return m, tea.Quit95 }96 m.Ticks -= 197 }9899 return m, nil100}101102func updateChosen(msg tea.Msg, m Model) (tea.Model, tea.Cmd) {103 switch msg := msg.(type) {104105 case tea.KeyPressMsg:106 switch msg {107 case "q":108 fallthrough109 case "esc":110 fallthrough111 case "ctrl+c":112 return m, tea.Quit113 }114115 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 }125126 case tickMsg:127 if m.Loaded {128 if m.Ticks == 0 {129 return m, tea.Quit130 }131 m.Ticks -= 1132 }133 }134135 return m, nil136}137138// VIEWS139140func view(model tea.Model) string {141 m, _ := model.(Model)142 if !m.Chosen {143 return choicesView(m)144 }145 return chosenView(m)146}147148const choicesTpl = `What to do today?149150%s151152Program quits in %d seconds.153154(press j/k or up/down to select, enter to choose, and q or esc to quit)`155156func choicesView(m Model) string {157 c := m.Choice158159 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 )166167 return fmt.Sprintf(choicesTpl, choices, m.Ticks)168}169170func chosenView(m Model) string {171 var msg string172173 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 }183184 label := "Downloading..."185 if m.Loaded {186 label = fmt.Sprintf("Downloaded. Exiting in %d...", m.Ticks)187 }188189 return msg + "\n\n\n\n\n\n " + label + "\n" + progressbar(80, m.Progress) + "%"190}191192func checkbox(label string, checked bool) string {193 check := " "194 if checked {195 check = "x"196 }197 return fmt.Sprintf("[%s] %s", check, label)198}199200func 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
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 {15 Choice int16 Chosen bool17 Ticks int18 Frames int19 Progress float6420 Loaded bool21}2223type tickMsg struct{}2425type frameMsg struct{}2627func 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}3839// SUBSCRIPTIONS4041func tick(model tea.Model) tea.Msg {42 time.Sleep(time.Second)43 return tickMsg{}44}4546func frame(model tea.Model) tea.Msg {47 time.Sleep(time.Second / 60)48 return frameMsg{}49}5051// UPDATES5253func update(msg tea.Msg, model tea.Model) (tea.Model, tea.Cmd) {54 m, _ := model.(Model)5556 if !m.Chosen {57 return updateChoices(msg, m)58 }59 return updateChosen(msg, m)60}6162func updateChoices(msg tea.Msg, m Model) (tea.Model, tea.Cmd) {63 switch msg := msg.(type) {6465 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 }9192 case tickMsg:93 if m.Ticks == 0 {94 return m, tea.Quit95 }96 m.Ticks -= 197 }9899 return m, nil100}101102func updateChosen(msg tea.Msg, m Model) (tea.Model, tea.Cmd) {103 switch msg := msg.(type) {104105 case tea.KeyPressMsg:106 switch msg {107 case "q":108 fallthrough109 case "esc":110 fallthrough111 case "ctrl+c":112 return m, tea.Quit113 }114115 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 }125126 case tickMsg:127 if m.Loaded {128 if m.Ticks == 0 {129 return m, tea.Quit130 }131 m.Ticks -= 1132 }133 }134135 return m, nil136}137138// VIEWS139140func view(model tea.Model) string {141 m, _ := model.(Model)142 if !m.Chosen {143 return choicesView(m)144 }145 return chosenView(m)146}147148const choicesTpl = `What to do today?149150%s151152Program quits in %d seconds.153154(press j/k or up/down to select, enter to choose, and q or esc to quit)`155156func choicesView(m Model) string {157 c := m.Choice158159 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 )166167 return fmt.Sprintf(choicesTpl, choices, m.Ticks)168}169170func chosenView(m Model) string {171 var msg string172173 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 }183184 label := "Downloading..."185 if m.Loaded {186 label = fmt.Sprintf("Downloaded. Exiting in %d...", m.Ticks)187 }188189 return msg + "\n\n\n\n\n\n " + label + "\n" + progressbar(80, m.Progress) + "%"190}191192func checkbox(label string, checked bool) string {193 check := " "194 if checked {195 check = "x"196 }197 return fmt.Sprintf("[%s] %s", check, label)198}199200func 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}