|
| 1 | +package util |
| 2 | + |
| 3 | +import ( |
| 4 | + "math" |
| 5 | + "os" |
| 6 | + "strings" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/charmbracelet/lipgloss/v2" |
| 10 | + "github.com/charmbracelet/lipgloss/v2/compat" |
| 11 | + "github.com/sst/opencode/internal/styles" |
| 12 | +) |
| 13 | + |
| 14 | +var shimmerStart = time.Now() |
| 15 | + |
| 16 | +// Shimmer renders text with a moving foreground highlight. |
| 17 | +// bg is the background color, dim is the base text color, bright is the highlight color. |
| 18 | +func Shimmer(s string, bg compat.AdaptiveColor, _ compat.AdaptiveColor, _ compat.AdaptiveColor) string { |
| 19 | + if s == "" { |
| 20 | + return "" |
| 21 | + } |
| 22 | + |
| 23 | + runes := []rune(s) |
| 24 | + n := len(runes) |
| 25 | + if n == 0 { |
| 26 | + return s |
| 27 | + } |
| 28 | + |
| 29 | + pad := 10 |
| 30 | + period := float64(n + pad*2) |
| 31 | + sweep := 2.5 |
| 32 | + elapsed := time.Since(shimmerStart).Seconds() |
| 33 | + pos := (math.Mod(elapsed, sweep) / sweep) * period |
| 34 | + |
| 35 | + half := 4.0 |
| 36 | + |
| 37 | + type seg struct { |
| 38 | + useHex bool |
| 39 | + hex string |
| 40 | + bold bool |
| 41 | + faint bool |
| 42 | + text string |
| 43 | + } |
| 44 | + var segs []seg |
| 45 | + |
| 46 | + useHex := hasTrueColor() |
| 47 | + for i, r := range runes { |
| 48 | + ip := float64(i + pad) |
| 49 | + dist := math.Abs(ip - pos) |
| 50 | + t := 0.0 |
| 51 | + if dist <= half { |
| 52 | + x := math.Pi * (dist / half) |
| 53 | + t = 0.5 * (1.0 + math.Cos(x)) |
| 54 | + } |
| 55 | + // Cosine brightness: base + amp*t (quantized for grouping) |
| 56 | + base := 0.55 |
| 57 | + amp := 0.45 |
| 58 | + brightness := base |
| 59 | + if t > 0 { |
| 60 | + brightness = base + amp*t |
| 61 | + } |
| 62 | + lvl := int(math.Round(brightness * 255.0)) |
| 63 | + if !useHex { |
| 64 | + step := 24 // ~11 steps across range for non-truecolor |
| 65 | + lvl = int(math.Round(float64(lvl)/float64(step))) * step |
| 66 | + } |
| 67 | + |
| 68 | + bold := lvl >= 208 |
| 69 | + faint := lvl <= 128 |
| 70 | + |
| 71 | + // truecolor if possible; else fallback to modifiers only |
| 72 | + hex := "" |
| 73 | + if useHex { |
| 74 | + if lvl < 0 { |
| 75 | + lvl = 0 |
| 76 | + } |
| 77 | + if lvl > 255 { |
| 78 | + lvl = 255 |
| 79 | + } |
| 80 | + hex = rgbHex(lvl, lvl, lvl) |
| 81 | + } |
| 82 | + |
| 83 | + if len(segs) == 0 { |
| 84 | + segs = append(segs, seg{useHex: useHex, hex: hex, bold: bold, faint: faint, text: string(r)}) |
| 85 | + } else { |
| 86 | + last := &segs[len(segs)-1] |
| 87 | + if last.useHex == useHex && last.hex == hex && last.bold == bold && last.faint == faint { |
| 88 | + last.text += string(r) |
| 89 | + } else { |
| 90 | + segs = append(segs, seg{useHex: useHex, hex: hex, bold: bold, faint: faint, text: string(r)}) |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + var b strings.Builder |
| 96 | + for _, g := range segs { |
| 97 | + st := styles.NewStyle().Background(bg) |
| 98 | + if g.useHex && g.hex != "" { |
| 99 | + c := compat.AdaptiveColor{Dark: lipgloss.Color(g.hex), Light: lipgloss.Color(g.hex)} |
| 100 | + st = st.Foreground(c) |
| 101 | + } |
| 102 | + if g.bold { |
| 103 | + st = st.Bold(true) |
| 104 | + } |
| 105 | + if g.faint { |
| 106 | + st = st.Faint(true) |
| 107 | + } |
| 108 | + b.WriteString(st.Render(g.text)) |
| 109 | + } |
| 110 | + return b.String() |
| 111 | +} |
| 112 | + |
| 113 | +func hasTrueColor() bool { |
| 114 | + c := strings.ToLower(os.Getenv("COLORTERM")) |
| 115 | + return strings.Contains(c, "truecolor") || strings.Contains(c, "24bit") |
| 116 | +} |
| 117 | + |
| 118 | +func rgbHex(r, g, b int) string { |
| 119 | + if r < 0 { |
| 120 | + r = 0 |
| 121 | + } |
| 122 | + if r > 255 { |
| 123 | + r = 255 |
| 124 | + } |
| 125 | + if g < 0 { |
| 126 | + g = 0 |
| 127 | + } |
| 128 | + if g > 255 { |
| 129 | + g = 255 |
| 130 | + } |
| 131 | + if b < 0 { |
| 132 | + b = 0 |
| 133 | + } |
| 134 | + if b > 255 { |
| 135 | + b = 255 |
| 136 | + } |
| 137 | + return "#" + hex2(r) + hex2(g) + hex2(b) |
| 138 | +} |
| 139 | + |
| 140 | +func hex2(v int) string { |
| 141 | + const digits = "0123456789abcdef" |
| 142 | + return string([]byte{digits[(v>>4)&0xF], digits[v&0xF]}) |
| 143 | +} |
0 commit comments