Skip to content
Myr

main.go

This file

Path

examples/eyes/main.go

197 lines · 4.3 KiB · Go

Last changed by

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

History of this file

Take it

docs: blinking eyes (#1353)Carlos Alexandro Beckerbefore adoptione5fb1b8

lines 1–10why, one step back
1// roughly converted to Go from https://github.com/dmtrKovalenko/esp32-smooth-eye-blinking/blob/main/src/main.cpp
2package main34import (5 "fmt"6 "math"7 "math/rand"8 "strings"9 "time"10

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

lines 11–12why, one step back
11 tea "charm.land/bubbletea/v2"
12 "charm.land/lipgloss/v2"

docs: blinking eyes (#1353)Carlos Alexandro Beckerbefore adoptione5fb1b8

lines 13–46why, one step back
13)
1415const (16 // Eye dimensions (corresponding to original EYE_WIDTH and EYE_HEIGHT)17 eyeWidth = 1518 eyeHeight = 12 // Increased height for taller eyes19 eyeSpacing = 402021 // Blink animation timing (matching original constants)22 blinkFrames = 2023 openTimeMin = 100024 openTimeMax = 400025)2627// Characters for drawing the eyes28const (29 eyeChar = "●"30 bgChar = " "31)3233type model struct {34 width int35 height int36 eyePositions [2]int37 eyeY int38 isBlinking bool39 blinkState int40 lastBlink time.Time41 openTime time.Duration42}4344type tickMsg time.Time4546func main() {

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

line 47why, one step back
47 p := tea.NewProgram(initialModel())

docs: blinking eyes (#1353)Carlos Alexandro Beckerbefore adoptione5fb1b8

lines 48–75why, one step back
48 if _, err := p.Run(); err != nil {
49 fmt.Printf("Error running program: %v\n", err)50 }51}5253func initialModel() model {54 m := model{55 width: 80,56 height: 24,57 isBlinking: false,58 blinkState: 0,59 lastBlink: time.Now(),60 openTime: time.Duration(rand.Intn(openTimeMax-openTimeMin)+openTimeMin) * time.Millisecond,61 }6263 m.updateEyePositions()64 return m65}6667func (m *model) updateEyePositions() {68 startX := (m.width - eyeSpacing) / 269 m.eyeY = m.height / 27071 m.eyePositions[0] = startX72 m.eyePositions[1] = startX + eyeSpacing73}7475func (m model) Init() tea.Cmd {

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

line 76why, one step back
76 return tickCmd()

docs: blinking eyes (#1353)Carlos Alexandro Beckerbefore adoptione5fb1b8

lines 77–87why, one step back
77}
7879func tickCmd() tea.Cmd {80 return tea.Tick(50*time.Millisecond, func(t time.Time) tea.Msg {81 return tickMsg(t)82 })83}8485func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {86 switch msg := msg.(type) {87 case tea.KeyMsg:

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

lines 88–89why, one step back
88 switch msg.String() {
89 case "ctrl+c", "esc":

docs: blinking eyes (#1353)Carlos Alexandro Beckerbefore adoptione5fb1b8

lines 90–124why, one step back
90 return m, tea.Quit
91 }9293 case tea.WindowSizeMsg:94 m.width = msg.Width95 m.height = msg.Height96 m.updateEyePositions()9798 case tickMsg:99 currentTime := time.Now()100101 if !m.isBlinking && currentTime.Sub(m.lastBlink) >= m.openTime {102 m.isBlinking = true103 m.blinkState = 0104 }105106 if m.isBlinking {107 m.blinkState++108109 if m.blinkState >= blinkFrames {110 m.isBlinking = false111 m.lastBlink = currentTime112 m.openTime = time.Duration(rand.Intn(openTimeMax-openTimeMin)+openTimeMin) * time.Millisecond113114 // 10% chance of double blink (matching original logic)115 if rand.Intn(10) == 0 {116 m.openTime = 300 * time.Millisecond117 }118 }119 }120 }121122 return m, tickCmd()123}124

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

lines 125–128why, one step back
125func (m model) View() tea.View {
126 var v tea.View127 v.AltScreen = true // Use alternate screen buffer128

docs: blinking eyes (#1353)Carlos Alexandro Beckerbefore adoptione5fb1b8

lines 129–173why, one step back
129 // Create empty canvas
130 canvas := make([][]string, m.height)131 for y := range canvas {132 canvas[y] = make([]string, m.width)133 for x := range canvas[y] {134 canvas[y][x] = bgChar135 }136 }137138 // Calculate current eye height based on blink state139 currentHeight := eyeHeight140 if m.isBlinking {141 var blinkProgress float64142143 if m.blinkState < blinkFrames/2 {144 // Closing eyes (with easing function from original)145 blinkProgress = float64(m.blinkState) / float64(blinkFrames/2)146 blinkProgress = 1.0 - (blinkProgress * blinkProgress)147 } else {148 // Opening eyes (with easing function from original)149 blinkProgress = float64(m.blinkState-blinkFrames/2) / float64(blinkFrames/2)150 blinkProgress = blinkProgress * (2.0 - blinkProgress)151 }152153 currentHeight = int(math.Max(1, float64(eyeHeight)*blinkProgress))154 }155156 // Draw both eyes157 for i := 0; i < 2; i++ {158 drawEllipse(canvas, m.eyePositions[i], m.eyeY, eyeWidth, currentHeight)159 }160161 // Convert canvas to string162 var s strings.Builder163 for _, row := range canvas {164 for _, cell := range row {165 s.WriteString(cell)166 }167 s.WriteString("\n")168 }169170 // Style output171 style := lipgloss.NewStyle().172 Foreground(lipgloss.Color("#F0F0F0"))173

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

lines 174–176why, one step back
174 v.SetContent(style.Render(s.String()))
175176 return v

docs: blinking eyes (#1353)Carlos Alexandro Beckerbefore adoptione5fb1b8

lines 177–197why, one step back
177}
178179func drawEllipse(canvas [][]string, x0, y0, rx, ry int) {180 // Improved ellipse drawing algorithm with better angles181 for y := -ry; y <= ry; y++ {182 // Calculate the width at this y position for a smoother ellipse183 // Use a slightly modified formula to improve the angles184 width := int(float64(rx) * math.Sqrt(1.0-math.Pow(float64(y)/float64(ry), 2.0)))185186 for x := -width; x <= width; x++ {187 // Calculate canvas position188 canvasX := x0 + x189 canvasY := y0 + y190191 // Make sure we're within canvas bounds192 if canvasX >= 0 && canvasX < len(canvas[0]) && canvasY >= 0 && canvasY < len(canvas) {193 canvas[canvasY][canvasX] = eyeChar194 }195 }196 }197}