mirror of
https://github.com/charmbracelet/lipgloss.git
synced 2024-10-26 14:48:54 +03:00
Protect against concurrent color profile access with a sync.RWMutex
This commit is contained in:
parent
514fa059f3
commit
0c67c8c3bb
10
color.go
10
color.go
@ -16,12 +16,15 @@ var (
|
||||
getBackgroundColor sync.Once
|
||||
explicitBackgroundColor bool
|
||||
|
||||
colorProfileMtx sync.Mutex
|
||||
colorProfileMtx sync.RWMutex
|
||||
)
|
||||
|
||||
// ColorProfile returns the detected termenv color profile. It will perform the
|
||||
// actual check only once.
|
||||
func ColorProfile() termenv.Profile {
|
||||
colorProfileMtx.RLock()
|
||||
defer colorProfileMtx.RUnlock()
|
||||
|
||||
if !explicitColorProfile {
|
||||
getColorProfile.Do(func() {
|
||||
colorProfile = termenv.EnvColorProfile()
|
||||
@ -49,12 +52,16 @@ func ColorProfile() termenv.Profile {
|
||||
func SetColorProfile(p termenv.Profile) {
|
||||
colorProfileMtx.Lock()
|
||||
defer colorProfileMtx.Unlock()
|
||||
|
||||
colorProfile = p
|
||||
explicitColorProfile = true
|
||||
}
|
||||
|
||||
// HasDarkBackground returns whether or not the terminal has a dark background.
|
||||
func HasDarkBackground() bool {
|
||||
colorProfileMtx.RLock()
|
||||
defer colorProfileMtx.RUnlock()
|
||||
|
||||
if !explicitBackgroundColor {
|
||||
getBackgroundColor.Do(func() {
|
||||
hasDarkBackground = termenv.HasDarkBackground()
|
||||
@ -77,6 +84,7 @@ func HasDarkBackground() bool {
|
||||
func SetHasDarkBackground(b bool) {
|
||||
colorProfileMtx.Lock()
|
||||
defer colorProfileMtx.Unlock()
|
||||
|
||||
hasDarkBackground = b
|
||||
explicitBackgroundColor = true
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user