Skip to content
Myr

main.go

This file

Path

examples/cellbuffer/main.go

199 lines · 3.7 KiB · Go

Last changed by

docs(examples): add cellbuffer + harmonica example (#409)Christian Rochabefore adoption3765727

History of this file

Take it

1package main23// 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"1213	tea "github.com/charmbracelet/bubbletea"14	"github.com/charmbracelet/harmonica"15)1617const (18	fps       = 6019	frequency = 7.520	damping   = 0.1521)2223func drawEllipse(cb *cellbuffer, xc, yc, rx, ry float64) {24	const c = "*"25	var (26		dx, dy, d1, d2 float6427		x              float64 = 028		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(c, int(x+xc), int(y+yc))37		cb.set(c, int(-x+xc), int(y+yc))38		cb.set(c, int(x+xc), int(-y+yc))39		cb.set(c, 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(c, int(x+xc), int(y+yc))57		cb.set(c, int(-x+xc), int(y+yc))58		cb.set(c, int(x+xc), int(-y+yc))59		cb.set(c, 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(v string, 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] = v94}9596func (c *cellbuffer) clear(x, y int) {97	c.set(" ", x, y)98}99100func (c *cellbuffer) wipe() {101	for i := range c.cells {102		c.cells[i] = " "103	}104}105106func (c cellbuffer) width() int {107	return c.stride108}109110func (c cellbuffer) height() int {111	h := len(c.cells) / c.stride112	if len(c.cells)%c.stride != 0 {113		h++114	}115	return h116}117118func (c cellbuffer) ready() bool {119	return len(c.cells) > 0120}121122func (c cellbuffer) String() string {123	var b strings.Builder124	for i := 0; i < len(c.cells); i++ {125		if i > 0 && i%c.stride == 0 && i < len(c.cells)-1 {126			b.WriteRune('\n')127		}128		b.WriteString(c.cells[i])129	}130	return b.String()131}132133type frameMsg struct{}134135func animate() tea.Cmd {136	return tea.Tick(time.Second/fps, func(_ time.Time) tea.Msg {137		return frameMsg{}138	})139}140141type model struct {142	cells                cellbuffer143	spring               harmonica.Spring144	targetX, targetY     float64145	x, y                 float64146	xVelocity, yVelocity float64147}148149func (m model) Init() tea.Cmd {150	return animate()151}152153func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {154	switch msg := msg.(type) {155	case tea.KeyMsg:156		return m, tea.Quit157	case tea.WindowSizeMsg:158		if !m.cells.ready() {159			m.targetX, m.targetY = float64(msg.Width)/2, float64(msg.Height)/2160		}161		m.cells.init(msg.Width, msg.Height)162		return m, nil163	case tea.MouseMsg:164		if !m.cells.ready() {165			return m, nil166		}167		m.targetX, m.targetY = float64(msg.X), float64(msg.Y)168		return m, nil169170	case frameMsg:171		if !m.cells.ready() {172			return m, nil173		}174175		m.cells.wipe()176		m.x, m.xVelocity = m.spring.Update(m.x, m.xVelocity, m.targetX)177		m.y, m.yVelocity = m.spring.Update(m.y, m.yVelocity, m.targetY)178		drawEllipse(&m.cells, m.x, m.y, 16, 8)179		return m, animate()180	default:181		return m, nil182	}183}184185func (m model) View() string {186	return m.cells.String()187}188189func main() {190	m := model{191		spring: harmonica.NewSpring(harmonica.FPS(fps), frequency, damping),192	}193194	p := tea.NewProgram(m, tea.WithAltScreen(), tea.WithMouseCellMotion())195	if _, err := p.Run(); err != nil {196		fmt.Println("Uh oh:", err)197		os.Exit(1)198	}199}