Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: cache the color profile #147

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions output.go
Expand Up @@ -133,6 +133,11 @@
}
}

// 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)
aymanbagabas marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -12,11 +12,11 @@
)

const (
// Escape character

Check failure on line 15 in termenv.go

View workflow job for this annotation

GitHub Actions / lint-soft

Comment should end in a period (godot)
ESC = '\x1b'
// Bell

Check failure on line 17 in termenv.go

View workflow job for this annotation

GitHub Actions / lint-soft

Comment should end in a period (godot)
BEL = '\a'
// Control Sequence Introducer

Check failure on line 19 in termenv.go

View workflow job for this annotation

GitHub Actions / lint-soft

Comment should end in a period (godot)
CSI = string(ESC) + "["
// Operating System Command
OSC = string(ESC) + "]"
Expand Down Expand Up @@ -80,7 +80,8 @@
// 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 @@
// 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