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
Provenance
- Commit
e5fb1b8- Tree
39c1c95- Author
Carlos Alexandro Becker <caarlos0@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
f2333d7
1 file changed+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.cpp2+package main3+4+import (5+ "fmt"6+ "math"7+ "math/rand"8+ "strings"9+ "time"10+11+ tea "github.com/charmbracelet/bubbletea"12+ "github.com/charmbracelet/lipgloss"13+)14+15+const (16+ // Eye dimensions (corresponding to original EYE_WIDTH and EYE_HEIGHT)17+ eyeWidth = 1518+ eyeHeight = 12 // Increased height for taller eyes19+ eyeSpacing = 4020+21+ // Blink animation timing (matching original constants)22+ blinkFrames = 2023+ openTimeMin = 100024+ openTimeMax = 400025+)26+27+// Characters for drawing the eyes28+const (29+ eyeChar = "●"30+ bgChar = " "31+)32+33+type model struct {34+ width int35+ height int36+ eyePositions [2]int37+ eyeY int38+ isBlinking bool39+ blinkState int40+ lastBlink time.Time41+ openTime time.Duration42+}43+44+type tickMsg time.Time45+46+func 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+}52+53+func 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+ }62+63+ m.updateEyePositions()64+ return m65+}66+67+func (m *model) updateEyePositions() {68+ startX := (m.width - eyeSpacing) / 269+ m.eyeY = m.height / 270+71+ m.eyePositions[0] = startX72+ m.eyePositions[1] = startX + eyeSpacing73+}74+75+func (m model) Init() tea.Cmd {76+ return tea.Batch(77+ tickCmd(),78+ tea.EnterAltScreen,79+ )80+}81+82+func tickCmd() tea.Cmd {83+ return tea.Tick(50*time.Millisecond, func(t time.Time) tea.Msg {84+ return tickMsg(t)85+ })86+}87+88+func (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+ }94+95+ case tea.WindowSizeMsg:96+ m.width = msg.Width97+ m.height = msg.Height98+ m.updateEyePositions()99+100+ case tickMsg:101+ currentTime := time.Now()102+103+ if !m.isBlinking && currentTime.Sub(m.lastBlink) >= m.openTime {104+ m.isBlinking = true105+ m.blinkState = 0106+ }107+108+ if m.isBlinking {109+ m.blinkState++110+111+ if m.blinkState >= blinkFrames {112+ m.isBlinking = false113+ m.lastBlink = currentTime114+ m.openTime = time.Duration(rand.Intn(openTimeMax-openTimeMin)+openTimeMin) * time.Millisecond115+116+ // 10% chance of double blink (matching original logic)117+ if rand.Intn(10) == 0 {118+ m.openTime = 300 * time.Millisecond119+ }120+ }121+ }122+ }123+124+ return m, tickCmd()125+}126+127+func (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+ }136+137+ // Calculate current eye height based on blink state138+ currentHeight := eyeHeight139+ if m.isBlinking {140+ var blinkProgress float64141+142+ 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+ }151+152+ currentHeight = int(math.Max(1, float64(eyeHeight)*blinkProgress))153+ }154+155+ // Draw both eyes156+ for i := 0; i < 2; i++ {157+ drawEllipse(canvas, m.eyePositions[i], m.eyeY, eyeWidth, currentHeight)158+ }159+160+ // 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+ }168+169+ // Style output170+ style := lipgloss.NewStyle().171+ Foreground(lipgloss.Color("#F0F0F0"))172+173+ return style.Render(s.String())174+}175+176+func 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)))182+183+ for x := -width; x <= width; x++ {184+ // Calculate canvas position185+ canvasX := x0 + x186+ canvasY := y0 + y187+188+ // 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+}