Skip to content

Commit

Permalink
feat: Add flag for turning off colors in TTY printing (#214)
Browse files Browse the repository at this point in the history
  • Loading branch information
varungandhi-src authored Nov 1, 2023
1 parent 79eeae0 commit 4e535ae
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
33 changes: 27 additions & 6 deletions cmd/scip/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package main
import (
"io"
"math"
"os"
"strings"

"github.com/k0kubun/pp/v3"
"github.com/urfave/cli/v2"
Expand All @@ -12,31 +14,49 @@ import (
)

func printCommand() cli.Command {
var json bool
var json, colorOutput bool
snapshot := cli.Command{
Name: "print",
Usage: "Print a SCIP index in a human-readable format for debugging",
Description: `WARNING: The output may change over time.
Do not rely on the output of this command in scripts`,
Usage: "Print a SCIP index for debugging",
Description: `WARNING: The TTY output may change over time.
Do not rely on non-JSON output in scripts`,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "json",
Usage: "Output in JSON format",
Destination: &json,
},
&cli.BoolFlag{
Name: "color",
Usage: "Enable color output for TTY (no effect for JSON)",
Destination: &colorOutput,
Value: true,
DefaultText: "true",
},
},
Action: func(c *cli.Context) error {
indexPath := c.Args().Get(0)
if indexPath == "" {
return errors.New("missing argument for path to SCIP index")
}
return printMain(indexPath, json, c.App.Writer)
// Following https://no-color.org/
if val, found := os.LookupEnv("NO_COLOR"); found && val != "" {
switch strings.ToLower(val) {
case "":
break
case "0", "false", "off":
colorOutput = false
default:
colorOutput = true
}
}
return printMain(indexPath, colorOutput, json, c.App.Writer)
},
}
return snapshot
}

func printMain(indexPath string, json bool, out io.Writer) error {
func printMain(indexPath string, colorOutput bool, json bool, out io.Writer) error {
index, err := readFromOption(indexPath)
if err != nil {
return err
Expand All @@ -51,6 +71,7 @@ func printMain(indexPath string, json bool, out io.Writer) error {
return err
} else {
prettyPrinter := pp.New()
prettyPrinter.SetColoringEnabled(colorOutput)
prettyPrinter.SetExportedOnly(true)
prettyPrinter.SetOutput(out)
_, err = prettyPrinter.Print(index)
Expand Down
10 changes: 6 additions & 4 deletions docs/CLI.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ DESCRIPTION:
COMMANDS:
convert Convert a SCIP index to an LSIF index
lint Flag potential issues with a SCIP index
print Print a SCIP index in a human-readable format for debugging
print Print a SCIP index for debugging
snapshot Generate snapshot files for golden testing
stats Output useful statistics about a SCIP index
help, h Shows a list of commands or help for one command
Expand Down Expand Up @@ -74,17 +74,19 @@ DESCRIPTION:

```
NAME:
scip print - Print a SCIP index in a human-readable format for debugging
scip print - Print a SCIP index for debugging
USAGE:
scip print [command options] [arguments...]
DESCRIPTION:
WARNING: The output may change over time.
Do not rely on the output of this command in scripts
WARNING: The TTY output may change over time.
Do not rely on non-JSON output in scripts
OPTIONS:
--json Output in JSON format (default: false)
--color Enable color output for TTY (no effect for JSON) (default: true)
--help, -h show help
```

## `scip snapshot`
Expand Down

0 comments on commit 4e535ae

Please sign in to comment.