From 933560680fe8ae35f2609783305fb55d7a819725 Mon Sep 17 00:00:00 2001 From: Gareth Jones <3151613+G-Rath@users.noreply.github.com> Date: Fri, 3 Oct 2025 12:19:31 +1300 Subject: [PATCH] feat: don't enable colors if `TERM` is set to `dumb` --- text/color.go | 6 +++++- text/color_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) 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()) }