Skip to content

Commit 0c4bd34

Browse files
authored
Automatically color output based on whether output is a terminal (#240)
* Automatically color output based on whether output is a terminal
1 parent 8342248 commit 0c4bd34

File tree

2 files changed

+6
-0
lines changed

2 files changed

+6
-0
lines changed

cmd/root.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package cmd
22

33
import (
4+
"os"
5+
46
"github.com/mgutz/ansi"
57
"github.com/spf13/cobra"
8+
"golang.org/x/crypto/ssh/terminal"
69
)
710

811
const rootCmdLongUsage = `
@@ -40,6 +43,8 @@ func New() *cobra.Command {
4043
PersistentPreRun: func(cmd *cobra.Command, args []string) {
4144
if nc, _ := cmd.Flags().GetBool("no-color"); nc {
4245
ansi.DisableColors(true)
46+
} else if !cmd.Flags().Changed("no-color") {
47+
ansi.DisableColors(!terminal.IsTerminal(int(os.Stdout.Fd())))
4348
}
4449
},
4550
RunE: func(cmd *cobra.Command, args []string) error {

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ require (
2020
github.com/spf13/cobra v1.0.0
2121
github.com/spf13/pflag v1.0.5
2222
github.com/stretchr/testify v1.5.1
23+
golang.org/x/crypto v0.0.0-20200220183623-bac4c82f6975
2324
google.golang.org/grpc v1.30.0
2425
gopkg.in/yaml.v2 v2.3.0
2526
k8s.io/api v0.18.6

0 commit comments

Comments
 (0)