Actually, use sync.Once to do termenv queries

This commit is contained in:
Christian Rocha 2021-03-03 19:23:52 -05:00
parent 9b1445b6f5
commit 5913a9b105
No known key found for this signature in database
GPG Key ID: D6CC7A16E5878018

View File

@ -1,10 +1,15 @@
package lipgloss
import "github.com/muesli/termenv"
import (
"sync"
"github.com/muesli/termenv"
)
var (
color func(string) termenv.Color = termenv.ColorProfile().Color
hasDarkBackground = termenv.HasDarkBackground()
hasDarkBackground bool
color func(string) termenv.Color
initTermenv sync.Once
)
// ColorType is an interface used in color specifications.
@ -53,6 +58,11 @@ type AdaptiveColor struct {
}
func (a AdaptiveColor) value() string {
initTermenv.Do(func() {
hasDarkBackground = termenv.HasDarkBackground()
color = termenv.ColorProfile().Color
})
if hasDarkBackground {
return a.Dark
}