Skip to content

Commit e14c1e0

Browse files
committed
feat: respect NO_COLOR environment variable
- Export IsTerminal() in jsonutils package as single source of truth - Check NO_COLOR env var before terminal detection - Update configs.go to use jsonutils.IsTerminal() - Follows https://no-color.org/ standard
1 parent e7bd724 commit e14c1e0

3 files changed

Lines changed: 20 additions & 9 deletions

File tree

cmd/mcptools/commands/configs.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import (
99
"sort"
1010
"strings"
1111

12+
"github.com/f/mcptools/pkg/jsonutils"
1213
"github.com/spf13/cobra"
13-
"golang.org/x/term"
1414
"golang.org/x/text/cases"
1515
"golang.org/x/text/language"
1616
)
@@ -516,7 +516,7 @@ func formatColoredGroupedServers(servers []ServerConfig) string {
516516

517517
var buf bytes.Buffer
518518
// Check if we're outputting to a terminal (for colors)
519-
useColors := term.IsTerminal(int(os.Stdout.Fd()))
519+
useColors := jsonutils.IsTerminal()
520520

521521
for _, source := range sourceOrder {
522522
// Print source header with bold blue

pkg/jsonutils/jsonutils.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,11 @@ const (
3737
shortTypeArray = "arr"
3838
)
3939

40-
// isTerminal determines if stdout is a terminal (for colorized output).
41-
func isTerminal() bool {
40+
// IsTerminal reports whether stdout is a terminal and NO_COLOR is not set.
41+
func IsTerminal() bool {
42+
if _, noColor := os.LookupEnv("NO_COLOR"); noColor {
43+
return false
44+
}
4245
return term.IsTerminal(int(os.Stdout.Fd()))
4346
}
4447

@@ -152,7 +155,7 @@ func formatToolsList(tools any) (string, error) {
152155
termWidth := getTermWidth()
153156
descIndent := " " // 5 spaces for description indentation
154157
descWidth := termWidth - len(descIndent)
155-
useColors := isTerminal()
158+
useColors := IsTerminal()
156159

157160
for i, t := range toolsSlice {
158161
tool, ok1 := t.(map[string]any)
@@ -620,7 +623,7 @@ func formatResourcesList(resources any) (string, error) {
620623

621624
var buf bytes.Buffer
622625
w := tabwriter.NewWriter(&buf, 0, 0, 2, ' ', 0)
623-
useColors := isTerminal()
626+
useColors := IsTerminal()
624627

625628
// NOTE: Ensure that the column headers are the same length,
626629
// including the color escape sequences!
@@ -685,7 +688,7 @@ func formatPromptsList(prompts any) (string, error) {
685688
termWidth := getTermWidth()
686689
descIndent := " " // 5 spaces for description indentation
687690
descWidth := termWidth - len(descIndent)
688-
useColors := isTerminal()
691+
useColors := IsTerminal()
689692

690693
for i, p := range promptsSlice {
691694
prompt, ok1 := p.(map[string]any)
@@ -731,7 +734,7 @@ func formatContent(content any) (string, error) {
731734
}
732735

733736
var buf strings.Builder
734-
useColors := isTerminal()
737+
useColors := IsTerminal()
735738

736739
for _, c := range contentSlice {
737740
contentItem, ok1 := c.(map[string]any)
@@ -775,7 +778,7 @@ func formatGenericMap(data map[string]any) (string, error) {
775778

776779
var buf bytes.Buffer
777780
w := tabwriter.NewWriter(&buf, 0, 0, 2, ' ', 0)
778-
useColors := isTerminal()
781+
useColors := IsTerminal()
779782

780783
if useColors {
781784
fmt.Fprintf(w, "%sKEY%s\t%sVALUE%s\n",

pkg/jsonutils/jsonutils_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,3 +504,11 @@ func TestNormalizeParameterType(t *testing.T) {
504504
})
505505
}
506506
}
507+
508+
func TestIsTerminal_NoColor(t *testing.T) {
509+
// With NO_COLOR set, isTerminal should return false
510+
t.Setenv("NO_COLOR", "1")
511+
if IsTerminal() {
512+
t.Error("IsTerminal() should return false when NO_COLOR is set")
513+
}
514+
}

0 commit comments

Comments
 (0)