diff --git a/text/color.go b/text/color.go index 4154111..264e43a 100644 --- a/text/color.go +++ b/text/color.go @@ -27,7 +27,11 @@ func areColorsOnInTheEnv() bool { if os.Getenv("FORCE_COLOR") == "1" { return true } - return os.Getenv("NO_COLOR") == "" || os.Getenv("NO_COLOR") == "0" + if os.Getenv("NO_COLOR") == "" || os.Getenv("NO_COLOR") == "0" { + return os.Getenv("TERM") != "dumb" + } + + return false } // The logic here is inspired from github.com/fatih/color; the following is diff --git a/text/color_test.go b/text/color_test.go index 447169c..3fabb83 100644 --- a/text/color_test.go +++ b/text/color_test.go @@ -28,18 +28,45 @@ func TestColor_EnableAndDisable(t *testing.T) { func TestColor_areColorsOnInTheEnv(t *testing.T) { _ = os.Setenv("FORCE_COLOR", "0") _ = os.Setenv("NO_COLOR", "0") + _ = os.Setenv("TERM", "xterm") assert.True(t, areColorsOnInTheEnv()) + _ = os.Setenv("FORCE_COLOR", "0") + _ = os.Setenv("NO_COLOR", "0") + _ = os.Setenv("TERM", "dumb") + assert.False(t, areColorsOnInTheEnv()) + + // --- + _ = os.Setenv("FORCE_COLOR", "0") + _ = os.Setenv("NO_COLOR", "1") + _ = os.Setenv("TERM", "xterm") + assert.False(t, areColorsOnInTheEnv()) + _ = os.Setenv("FORCE_COLOR", "0") _ = os.Setenv("NO_COLOR", "1") + _ = os.Setenv("TERM", "dumb") assert.False(t, areColorsOnInTheEnv()) + // --- _ = os.Setenv("FORCE_COLOR", "1") _ = os.Setenv("NO_COLOR", "0") + _ = os.Setenv("TERM", "xterm") + assert.True(t, areColorsOnInTheEnv()) + + _ = os.Setenv("FORCE_COLOR", "1") + _ = os.Setenv("NO_COLOR", "0") + _ = os.Setenv("TERM", "dumb") + assert.True(t, areColorsOnInTheEnv()) + + // --- + _ = os.Setenv("FORCE_COLOR", "1") + _ = os.Setenv("NO_COLOR", "1") + _ = os.Setenv("TERM", "xterm") assert.True(t, areColorsOnInTheEnv()) _ = os.Setenv("FORCE_COLOR", "1") _ = os.Setenv("NO_COLOR", "1") + _ = os.Setenv("TERM", "dumb") assert.True(t, areColorsOnInTheEnv()) }