From 04bc0154c1209b9a1dede09795a0b145cfa58a42 Mon Sep 17 00:00:00 2001 From: Andrey Nering Date: Thu, 30 Oct 2025 15:32:35 -0300 Subject: [PATCH] fix(ux): fix crush logo flicker on window resizes (#1338) --- internal/tui/components/logo/logo.go | 5 ++--- internal/tui/components/logo/rand.go | 24 ++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) create mode 100644 internal/tui/components/logo/rand.go diff --git a/internal/tui/components/logo/logo.go b/internal/tui/components/logo/logo.go index c5902477b944602bd9b70398541631b3362b2e5f..6d1fbe5c69b908c27f3819011a02054d9b940452 100644 --- a/internal/tui/components/logo/logo.go +++ b/internal/tui/components/logo/logo.go @@ -4,7 +4,6 @@ package logo import ( "fmt" "image/color" - "math/rand/v2" "strings" "github.com/MakeNowJust/heredoc" @@ -53,7 +52,7 @@ func Render(version string, compact bool, o Opts) string { } stretchIndex := -1 // -1 means no stretching. if !compact { - stretchIndex = rand.IntN(len(letterforms)) + stretchIndex = cachedRandN(len(letterforms)) } crush := renderWord(spacing, stretchIndex, letterforms...) @@ -337,7 +336,7 @@ func stretchLetterformPart(s string, p letterformProps) string { } n := p.width if p.stretch { - n = rand.IntN(p.maxStretch-p.minStretch) + p.minStretch //nolint:gosec + n = cachedRandN(p.maxStretch-p.minStretch) + p.minStretch //nolint:gosec } parts := make([]string, n) for i := range parts { diff --git a/internal/tui/components/logo/rand.go b/internal/tui/components/logo/rand.go new file mode 100644 index 0000000000000000000000000000000000000000..cf79487e23825b468c98a0f27bbc8dbfbb1a7081 --- /dev/null +++ b/internal/tui/components/logo/rand.go @@ -0,0 +1,24 @@ +package logo + +import ( + "math/rand/v2" + "sync" +) + +var ( + randCaches = make(map[int]int) + randCachesMu sync.Mutex +) + +func cachedRandN(n int) int { + randCachesMu.Lock() + defer randCachesMu.Unlock() + + if n, ok := randCaches[n]; ok { + return n + } + + r := rand.IntN(n) + randCaches[n] = r + return r +}