Skip to content
Merged
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
1 change: 1 addition & 0 deletions .nextchanges/cli/no-color-keeps-prompts.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* `NO_COLOR` no longer disables prompts, spinners or the list pager; they run without color. ([#7700](https://github.com/databricks/cli/pull/7700))
6 changes: 5 additions & 1 deletion experimental/postgres/cmd/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,11 @@ func runQuery(ctx context.Context, cmd *cobra.Command, args []string, f queryFla
// Promote-to-interactive only when stdout is a prompt-capable TTY so
// a pipe falls back to the static table rather than launching a TUI
// into a dead writer.
sink := newSinkInteractive(format, out, stderr, stdoutTTY && cmdio.IsPromptSupported(ctx))
// The table viewer marks the cursor row and search matches with color
// only, so it needs color as well as a prompt-capable terminal (like
// the aitools query viewer and air list).
interactive := stdoutTTY && cmdio.IsPromptSupported(ctx) && cmdio.SupportsColor(ctx, out)
sink := newSinkInteractive(format, out, stderr, interactive)
stmtCtx, stmtCancel := withStatementTimeout(signalCtx, f.timeout)
err := executeOne(stmtCtx, conn, units[0].SQL, sink)
stmtCancel()
Expand Down
31 changes: 24 additions & 7 deletions libs/cmdio/capabilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ type Capabilities struct {
// keystrokes from it, so prompts and pagers are disabled there.
stdinIsMSYSPipe bool

// Environment flags
color bool // Color output is enabled (NO_COLOR not set and TERM not dumb)
// Environment flags. They are independent on purpose: NO_COLOR is a
// color preference only (https://no-color.org), while TERM=dumb means
// the terminal can't move the cursor, which rules out redrawing output.
noColor bool // NO_COLOR is set
dumb bool // TERM=dumb
}

// newCapabilities detects terminal capabilities from context and I/O streams.
Expand All @@ -29,14 +32,22 @@ func newCapabilities(ctx context.Context, in io.Reader, out, err io.Writer) Capa
stdinIsTTY: isTTY(in),
stdoutIsTTY: isTTY(out),
stderrIsTTY: isTTY(err),
color: env.Get(ctx, "NO_COLOR") == "" && env.Get(ctx, "TERM") != "dumb",
noColor: env.Get(ctx, "NO_COLOR") != "",
dumb: env.Get(ctx, "TERM") == "dumb",
stdinIsMSYSPipe: isMSYSPipe(in),
}
}

// SupportsInteractive returns true if terminal supports interactive features (colors, spinners).
// SupportsInteractive returns true if stderr can show output that redraws
// in place (spinners, prompts). NO_COLOR does not affect this; it only
// removes color from that output.
func (c Capabilities) SupportsInteractive() bool {
return c.stderrIsTTY && c.color
return c.stderrIsTTY && !c.dumb
}

// colorAllowed reports whether the environment permits color at all.
func (c Capabilities) colorAllowed() bool {
return !c.noColor && !c.dumb
}

// SupportsPrompt returns true if terminal supports user prompting.
Expand All @@ -49,14 +60,20 @@ func (c Capabilities) SupportsPrompt() bool {
// SupportsColor returns true if the given writer supports colored output.
// This checks both TTY status and environment variables (NO_COLOR, TERM=dumb).
func (c Capabilities) SupportsColor(w io.Writer) bool {
return isTTY(w) && c.color
return isTTY(w) && c.colorAllowed()
}

// SupportsStdoutColor returns true if stdout supports colored output.
// Use this when emitting colored bytes to a writer that wraps stdout (e.g.
// a buffered flusher) where SupportsColor's isTTY check would be misled.
func (c Capabilities) SupportsStdoutColor() bool {
return c.stdoutIsTTY && c.color
return c.stdoutIsTTY && c.colorAllowed()
}

// SupportsStderrColor is the stderr counterpart of SupportsStdoutColor.
// Prompts use it; they render to stderr.
func (c Capabilities) SupportsStderrColor() bool {
return c.stderrIsTTY && c.colorAllowed()
}

// SupportsPager returns true when we can drive an interactive pager.
Expand Down
252 changes: 80 additions & 172 deletions libs/cmdio/capabilities_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,36 @@ import (
"io"
"testing"

"github.com/databricks/cli/libs/env"
"github.com/stretchr/testify/assert"
)

func TestNewCapabilities_Env(t *testing.T) {
// Pin both variables so the developer's shell can't leak in.
ctx := env.Set(env.Set(t.Context(), "NO_COLOR", ""), "TERM", "")
c := newCapabilities(ctx, nil, nil, nil)
assert.False(t, c.noColor)
assert.False(t, c.dumb)

c = newCapabilities(env.Set(ctx, "NO_COLOR", "1"), nil, nil, nil)
assert.True(t, c.noColor)
assert.False(t, c.dumb)

c = newCapabilities(env.Set(ctx, "TERM", "dumb"), nil, nil, nil)
assert.False(t, c.noColor)
assert.True(t, c.dumb)
}

func TestCapabilities_SupportsInteractive(t *testing.T) {
tests := []struct {
name string
caps Capabilities
expected bool
}{
{
name: "normal terminal",
caps: Capabilities{stderrIsTTY: true, color: true},
expected: true,
},
{
name: "NO_COLOR set",
caps: Capabilities{stderrIsTTY: true, color: false},
expected: false,
},
{
name: "stderr not TTY",
caps: Capabilities{stderrIsTTY: false, color: true},
expected: false,
},
{
name: "both NO_COLOR and no TTY",
caps: Capabilities{stderrIsTTY: false, color: false},
expected: false,
},
{"normal terminal", Capabilities{stderrIsTTY: true}, true},
{"NO_COLOR only removes color", Capabilities{stderrIsTTY: true, noColor: true}, true},
{"TERM=dumb can't redraw", Capabilities{stderrIsTTY: true, dumb: true}, false},
{"stderr not TTY", Capabilities{stderrIsTTY: false}, false},
}

for _, tt := range tests {
Expand All @@ -48,77 +49,14 @@ func TestCapabilities_SupportsPrompt(t *testing.T) {
caps Capabilities
expected bool
}{
{
name: "full TTY support",
caps: Capabilities{
stdinIsTTY: true,
stdoutIsTTY: true,
stderrIsTTY: true,
color: true,
},
expected: true,
},
{
name: "MSYS2 pipe stdin blocks prompts",
caps: Capabilities{
stdinIsTTY: true,
stdoutIsTTY: true,
stderrIsTTY: true,
color: true,
stdinIsMSYSPipe: true,
},
expected: false,
},
{
name: "stdin not TTY",
caps: Capabilities{
stdinIsTTY: false,
stdoutIsTTY: true,
stderrIsTTY: true,
color: true,
},
expected: false,
},
{
name: "stdout not TTY (prompts write to stderr, so this is OK)",
caps: Capabilities{
stdinIsTTY: true,
stdoutIsTTY: false,
stderrIsTTY: true,
color: true,
},
expected: true,
},
{
name: "stderr not TTY",
caps: Capabilities{
stdinIsTTY: true,
stdoutIsTTY: true,
stderrIsTTY: false,
color: true,
},
expected: false,
},
{
name: "NO_COLOR set",
caps: Capabilities{
stdinIsTTY: true,
stdoutIsTTY: true,
stderrIsTTY: true,
color: false,
},
expected: false,
},
{
name: "no TTY support at all",
caps: Capabilities{
stdinIsTTY: false,
stdoutIsTTY: false,
stderrIsTTY: false,
color: true,
},
expected: false,
},
{"full TTY support", Capabilities{stdinIsTTY: true, stdoutIsTTY: true, stderrIsTTY: true}, true},
{"MSYS2 pipe stdin blocks prompts", Capabilities{stdinIsTTY: true, stdoutIsTTY: true, stderrIsTTY: true, stdinIsMSYSPipe: true}, false},
{"stdin not TTY", Capabilities{stdoutIsTTY: true, stderrIsTTY: true}, false},
{"stdout not TTY (prompts write to stderr, so this is OK)", Capabilities{stdinIsTTY: true, stderrIsTTY: true}, true},
{"stderr not TTY", Capabilities{stdinIsTTY: true, stdoutIsTTY: true}, false},
{"NO_COLOR still prompts", Capabilities{stdinIsTTY: true, stdoutIsTTY: true, stderrIsTTY: true, noColor: true}, true},
{"TERM=dumb", Capabilities{stdinIsTTY: true, stdoutIsTTY: true, stderrIsTTY: true, dumb: true}, false},
{"no TTY support at all", Capabilities{}, false},
}

for _, tt := range tests {
Expand All @@ -128,43 +66,61 @@ func TestCapabilities_SupportsPrompt(t *testing.T) {
}
}

func TestCapabilities_SupportsPager(t *testing.T) {
Comment thread
pietern marked this conversation as resolved.
Resolved
tests := []struct {
name string
caps Capabilities
expected bool
}{
{"full TTY support", Capabilities{stdinIsTTY: true, stdoutIsTTY: true, stderrIsTTY: true}, true},
{"NO_COLOR keeps the pager (uncolored)", Capabilities{stdinIsTTY: true, stdoutIsTTY: true, stderrIsTTY: true, noColor: true}, true},
{"TERM=dumb can't redraw", Capabilities{stdinIsTTY: true, stdoutIsTTY: true, stderrIsTTY: true, dumb: true}, false},
{"stdout redirected", Capabilities{stdinIsTTY: true, stderrIsTTY: true}, false},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.expected, tt.caps.SupportsPager())
})
}
}

func TestCapabilities_SupportsColor(t *testing.T) {
tests := []struct {
name string
caps Capabilities
writer io.Writer
expected bool
}{
{
name: "TTY with no env restrictions",
caps: Capabilities{color: true},
writer: FakeTTY(io.Discard),
expected: true,
},
{
name: "TTY with NO_COLOR set",
caps: Capabilities{color: false},
writer: FakeTTY(io.Discard),
expected: false,
},
{
name: "non-TTY writer",
caps: Capabilities{color: true},
writer: io.Discard,
expected: false,
},
{
name: "non-TTY with NO_COLOR",
caps: Capabilities{color: false},
writer: io.Discard,
expected: false,
},
{"TTY with no env restrictions", Capabilities{}, FakeTTY(io.Discard), true},
{"TTY with NO_COLOR set", Capabilities{noColor: true}, FakeTTY(io.Discard), false},
{"TTY with TERM=dumb", Capabilities{dumb: true}, FakeTTY(io.Discard), false},
{"non-TTY writer", Capabilities{}, io.Discard, false},
{"non-TTY with NO_COLOR", Capabilities{noColor: true}, io.Discard, false},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.expected, tt.caps.SupportsColor(tt.writer))
})
}
}

func TestCapabilities_SupportsStreamColor(t *testing.T) {
tests := []struct {
name string
caps Capabilities
stdout, stderr bool
}{
{"both TTYs", Capabilities{stdoutIsTTY: true, stderrIsTTY: true}, true, true},
{"NO_COLOR", Capabilities{stdoutIsTTY: true, stderrIsTTY: true, noColor: true}, false, false},
{"only stdout TTY", Capabilities{stdoutIsTTY: true}, true, false},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := tt.caps.SupportsColor(tt.writer)
assert.Equal(t, tt.expected, result)
assert.Equal(t, tt.stdout, tt.caps.SupportsStdoutColor())
assert.Equal(t, tt.stderr, tt.caps.SupportsStderrColor())
})
}
}
Expand All @@ -175,61 +131,13 @@ func TestCapabilities_InteractiveMode(t *testing.T) {
caps Capabilities
expected InteractiveMode
}{
{
name: "full interactive - all TTYs, color, console stdin",
caps: Capabilities{
stdinIsTTY: true,
stderrIsTTY: true,
color: true,
},
expected: InteractiveModeFull,
},
{
name: "output only - stdin not TTY",
caps: Capabilities{
stdinIsTTY: false,
stderrIsTTY: true,
color: true,
},
expected: InteractiveModeOutputOnly,
},
{
name: "output only - MSYS2 pipe stdin",
caps: Capabilities{
stdinIsTTY: true,
stderrIsTTY: true,
color: true,
stdinIsMSYSPipe: true,
},
expected: InteractiveModeOutputOnly,
},
{
name: "none - stderr not TTY",
caps: Capabilities{
stdinIsTTY: true,
stderrIsTTY: false,
color: true,
},
expected: InteractiveModeNone,
},
{
name: "none - NO_COLOR set",
caps: Capabilities{
stdinIsTTY: true,
stderrIsTTY: true,
color: false,
},
expected: InteractiveModeNone,
},
{
name: "none - no TTY support at all",
caps: Capabilities{
stdinIsTTY: false,
stderrIsTTY: false,
color: false,
},
expected: InteractiveModeNone,
},
{"full interactive", Capabilities{stdinIsTTY: true, stderrIsTTY: true}, InteractiveModeFull},
{"full interactive with NO_COLOR", Capabilities{stdinIsTTY: true, stderrIsTTY: true, noColor: true}, InteractiveModeFull},
{"output only - stdin not TTY", Capabilities{stdinIsTTY: false, stderrIsTTY: true}, InteractiveModeOutputOnly},
{"output only - MSYS2 pipe stdin", Capabilities{stdinIsTTY: true, stderrIsTTY: true, stdinIsMSYSPipe: true}, InteractiveModeOutputOnly},
{"none - stderr not TTY", Capabilities{stdinIsTTY: true, stderrIsTTY: false}, InteractiveModeNone},
{"none - TERM=dumb", Capabilities{stdinIsTTY: true, stderrIsTTY: true, dumb: true}, InteractiveModeNone},
{"none - no TTY support at all", Capabilities{}, InteractiveModeNone},
}

for _, tt := range tests {
Expand Down
2 changes: 1 addition & 1 deletion libs/cmdio/conpty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestIsMSYSPipeAnonymousPipe(t *testing.T) {
func TestMSYSPipeBlocksPromptsOnly(t *testing.T) {
// An MSYS2 pty pipe counts as a TTY for output and line input, but not
// for prompts and pagers, which need raw keystrokes.
c := Capabilities{stdinIsTTY: true, stdoutIsTTY: true, stderrIsTTY: true, color: true, stdinIsMSYSPipe: true}
c := Capabilities{stdinIsTTY: true, stdoutIsTTY: true, stderrIsTTY: true, stdinIsMSYSPipe: true}
assert.True(t, c.SupportsInteractive())
assert.True(t, c.SupportsColor(FakeTTY(&bytes.Buffer{})))
assert.False(t, c.SupportsPrompt())
Expand Down
Loading
Loading