mirror of
https://github.com/charmbracelet/lipgloss.git
synced 2024-08-17 09:50:31 +03:00
Don't check color profile and terminal darkness until absolutely necessary
This commit is contained in:
parent
e45d00a9fe
commit
b3875326a3
@ -227,10 +227,10 @@ func styleBorder(border string, fg, bg TerminalColor) string {
|
||||
var style = termenv.Style{}
|
||||
|
||||
if fg != noColor {
|
||||
style = style.Foreground(color(fg.value()))
|
||||
style = style.Foreground(ColorProfile().Color(fg.value()))
|
||||
}
|
||||
if bg != noColor {
|
||||
style = style.Background(color(bg.value()))
|
||||
style = style.Background(ColorProfile().Color(bg.value()))
|
||||
}
|
||||
|
||||
return style.Styled(border)
|
||||
|
35
color.go
35
color.go
@ -1,16 +1,37 @@
|
||||
package lipgloss
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"github.com/lucasb-eyer/go-colorful"
|
||||
"github.com/muesli/termenv"
|
||||
)
|
||||
|
||||
var (
|
||||
ColorProfile termenv.Profile = termenv.ColorProfile()
|
||||
color func(string) termenv.Color = ColorProfile.Color
|
||||
hasDarkBackground bool = termenv.HasDarkBackground()
|
||||
colorProfile termenv.Profile
|
||||
getColorProfile sync.Once
|
||||
hasDarkBackground bool
|
||||
checkDarkBackground sync.Once
|
||||
)
|
||||
|
||||
// ColorProfile returns the detected termenv color profile. It will perform the
|
||||
// actual check only once.
|
||||
func ColorProfile() termenv.Profile {
|
||||
getColorProfile.Do(func() {
|
||||
colorProfile = termenv.ColorProfile()
|
||||
})
|
||||
return colorProfile
|
||||
}
|
||||
|
||||
// HadDarkBackground returns whether or not the terminal has a dark background.
|
||||
// It will perform the actual check only once.
|
||||
func HasDarkBackground() bool {
|
||||
checkDarkBackground.Do(func() {
|
||||
hasDarkBackground = termenv.HasDarkBackground()
|
||||
})
|
||||
return hasDarkBackground
|
||||
}
|
||||
|
||||
// TerminalColor is a color intended to be rendered in the terminal. It
|
||||
// satisfies the Go color.Color interface.
|
||||
type TerminalColor interface {
|
||||
@ -34,7 +55,7 @@ func (n NoColor) value() string {
|
||||
}
|
||||
|
||||
func (n NoColor) color() termenv.Color {
|
||||
return color("")
|
||||
return ColorProfile().Color("")
|
||||
}
|
||||
|
||||
// RGBA returns the RGBA value of this color. Because we have to return
|
||||
@ -60,7 +81,7 @@ func (c Color) value() string {
|
||||
}
|
||||
|
||||
func (c Color) color() termenv.Color {
|
||||
return color(string(c))
|
||||
return ColorProfile().Color(string(c))
|
||||
}
|
||||
|
||||
// RGBA returns the RGBA value of this color. This satisfies the Go Color
|
||||
@ -94,14 +115,14 @@ type AdaptiveColor struct {
|
||||
}
|
||||
|
||||
func (ac AdaptiveColor) value() string {
|
||||
if hasDarkBackground {
|
||||
if HasDarkBackground() {
|
||||
return ac.Dark
|
||||
}
|
||||
return ac.Light
|
||||
}
|
||||
|
||||
func (ac AdaptiveColor) color() termenv.Color {
|
||||
return color(ac.value())
|
||||
return ColorProfile().Color(ac.value())
|
||||
}
|
||||
|
||||
// RGBA returns the RGBA value of this color. This satisfies the Go Color
|
||||
|
Loading…
Reference in New Issue
Block a user