Skip to content

Commit 04bc015

Browse files
authored
fix(ux): fix crush logo flicker on window resizes (#1338)
1 parent ae70537 commit 04bc015

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

internal/tui/components/logo/logo.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ package logo
44
import (
55
"fmt"
66
"image/color"
7-
"math/rand/v2"
87
"strings"
98

109
"github.com/MakeNowJust/heredoc"
@@ -53,7 +52,7 @@ func Render(version string, compact bool, o Opts) string {
5352
}
5453
stretchIndex := -1 // -1 means no stretching.
5554
if !compact {
56-
stretchIndex = rand.IntN(len(letterforms))
55+
stretchIndex = cachedRandN(len(letterforms))
5756
}
5857

5958
crush := renderWord(spacing, stretchIndex, letterforms...)
@@ -337,7 +336,7 @@ func stretchLetterformPart(s string, p letterformProps) string {
337336
}
338337
n := p.width
339338
if p.stretch {
340-
n = rand.IntN(p.maxStretch-p.minStretch) + p.minStretch //nolint:gosec
339+
n = cachedRandN(p.maxStretch-p.minStretch) + p.minStretch //nolint:gosec
341340
}
342341
parts := make([]string, n)
343342
for i := range parts {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package logo
2+
3+
import (
4+
"math/rand/v2"
5+
"sync"
6+
)
7+
8+
var (
9+
randCaches = make(map[int]int)
10+
randCachesMu sync.Mutex
11+
)
12+
13+
func cachedRandN(n int) int {
14+
randCachesMu.Lock()
15+
defer randCachesMu.Unlock()
16+
17+
if n, ok := randCaches[n]; ok {
18+
return n
19+
}
20+
21+
r := rand.IntN(n)
22+
randCaches[n] = r
23+
return r
24+
}

0 commit comments

Comments
 (0)