on main help sign in
fix(ux): fix crush logo flicker on window resizes (#1338)
04bc015Andrey Nering
internal/tui/components/logo/logo.go+2−3
@@ -4,7 +4,6 @@ package logo
4import (
4import (
5 "fmt"
5 "fmt"
6 "image/color"
6 "image/color"
7− "math/rand/v2"
8 "strings"
7 "strings"
9
8
10 "github.com/MakeNowJust/heredoc"
9 "github.com/MakeNowJust/heredoc"
@@ -53,7 +52,7 @@ func Render(version string, compact bool, o Opts) string {
53 }
52 }
54 stretchIndex := -1 // -1 means no stretching.
53 stretchIndex := -1 // -1 means no stretching.
55 if !compact {
54 if !compact {
56− stretchIndex = rand.IntN(len(letterforms))
55+ stretchIndex = cachedRandN(len(letterforms))
57 }
56 }
58
57
59 crush := renderWord(spacing, stretchIndex, letterforms...)
58 crush := renderWord(spacing, stretchIndex, letterforms...)
@@ -337,7 +336,7 @@ func stretchLetterformPart(s string, p letterformProps) string {
337 }
336 }
338 n := p.width
337 n := p.width
339 if p.stretch {
338 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
341 }
340 }
342 parts := make([]string, n)
341 parts := make([]string, n)
343 for i := range parts {
342 for i := range parts {
internal/tui/components/logo/rand.gonew file+24
@@ -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+}