Skip to content

Commit 6233251

Browse files
fix: shimmer cpu cost (#2084)
1 parent 587b8ae commit 6233251

File tree

1 file changed

+35
-40
lines changed

1 file changed

+35
-40
lines changed

packages/tui/internal/util/shimmer.go

Lines changed: 35 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ import (
1111
"github.com/sst/opencode/internal/styles"
1212
)
1313

14-
var shimmerStart = time.Now()
14+
var (
15+
shimmerStart = time.Now()
16+
trueColorSupport = hasTrueColor()
17+
)
1518

1619
// Shimmer renders text with a moving foreground highlight.
1720
// bg is the background color, dim is the base text color, bright is the highlight color.
@@ -32,7 +35,7 @@ func Shimmer(s string, bg compat.AdaptiveColor, _ compat.AdaptiveColor, _ compat
3235
elapsed := time.Since(shimmerStart).Seconds()
3336
pos := (math.Mod(elapsed, sweep) / sweep) * period
3437

35-
half := 4.0
38+
half := 2.0
3639

3740
type seg struct {
3841
useHex bool
@@ -41,60 +44,52 @@ func Shimmer(s string, bg compat.AdaptiveColor, _ compat.AdaptiveColor, _ compat
4144
faint bool
4245
text string
4346
}
44-
var segs []seg
47+
segs := make([]seg, 0, n/4)
4548

46-
useHex := hasTrueColor()
49+
useHex := trueColorSupport
4750
for i, r := range runes {
4851
ip := float64(i + pad)
4952
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
7053

71-
// truecolor if possible; else fallback to modifiers only
54+
bold := false
55+
faint := true
7256
hex := ""
73-
if useHex {
74-
if lvl < 0 {
75-
lvl = 0
76-
}
77-
if lvl > 255 {
78-
lvl = 255
57+
58+
if dist <= half {
59+
// Simple 3-level brightness based on distance
60+
if dist <= half/3 {
61+
// Center: brightest
62+
bold = true
63+
faint = false
64+
if useHex {
65+
hex = "#ffffff"
66+
}
67+
} else {
68+
// Edge: medium bright
69+
bold = false
70+
faint = false
71+
if useHex {
72+
hex = "#cccccc"
73+
}
7974
}
80-
hex = rgbHex(lvl, lvl, lvl)
8175
}
8276

83-
if len(segs) == 0 {
77+
if len(segs) == 0 ||
78+
segs[len(segs)-1].useHex != useHex ||
79+
segs[len(segs)-1].hex != hex ||
80+
segs[len(segs)-1].bold != bold ||
81+
segs[len(segs)-1].faint != faint {
8482
segs = append(segs, seg{useHex: useHex, hex: hex, bold: bold, faint: faint, text: string(r)})
8583
} 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-
}
84+
segs[len(segs)-1].text += string(r)
9285
}
9386
}
9487

88+
baseStyle := styles.NewStyle().Background(bg)
9589
var b strings.Builder
90+
b.Grow(len(s) * 2)
9691
for _, g := range segs {
97-
st := styles.NewStyle().Background(bg)
92+
st := baseStyle
9893
if g.useHex && g.hex != "" {
9994
c := compat.AdaptiveColor{Dark: lipgloss.Color(g.hex), Light: lipgloss.Color(g.hex)}
10095
st = st.Foreground(c)

0 commit comments

Comments
 (0)