diff --git a/cmd/scip/print.go b/cmd/scip/print.go index 6ba57206..a858811d 100644 --- a/cmd/scip/print.go +++ b/cmd/scip/print.go @@ -3,6 +3,8 @@ package main import ( "io" "math" + "os" + "strings" "github.com/k0kubun/pp/v3" "github.com/urfave/cli/v2" @@ -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 @@ -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) diff --git a/docs/CLI.md b/docs/CLI.md index 51dacb76..ba01b3bc 100644 --- a/docs/CLI.md +++ b/docs/CLI.md @@ -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 @@ -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`