Honor NO_COLOR in the terminal log handler (closes #402)
check / check (push) Successful in 10m43s
check / check (push) Successful in 10m43s
The TTY log handler always wrote ANSI color, unlike internal/ui, which disables color when NO_COLOR is set. Add a color flag to TTYHandler, set from NO_COLOR with the same test internal/ui uses, and route every color code through a paint helper that returns plain text when color is off. The README's color sentence now covers log lines too. Model: deepseek-v4-pro
This commit is contained in:
1 parent
5abacbc5c1
commit
b21996844f
3 files changed
+75
-11
No files matched your search
@@ -421,3 +421,44 @@ func TestTTYHandlerEmptyGroupAndAttrsAreNoOps(t *testing.T) {
|
||||
assert.Contains(t, plain, "kept=yes")
|
||||
assert.NotContains(t, plain, " =")
|
||||
}
|
||||
|
||||
// TestTTYHandlerRespectsNoColor checks that only a non-empty NO_COLOR
|
||||
// turns color off, matching internal/ui's shouldColor test. The handler
|
||||
// is only ever chosen for a terminal, so this is the one remaining
|
||||
// condition that can still leave color in log output.
|
||||
func TestTTYHandlerRespectsNoColor(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
noColor string
|
||||
colored bool
|
||||
}{
|
||||
{
|
||||
name: "NO_COLOR turns color off",
|
||||
noColor: "1",
|
||||
},
|
||||
{
|
||||
name: "empty NO_COLOR keeps color on",
|
||||
noColor: "",
|
||||
colored: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, testCase := range cases {
|
||||
t.Run(testCase.name, func(t *testing.T) {
|
||||
t.Setenv("NO_COLOR", testCase.noColor)
|
||||
|
||||
var buf bytes.Buffer
|
||||
|
||||
logger := slog.New(log.NewTTYHandler(&buf, debugHandlerOptions()))
|
||||
logger.Info("hello", "key", "value")
|
||||
|
||||
if testCase.colored {
|
||||
assert.Contains(t, buf.String(), "\x1b[",
|
||||
"an empty NO_COLOR must leave ANSI color in log output")
|
||||
} else {
|
||||
assert.NotContains(t, buf.String(), "\x1b[",
|
||||
"NO_COLOR must suppress every ANSI escape in log output")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user