Skip to content

Commit 4e535ae

Browse files
feat: Add flag for turning off colors in TTY printing (#214)
1 parent 79eeae0 commit 4e535ae

File tree

2 files changed

+33
-10
lines changed

2 files changed

+33
-10
lines changed

cmd/scip/print.go

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package main
33
import (
44
"io"
55
"math"
6+
"os"
7+
"strings"
68

79
"github.com/k0kubun/pp/v3"
810
"github.com/urfave/cli/v2"
@@ -12,31 +14,49 @@ import (
1214
)
1315

1416
func printCommand() cli.Command {
15-
var json bool
17+
var json, colorOutput bool
1618
snapshot := cli.Command{
1719
Name: "print",
18-
Usage: "Print a SCIP index in a human-readable format for debugging",
19-
Description: `WARNING: The output may change over time.
20-
Do not rely on the output of this command in scripts`,
20+
Usage: "Print a SCIP index for debugging",
21+
Description: `WARNING: The TTY output may change over time.
22+
Do not rely on non-JSON output in scripts`,
2123
Flags: []cli.Flag{
2224
&cli.BoolFlag{
2325
Name: "json",
2426
Usage: "Output in JSON format",
2527
Destination: &json,
2628
},
29+
&cli.BoolFlag{
30+
Name: "color",
31+
Usage: "Enable color output for TTY (no effect for JSON)",
32+
Destination: &colorOutput,
33+
Value: true,
34+
DefaultText: "true",
35+
},
2736
},
2837
Action: func(c *cli.Context) error {
2938
indexPath := c.Args().Get(0)
3039
if indexPath == "" {
3140
return errors.New("missing argument for path to SCIP index")
3241
}
33-
return printMain(indexPath, json, c.App.Writer)
42+
// Following https://no-color.org/
43+
if val, found := os.LookupEnv("NO_COLOR"); found && val != "" {
44+
switch strings.ToLower(val) {
45+
case "":
46+
break
47+
case "0", "false", "off":
48+
colorOutput = false
49+
default:
50+
colorOutput = true
51+
}
52+
}
53+
return printMain(indexPath, colorOutput, json, c.App.Writer)
3454
},
3555
}
3656
return snapshot
3757
}
3858

39-
func printMain(indexPath string, json bool, out io.Writer) error {
59+
func printMain(indexPath string, colorOutput bool, json bool, out io.Writer) error {
4060
index, err := readFromOption(indexPath)
4161
if err != nil {
4262
return err
@@ -51,6 +71,7 @@ func printMain(indexPath string, json bool, out io.Writer) error {
5171
return err
5272
} else {
5373
prettyPrinter := pp.New()
74+
prettyPrinter.SetColoringEnabled(colorOutput)
5475
prettyPrinter.SetExportedOnly(true)
5576
prettyPrinter.SetOutput(out)
5677
_, err = prettyPrinter.Print(index)

docs/CLI.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ DESCRIPTION:
2828
COMMANDS:
2929
convert Convert a SCIP index to an LSIF index
3030
lint Flag potential issues with a SCIP index
31-
print Print a SCIP index in a human-readable format for debugging
31+
print Print a SCIP index for debugging
3232
snapshot Generate snapshot files for golden testing
3333
stats Output useful statistics about a SCIP index
3434
help, h Shows a list of commands or help for one command
@@ -74,17 +74,19 @@ DESCRIPTION:
7474

7575
```
7676
NAME:
77-
scip print - Print a SCIP index in a human-readable format for debugging
77+
scip print - Print a SCIP index for debugging
7878
7979
USAGE:
8080
scip print [command options] [arguments...]
8181
8282
DESCRIPTION:
83-
WARNING: The output may change over time.
84-
Do not rely on the output of this command in scripts
83+
WARNING: The TTY output may change over time.
84+
Do not rely on non-JSON output in scripts
8585
8686
OPTIONS:
8787
--json Output in JSON format (default: false)
88+
--color Enable color output for TTY (no effect for JSON) (default: true)
89+
--help, -h show help
8890
```
8991

9092
## `scip snapshot`

0 commit comments

Comments
 (0)