Skip to content

Commit

Permalink
fix: respect isatty
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed Jan 27, 2025
1 parent 1454f74 commit 0394ba7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
27 changes: 16 additions & 11 deletions env.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,27 @@ import (
// colors but not text decoration, i.e. bold, italic, faint, etc.
//
// See https://no-color.org/ and https://bixense.com/clicolors/ for more information.
func Detect(output io.Writer, env []string) (p Profile) {
func Detect(output io.Writer, env []string) Profile {
out, ok := output.(term.File)
isatty := ok && term.IsTerminal(out.Fd())
environ := newEnviron(env)
term := environ.get("TERM")
isDumb := term == "dumb"

Check failure on line 35 in env.go

View workflow job for this annotation

GitHub Actions / lint / lint-soft (ubuntu-latest)

string `dumb` has 4 occurrences, make it a constant (goconst)

Check failure on line 35 in env.go

View workflow job for this annotation

GitHub Actions / lint / lint-soft (macos-latest)

string `dumb` has 4 occurrences, make it a constant (goconst)

Check failure on line 35 in env.go

View workflow job for this annotation

GitHub Actions / lint / lint-soft (ubuntu-latest)

string `dumb` has 4 occurrences, make it a constant (goconst)

Check failure on line 35 in env.go

View workflow job for this annotation

GitHub Actions / lint / lint-soft (windows-latest)

string `dumb` has 4 occurrences, make it a constant (goconst)

Check failure on line 35 in env.go

View workflow job for this annotation

GitHub Actions / lint / lint-soft (windows-latest)

string `dumb` has 4 occurrences, make it a constant (goconst)

Check failure on line 35 in env.go

View workflow job for this annotation

GitHub Actions / lint / lint-soft (macos-latest)

string `dumb` has 4 occurrences, make it a constant (goconst)
envp := colorProfile(isatty, environ)
if envp == TrueColor || envNoColor(environ) {
// We already know we have TrueColor, or NO_COLOR is set.
return envp
}

tip := Terminfo(term)
tmuxp := tmux(environ)
if isatty && !isDumb {
tip := Terminfo(term)
tmuxp := tmux(environ)

// Color profile is the minimum of env, terminfo, and tmux.
return min(envp, min(tip, tmuxp))
// Color profile is the minimum of env, terminfo, and tmux.
return min(envp, min(tip, tmuxp))
}

return envp
}

// Env returns the color profile based on the terminal environment variables.
Expand All @@ -64,17 +69,17 @@ func Env(env []string) (p Profile) {
}

func colorProfile(isatty bool, env environ) (p Profile) {
envp := envColorProfile(env)
p = envp
isDumb := env.get("TERM") == "dumb"

// Check if the output is a terminal.
// Treat dumb terminals as NoTTY
envp := envColorProfile(env)
if !isatty || isDumb {
// Check if the output is a terminal.
// Treat dumb terminals as NoTTY
p = NoTTY
} else {
p = envp
}

if envNoColor(env) {
if envNoColor(env) && isatty {
if p < Ascii {
p = Ascii
}
Expand Down
8 changes: 8 additions & 0 deletions writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package colorprofile
import (
"bytes"
"io"
"os"
"testing"

"github.com/charmbracelet/x/ansi"
Expand Down Expand Up @@ -160,6 +161,13 @@ func TestNewWriterPanic(t *testing.T) {
_ = NewWriter(io.Discard, []string{"TERM=dumb"})
}

func TestNewWriterOsEnviron(t *testing.T) {
w := NewWriter(io.Discard, os.Environ())
if w.Profile != NoTTY {
t.Errorf("expected NoTTY, got %v", w.Profile)
}
}

func BenchmarkWriter(b *testing.B) {
w := &Writer{&bytes.Buffer{}, ANSI}
input := []byte("\x1b[1;3;59mhello\x1b[m \x1b[38;2;255;133;55mworld\x1b[m")
Expand Down

0 comments on commit 0394ba7

Please sign in to comment.