Skip to content
Myr

main.go

This file

Path

examples/cellbuffer/main.go

204 lines · 3.8 KiB · Go

Last changed by

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

History of this file

Take it

The walk stopped after 200 commits: lines older than 3dfbcd9 point at it.

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 1–12why, one step back
1package main
23// A simple example demonstrating how to draw and animate on a cellular grid.4// Note that the cellbuffer implementation in this example does not support5// double-width runes.67import (8 "fmt"9 "os"10 "strings"11 "time"12

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

line 13why, one step back
13 tea "charm.land/bubbletea/v2"

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 14–119why, one step back
14 "github.com/charmbracelet/harmonica"
15)1617const (18 fps = 6019 frequency = 7.520 damping = 0.1521 asterisk = "*"22)2324func drawEllipse(cb *cellbuffer, xc, yc, rx, ry float64) {25 var (26 dx, dy, d1, d2 float6427 x float6428 y = ry29 )3031 d1 = ry*ry - rx*rx*ry + 0.25*rx*rx32 dx = 2 * ry * ry * x33 dy = 2 * rx * rx * y3435 for dx < dy {36 cb.set(int(x+xc), int(y+yc))37 cb.set(int(-x+xc), int(y+yc))38 cb.set(int(x+xc), int(-y+yc))39 cb.set(int(-x+xc), int(-y+yc))40 if d1 < 0 {41 x++42 dx = dx + (2 * ry * ry)43 d1 = d1 + dx + (ry * ry)44 } else {45 x++46 y--47 dx = dx + (2 * ry * ry)48 dy = dy - (2 * rx * rx)49 d1 = d1 + dx - dy + (ry * ry)50 }51 }5253 d2 = ((ry * ry) * ((x + 0.5) * (x + 0.5))) + ((rx * rx) * ((y - 1) * (y - 1))) - (rx * rx * ry * ry)5455 for y >= 0 {56 cb.set(int(x+xc), int(y+yc))57 cb.set(int(-x+xc), int(y+yc))58 cb.set(int(x+xc), int(-y+yc))59 cb.set(int(-x+xc), int(-y+yc))60 if d2 > 0 {61 y--62 dy = dy - (2 * rx * rx)63 d2 = d2 + (rx * rx) - dy64 } else {65 y--66 x++67 dx = dx + (2 * ry * ry)68 dy = dy - (2 * rx * rx)69 d2 = d2 + dx - dy + (rx * rx)70 }71 }72}7374type cellbuffer struct {75 cells []string76 stride int77}7879func (c *cellbuffer) init(w, h int) {80 if w == 0 {81 return82 }83 c.stride = w84 c.cells = make([]string, w*h)85 c.wipe()86}8788func (c cellbuffer) set(x, y int) {89 i := y*c.stride + x90 if i > len(c.cells)-1 || x < 0 || y < 0 || x >= c.width() || y >= c.height() {91 return92 }93 c.cells[i] = asterisk94}9596func (c *cellbuffer) wipe() {97 for i := range c.cells {98 c.cells[i] = " "99 }100}101102func (c cellbuffer) width() int {103 return c.stride104}105106func (c cellbuffer) height() int {107 h := len(c.cells) / c.stride108 if len(c.cells)%c.stride != 0 {109 h++110 }111 return h112}113114func (c cellbuffer) ready() bool {115 return len(c.cells) > 0116}117118func (c cellbuffer) String() string {119 var b strings.Builder

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

line 120why, one step back
120 for i := range c.cells {

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 121–150why, one step back
121 if i > 0 && i%c.stride == 0 && i < len(c.cells)-1 {
122 b.WriteRune('\n')123 }124 b.WriteString(c.cells[i])125 }126 return b.String()127}128129type frameMsg struct{}130131func animate() tea.Cmd {132 return tea.Tick(time.Second/fps, func(_ time.Time) tea.Msg {133 return frameMsg{}134 })135}136137type model struct {138 cells cellbuffer139 spring harmonica.Spring140 targetX, targetY float64141 x, y float64142 xVelocity, yVelocity float64143}144145func (m model) Init() tea.Cmd {146 return animate()147}148149func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {150 switch msg := msg.(type) {

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

line 151why, one step back
151 case tea.KeyPressMsg:

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 152–159why, one step back
152 return m, tea.Quit
153 case tea.WindowSizeMsg:154 if !m.cells.ready() {155 m.targetX, m.targetY = float64(msg.Width)/2, float64(msg.Height)/2156 }157 m.cells.init(msg.Width, msg.Height)158 return m, nil159 case tea.MouseMsg:

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

lines 160–164why, one step back
160 switch msg.(type) {
161 case tea.MouseClickMsg, tea.MouseMotionMsg:162 default:163 break164 }

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 165–167why, one step back
165 if !m.cells.ready() {
166 return m, nil167 }

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

lines 168–169why, one step back
168 mouse := msg.Mouse()
169 m.targetX, m.targetY = float64(mouse.X), float64(mouse.Y)

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 170–186why, one step back
170 return m, nil
171172 case frameMsg:173 if !m.cells.ready() {174 return m, nil175 }176177 m.cells.wipe()178 m.x, m.xVelocity = m.spring.Update(m.x, m.xVelocity, m.targetX)179 m.y, m.yVelocity = m.spring.Update(m.y, m.yVelocity, m.targetY)180 drawEllipse(&m.cells, m.x, m.y, 16, 8)181 return m, animate()182 default:183 return m, nil184 }185}186

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

lines 187–191why, one step back
187func (m model) View() tea.View {
188 v := tea.NewView(m.cells.String())189 v.AltScreen = true190 v.MouseMode = tea.MouseModeCellMotion191 return v

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 192–198why, one step back
192}
193194func main() {195 m := model{196 spring: harmonica.NewSpring(harmonica.FPS(fps), frequency, damping),197 }198

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

line 199why, one step back
199 p := tea.NewProgram(m)

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

line 200why, one step back
200 if _, err := p.Run(); err != nil {

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

line 201why, one step back
201 fmt.Fprintln(os.Stderr, "Uh oh:", err)

feat(render): skipping lines from previous frameKonstantin Antonovbefore adoption3dfbcd9

lines 202–204why, one step back
202 os.Exit(1)
203 }204}