From 29934960a64dcf56f5d769c64c5ceb3ac52e37e2 Mon Sep 17 00:00:00 2001 From: Varun Gandhi Date: Tue, 31 Oct 2023 10:34:43 +0800 Subject: [PATCH] feat: Add flag for turning off colors in TTY printing --- cmd/scip/print.go | 20 ++++++++++++++------ docs/CLI.md | 15 ++++++++++----- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/cmd/scip/print.go b/cmd/scip/print.go index 6ba57206..c355b75c 100644 --- a/cmd/scip/print.go +++ b/cmd/scip/print.go @@ -12,31 +12,38 @@ 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) + 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 +58,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..a2627a0f 100644 --- a/docs/CLI.md +++ b/docs/CLI.md @@ -18,7 +18,10 @@ USAGE: scip [global options] command [command options] [arguments...] VERSION: - v0.3.1 + v0.3.1-dev +SHA: 3289897621a5e4eeecf7c17c1dc93f4025181597 +timestamp: 2023-10-31T07:03:12Z +clean: true DESCRIPTION: For more details, see the project README at: @@ -28,7 +31,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 +77,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`