@@ -3,6 +3,8 @@ package main
33import (
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
1416func 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 )
0 commit comments