@@ -3,6 +3,8 @@ package main
3
3
import (
4
4
"io"
5
5
"math"
6
+ "os"
7
+ "strings"
6
8
7
9
"github.com/k0kubun/pp/v3"
8
10
"github.com/urfave/cli/v2"
@@ -12,31 +14,49 @@ import (
12
14
)
13
15
14
16
func printCommand () cli.Command {
15
- var json bool
17
+ var json , colorOutput bool
16
18
snapshot := cli.Command {
17
19
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` ,
21
23
Flags : []cli.Flag {
22
24
& cli.BoolFlag {
23
25
Name : "json" ,
24
26
Usage : "Output in JSON format" ,
25
27
Destination : & json ,
26
28
},
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
+ },
27
36
},
28
37
Action : func (c * cli.Context ) error {
29
38
indexPath := c .Args ().Get (0 )
30
39
if indexPath == "" {
31
40
return errors .New ("missing argument for path to SCIP index" )
32
41
}
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 )
34
54
},
35
55
}
36
56
return snapshot
37
57
}
38
58
39
- func printMain (indexPath string , json bool , out io.Writer ) error {
59
+ func printMain (indexPath string , colorOutput bool , json bool , out io.Writer ) error {
40
60
index , err := readFromOption (indexPath )
41
61
if err != nil {
42
62
return err
@@ -51,6 +71,7 @@ func printMain(indexPath string, json bool, out io.Writer) error {
51
71
return err
52
72
} else {
53
73
prettyPrinter := pp .New ()
74
+ prettyPrinter .SetColoringEnabled (colorOutput )
54
75
prettyPrinter .SetExportedOnly (true )
55
76
prettyPrinter .SetOutput (out )
56
77
_ , err = prettyPrinter .Print (index )
0 commit comments