Skip to content
This repository was archived by the owner on Dec 28, 2024. It is now read-only.

Commit ae5618e

Browse files
authored
enable color by default if stdout is a TTY (#9)
* enable color by default if stdout is a TTY * --color -> --no-color * Revert "--color -> --no-color" This reverts commit a9105a6.
1 parent d5f64af commit ae5618e

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

cmd/root.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/spf13/viper"
1111

1212
"github.com/svixhq/svix-cli/config"
13+
"github.com/svixhq/svix-cli/utils"
1314
"github.com/svixhq/svix-cli/version"
1415
svix "github.com/svixhq/svix-libs/go"
1516
)
@@ -35,7 +36,10 @@ func init() {
3536
rootCmd.Flags().BoolP("version", "v", false, "Get the version of the Svix CLI") // overrides default msg
3637

3738
// Global Flags
38-
rootCmd.PersistentFlags().Bool("color", false, "colorize output json")
39+
isTTY, _, err := utils.IsTTY(os.Stdout)
40+
fmt.Fprintln(os.Stderr, isTTY)
41+
cobra.CheckErr(err)
42+
rootCmd.PersistentFlags().Bool("color", isTTY, "colorize output json") // on by default if TTY, off if not
3943
cobra.CheckErr(viper.BindPFlag("color", rootCmd.PersistentFlags().Lookup("color"))) // allow color flag to be set in config
4044

4145
// Register Commands

utils/pipe.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,11 @@ import (
77
)
88

99
func IsPipeWithData(f *os.File) (bool, error) {
10-
info, err := f.Stat()
10+
isTTY, hasData, err := IsTTY(f)
1111
if err != nil {
1212
return false, err
1313
}
14-
15-
if info.Mode()&os.ModeCharDevice != 0 || // stdin is not a Unix character device
16-
info.Size() <= 0 { // stdin has no bytes
17-
return false, nil
18-
}
19-
return true, nil
14+
return !isTTY && hasData, nil
2015
}
2116

2217
func ReadPipe() ([]byte, error) {
@@ -33,3 +28,14 @@ func ReadPipe() ([]byte, error) {
3328
}
3429
return []byte{}, nil
3530
}
31+
32+
func IsTTY(f *os.File) (isTTY bool, hasBytes bool, err error) {
33+
info, err := f.Stat()
34+
if err != nil {
35+
return false, false, err
36+
}
37+
38+
isTTY = info.Mode()&os.ModeCharDevice != 0
39+
hasBytes = info.Size() > 0
40+
return isTTY, hasBytes, nil
41+
}

0 commit comments

Comments
 (0)