Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions cmd/color_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package cmd

import "testing"

func TestShouldEnableColor(t *testing.T) {
tests := []struct {
name string
noColorEnv string
isTTY bool
want bool
}{
{"tty, no env", "", true, true},
{"not tty, no env", "", false, false},
{"tty, NO_COLOR set", "1", true, false},
{"not tty, NO_COLOR set", "1", false, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := shouldEnableColor(tt.noColorEnv, tt.isTTY); got != tt.want {
t.Errorf("shouldEnableColor(%q, %v) = %v, want %v", tt.noColorEnv, tt.isTTY, got, tt.want)
}
})
}
}
18 changes: 16 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/kernel/cli/cmd/mcp"
"github.com/kernel/cli/cmd/proxies"
"github.com/kernel/cli/pkg/auth"
"github.com/kernel/cli/pkg/table"
"github.com/kernel/cli/pkg/update"
"github.com/kernel/cli/pkg/util"
"github.com/kernel/kernel-go-sdk"
Expand Down Expand Up @@ -177,9 +178,22 @@ func init() {
}
}

// shouldEnableColor decides whether pterm color styling should be on.
// NO_COLOR (any non-empty value) always wins per https://no-color.org.
// Otherwise color is enabled only when stdout is an interactive terminal.
func shouldEnableColor(noColorEnv string, isTTY bool) bool {
if noColorEnv != "" {
return false
}
return isTTY
}

func initConfig() {
// Placeholder for future configuration (env vars, config files, etc.)
pterm.EnableStyling() // ensure pterm is initialised in case env disables it
if shouldEnableColor(os.Getenv("NO_COLOR"), table.IsStdoutTTY()) {
pterm.EnableStyling()
} else {
pterm.DisableStyling()
}
}

// Execute executes the root command.
Expand Down
Loading