Skip to content

Commit

Permalink
fix: cache the color profile
Browse files Browse the repository at this point in the history
No need to query the environment every time we access the color profile.
  • Loading branch information
aymanbagabas committed Jul 28, 2023
1 parent 3466887 commit 7165365
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 10 deletions.
5 changes: 5 additions & 0 deletions output.go
Expand Up @@ -133,6 +133,11 @@ func WithUnsafe() OutputOption {
}
}

// ColorProfile returns the supported color profile:

Check failure on line 136 in output.go

View workflow job for this annotation

GitHub Actions / lint-soft

Comment should end in a period (godot)

Check failure on line 136 in output.go

View workflow job for this annotation

GitHub Actions / lint-soft

Comment should end in a period (godot)
func (o Output) ColorProfile() Profile {
return o.Profile
}

// ForegroundColor returns the terminal's default foreground color.
func (o *Output) ForegroundColor() Color {
f := func() {
Expand Down
8 changes: 5 additions & 3 deletions termenv.go
Expand Up @@ -80,7 +80,8 @@ func EnvNoColor() bool {
// EnvColorProfile returns the color profile based on environment variables set
// Supports NO_COLOR (https://no-color.org/)
// and CLICOLOR/CLICOLOR_FORCE (https://bixense.com/clicolors/)
// If none of these environment variables are set, this behaves the same as ColorProfile()
// If none of these environment variables are set, this behaves the same as
// determining the color profile from the TERM environment variable.
// It will return the Ascii color profile if EnvNoColor() returns true
// If the terminal does not support any colors, but CLICOLOR_FORCE is set and not "0"
// then the ANSI color profile will be returned.
Expand All @@ -91,15 +92,16 @@ func EnvColorProfile() Profile {
// EnvColorProfile returns the color profile based on environment variables set
// Supports NO_COLOR (https://no-color.org/)
// and CLICOLOR/CLICOLOR_FORCE (https://bixense.com/clicolors/)
// If none of these environment variables are set, this behaves the same as ColorProfile()
// If none of these environment variables are set, this behaves the same as
// determining the color profile from the TERM environment variable.
// It will return the Ascii color profile if EnvNoColor() returns true
// If the terminal does not support any colors, but CLICOLOR_FORCE is set and not "0"
// then the ANSI color profile will be returned.
func (o *Output) EnvColorProfile() Profile {
if o.EnvNoColor() {
return Ascii
}
p := o.ColorProfile()
p := o.termColorProfile()
if o.cliColorForced() && p == Ascii {
return ANSI
}
Expand Down
6 changes: 3 additions & 3 deletions termenv_other.go
Expand Up @@ -5,9 +5,9 @@ package termenv

import "io"

// ColorProfile returns the supported color profile:
// ANSI256
func (o Output) ColorProfile() Profile {
// termColorProfile returns the supported color profile from the TERM
// environment variable.
func (o Output) termColorProfile() Profile {
return ANSI256
}

Expand Down
6 changes: 3 additions & 3 deletions termenv_unix.go
Expand Up @@ -18,9 +18,9 @@ const (
OSCTimeout = 5 * time.Second
)

// ColorProfile returns the supported color profile:
// Ascii, ANSI, ANSI256, or TrueColor.
func (o *Output) ColorProfile() Profile {
// termColorProfile returns the supported color profile from the TERM
// environment variable.
func (o *Output) termColorProfile() Profile {
if !o.isTTY() {
return Ascii
}
Expand Down
3 changes: 2 additions & 1 deletion termenv_windows.go
Expand Up @@ -10,7 +10,8 @@ import (
"golang.org/x/sys/windows"
)

func (o *Output) ColorProfile() Profile {
// termColorProfile returns the supported color profile from TERM environment:
func (o *Output) termColorProfile() Profile {
if !o.isTTY() {
return Ascii
}
Expand Down

0 comments on commit 7165365

Please sign in to comment.