Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add flag for turning off colors in TTY printing #214

Merged
merged 2 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 26 additions & 6 deletions cmd/scip/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"io"
"math"
"os"

"github.com/k0kubun/pp/v3"
"github.com/urfave/cli/v2"
Expand All @@ -12,31 +13,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 val {
case "":
break
case "0", "false", "FALSE", "off", "OFF":
varungandhi-src marked this conversation as resolved.
Show resolved Hide resolved
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 +70,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
Loading