Skip to content
Myr

main.go

This file

Path

examples/splash/main.go

178 lines · 3.8 KiB · Go

Last changed by

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

History of this file

Take it

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

lines 1–178why, one step back
1package main
23import (4 "fmt"5 "image/color"6 "math"7 "strings"8 "time"910 tea "charm.land/bubbletea/v2"11 "charm.land/lipgloss/v2"12)1314// This example was ported from the awesome Textualize project by @willmcgugan.15// Check it out here:16// https://github.com/Textualize/textual/blob/main/examples/splash.py1718// Color gradient19var colors = []color.Color{20 lipgloss.Color("#881177"),21 lipgloss.Color("#aa3355"),22 lipgloss.Color("#cc6666"),23 lipgloss.Color("#ee9944"),24 lipgloss.Color("#eedd00"),25 lipgloss.Color("#99dd55"),26 lipgloss.Color("#44dd88"),27 lipgloss.Color("#22ccbb"),28 lipgloss.Color("#00bbcc"),29 lipgloss.Color("#0099cc"),30 lipgloss.Color("#3366bb"),31 lipgloss.Color("#663399"),32}3334type model struct {35 width int36 height int37 rate int6438}3940func (m model) Init() tea.Cmd {41 return tick42}4344func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {45 switch msg := msg.(type) {46 case tea.KeyPressMsg:47 return m, tea.Quit48 case tea.WindowSizeMsg:49 m.width = msg.Width50 m.height = msg.Height51 case tickMsg:52 return m, tick53 }54 return m, nil55}5657func (m model) View() tea.View {58 var v tea.View59 v.AltScreen = true60 if m.width == 0 {61 v.SetContent("Initializing...")62 return v63 }6465 v.SetContent(m.gradient())66 return v67}6869func (m model) gradient() string {70 // Time-based angle for animation71 t := float64(time.Now().UnixNano()*m.rate) / float64(time.Second)72 angleRadians := -t * math.Pi / 180.073 sinAngle := math.Sin(angleRadians)74 cosAngle := math.Cos(angleRadians)7576 centerX := float64(m.width) / 277 centerY := float64(m.height)7879 var output strings.Builder8081 for lineY := range m.height {82 pointY := float64(lineY)*2 - centerY83 pointX := 0.0 - centerX8485 x1 := (centerX + (pointX*cosAngle - pointY*sinAngle)) / float64(m.width)86 x2 := (centerX + (pointX*cosAngle - (pointY+1.0)*sinAngle)) / float64(m.width)87 pointX = float64(m.width) - centerX88 endX1 := (centerX + (pointX*cosAngle - pointY*sinAngle)) / float64(m.width)89 deltaX := (endX1 - x1) / float64(m.width)9091 if math.Abs(deltaX) < 0.0001 {92 // Special case for verticals93 color1 := getGradientColor(x1)94 color2 := getGradientColor(x2)95 style := lipgloss.NewStyle().96 Foreground(color1).97 Background(color2)98 output.WriteString(style.Render(strings.Repeat("▀", m.width)))99 } else {100 // Render each column in the row101 for x := range m.width {102 pos1 := x1 + float64(x)*deltaX103 pos2 := x2 + float64(x)*deltaX104 color1 := getGradientColor(pos1)105 color2 := getGradientColor(pos2)106 style := lipgloss.NewStyle().107 Foreground(color1).108 Background(color2)109 output.WriteString(style.Render("▀"))110 }111 }112 if lineY < m.height-1 {113 output.WriteString("\n")114 }115 }116117 return output.String()118}119120func getGradientColor(position float64) color.Color {121 // Normalize position to [0,1]122 if position <= 0 {123 position = 0124 }125 if position >= 1 {126 position = 1127 }128129 // Calculate the color index130 idx := position * float64(len(colors)-1)131 i1 := int(math.Floor(idx))132 i2 := int(math.Ceil(idx))133134 // Ensure indices are within bounds135 i1 = i1 % len(colors)136 i2 = i2 % len(colors)137 if i1 < 0 {138 i1 += len(colors)139 }140 if i2 < 0 {141 i2 += len(colors)142 }143144 // Interpolate between colors145 t := idx - float64(i1)146 return interpolateColors(colors[i1], colors[i2], t)147}148149func interpolateColors(color1, color2 color.Color, t float64) color.Color {150 // Parse hex colors151 r1, g1, b1, _ := color1.RGBA()152 r1, g1, b1 = r1>>8, g1>>8, b1>>8153 r2, g2, b2, _ := color2.RGBA()154 r2, g2, b2 = r2>>8, g2>>8, b2>>8155156 // Interpolate157 r := int(float64(r1)*(1-t) + float64(r2)*t)158 g := int(float64(g1)*(1-t) + float64(g2)*t)159 b := int(float64(b1)*(1-t) + float64(b2)*t)160161 return color.RGBA{uint8(r), uint8(g), uint8(b), 255}162}163164type tickMsg time.Time165166func tick() tea.Msg {167 return tickMsg(time.Now())168}169170func main() {171 p := tea.NewProgram(172 model{rate: 90},173 )174175 if _, err := p.Run(); err != nil {176 fmt.Printf("Error running program: %v", err)177 }178}