Skip to content
Myr

docs: blinking eyes (#1353)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* docs: blinking eyes Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com> * docs: refer ---------

authored by Carlos Alexandro Becker · committed by GitHub · parent f2333d7 · Browse files · 1 file+194 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>

Diff stats

1 file+194 added−0 removed

  • other:+194 added−0 removed

1 file+194 added−0 removed

added:examples/eyes/main.go+194 added
1// roughly converted to Go from https://github.com/dmtrKovalenko/esp32-smooth-eye-blinking/blob/main/src/main.cpp2package main34import (5 "fmt"6 "math"7 "math/rand"8 "strings"9 "time"1011 tea "github.com/charmbracelet/bubbletea"12 "github.com/charmbracelet/lipgloss"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() {47 p := tea.NewProgram(initialModel(), tea.WithAltScreen())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 {76 return tea.Batch(77 tickCmd(),78 tea.EnterAltScreen,79 )80}8182func tickCmd() tea.Cmd {83 return tea.Tick(50*time.Millisecond, func(t time.Time) tea.Msg {84 return tickMsg(t)85 })86}8788func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {89 switch msg := msg.(type) {90 case tea.KeyMsg:91 if msg.Type == tea.KeyCtrlC || msg.Type == tea.KeyEsc {92 return m, tea.Quit93 }9495 case tea.WindowSizeMsg:96 m.width = msg.Width97 m.height = msg.Height98 m.updateEyePositions()99100 case tickMsg:101 currentTime := time.Now()102103 if !m.isBlinking && currentTime.Sub(m.lastBlink) >= m.openTime {104 m.isBlinking = true105 m.blinkState = 0106 }107108 if m.isBlinking {109 m.blinkState++110111 if m.blinkState >= blinkFrames {112 m.isBlinking = false113 m.lastBlink = currentTime114 m.openTime = time.Duration(rand.Intn(openTimeMax-openTimeMin)+openTimeMin) * time.Millisecond115116 // 10% chance of double blink (matching original logic)117 if rand.Intn(10) == 0 {118 m.openTime = 300 * time.Millisecond119 }120 }121 }122 }123124 return m, tickCmd()125}126127func (m model) View() string {128 // Create empty canvas129 canvas := make([][]string, m.height)130 for y := range canvas {131 canvas[y] = make([]string, m.width)132 for x := range canvas[y] {133 canvas[y][x] = bgChar134 }135 }136137 // Calculate current eye height based on blink state138 currentHeight := eyeHeight139 if m.isBlinking {140 var blinkProgress float64141142 if m.blinkState < blinkFrames/2 {143 // Closing eyes (with easing function from original)144 blinkProgress = float64(m.blinkState) / float64(blinkFrames/2)145 blinkProgress = 1.0 - (blinkProgress * blinkProgress)146 } else {147 // Opening eyes (with easing function from original)148 blinkProgress = float64(m.blinkState-blinkFrames/2) / float64(blinkFrames/2)149 blinkProgress = blinkProgress * (2.0 - blinkProgress)150 }151152 currentHeight = int(math.Max(1, float64(eyeHeight)*blinkProgress))153 }154155 // Draw both eyes156 for i := 0; i < 2; i++ {157 drawEllipse(canvas, m.eyePositions[i], m.eyeY, eyeWidth, currentHeight)158 }159160 // Convert canvas to string161 var s strings.Builder162 for _, row := range canvas {163 for _, cell := range row {164 s.WriteString(cell)165 }166 s.WriteString("\n")167 }168169 // Style output170 style := lipgloss.NewStyle().171 Foreground(lipgloss.Color("#F0F0F0"))172173 return style.Render(s.String())174}175176func drawEllipse(canvas [][]string, x0, y0, rx, ry int) {177 // Improved ellipse drawing algorithm with better angles178 for y := -ry; y <= ry; y++ {179 // Calculate the width at this y position for a smoother ellipse180 // Use a slightly modified formula to improve the angles181 width := int(float64(rx) * math.Sqrt(1.0-math.Pow(float64(y)/float64(ry), 2.0)))182183 for x := -width; x <= width; x++ {184 // Calculate canvas position185 canvasX := x0 + x186 canvasY := y0 + y187188 // Make sure we're within canvas bounds189 if canvasX >= 0 && canvasX < len(canvas[0]) && canvasY >= 0 && canvasY < len(canvas) {190 canvas[canvasY][canvasX] = eyeChar191 }192 }193 }194}