@@ -5,131 +5,130 @@ that suits you.
55
66Use simple and default helper functions with predefined foreground colors:
77
8- color.Cyan("Prints text in cyan.")
8+ color.Cyan("Prints text in cyan.")
99
10- // a newline will be appended automatically
11- color.Blue("Prints %s in blue.", "text")
10+ // a newline will be appended automatically
11+ color.Blue("Prints %s in blue.", "text")
1212
13- // More default foreground colors..
14- color.Red("We have red")
15- color.Yellow("Yellow color too!")
16- color.Magenta("And many others ..")
13+ // More default foreground colors..
14+ color.Red("We have red")
15+ color.Yellow("Yellow color too!")
16+ color.Magenta("And many others ..")
1717
18- // Hi-intensity colors
19- color.HiGreen("Bright green color.")
20- color.HiBlack("Bright black means gray..")
21- color.HiWhite("Shiny white color!")
18+ // Hi-intensity colors
19+ color.HiGreen("Bright green color.")
20+ color.HiBlack("Bright black means gray..")
21+ color.HiWhite("Shiny white color!")
2222
23- However there are times where custom color mixes are required. Below are some
23+ However, there are times when custom color mixes are required. Below are some
2424examples to create custom color objects and use the print functions of each
2525separate color object.
2626
27- // Create a new color object
28- c := color.New(color.FgCyan).Add(color.Underline)
29- c.Println("Prints cyan text with an underline.")
27+ // Create a new color object
28+ c := color.New(color.FgCyan).Add(color.Underline)
29+ c.Println("Prints cyan text with an underline.")
3030
31- // Or just add them to New()
32- d := color.New(color.FgCyan, color.Bold)
33- d.Printf("This prints bold cyan %s\n", "too!.")
31+ // Or just add them to New()
32+ d := color.New(color.FgCyan, color.Bold)
33+ d.Printf("This prints bold cyan %s\n", "too!.")
3434
3535
36- // Mix up foreground and background colors, create new mixes!
37- red := color.New(color.FgRed)
36+ // Mix up foreground and background colors, create new mixes!
37+ red := color.New(color.FgRed)
3838
39- boldRed := red.Add(color.Bold)
40- boldRed.Println("This will print text in bold red.")
39+ boldRed := red.Add(color.Bold)
40+ boldRed.Println("This will print text in bold red.")
4141
42- whiteBackground := red.Add(color.BgWhite)
43- whiteBackground.Println("Red text with White background.")
42+ whiteBackground := red.Add(color.BgWhite)
43+ whiteBackground.Println("Red text with White background.")
4444
45- // Use your own io.Writer output
46- color.New(color.FgBlue).Fprintln(myWriter, "blue color!")
45+ // Use your own io.Writer output
46+ color.New(color.FgBlue).Fprintln(myWriter, "blue color!")
4747
48- blue := color.New(color.FgBlue)
49- blue.Fprint(myWriter, "This will print text in blue.")
48+ blue := color.New(color.FgBlue)
49+ blue.Fprint(myWriter, "This will print text in blue.")
5050
5151You can create PrintXxx functions to simplify even more:
5252
53- // Create a custom print function for convenient
54- red := color.New(color.FgRed).PrintfFunc()
55- red("warning")
56- red("error: %s", err)
53+ // Create a custom print function for convenient
54+ red := color.New(color.FgRed).PrintfFunc()
55+ red("warning")
56+ red("error: %s", err)
5757
58- // Mix up multiple attributes
59- notice := color.New(color.Bold, color.FgGreen).PrintlnFunc()
60- notice("don't forget this...")
58+ // Mix up multiple attributes
59+ notice := color.New(color.Bold, color.FgGreen).PrintlnFunc()
60+ notice("don't forget this...")
6161
6262You can also FprintXxx functions to pass your own io.Writer:
6363
64- blue := color.New(FgBlue).FprintfFunc()
65- blue(myWriter, "important notice: %s", stars)
66-
67- // Mix up with multiple attributes
68- success := color.New(color.Bold, color.FgGreen).FprintlnFunc()
69- success(myWriter, don't forget this...")
64+ blue := color.New(FgBlue).FprintfFunc()
65+ blue(myWriter, "important notice: %s", stars)
7066
67+ // Mix up with multiple attributes
68+ success := color.New(color.Bold, color.FgGreen).FprintlnFunc()
69+ success(myWriter, don't forget this...")
7170
7271Or create SprintXxx functions to mix strings with other non-colorized strings:
7372
74- yellow := New(FgYellow).SprintFunc()
75- red := New(FgRed).SprintFunc()
73+ yellow := New(FgYellow).SprintFunc()
74+ red := New(FgRed).SprintFunc()
7675
77- fmt.Printf("this is a %s and this is %s.\n", yellow("warning"), red("error"))
76+ fmt.Printf("this is a %s and this is %s.\n", yellow("warning"), red("error"))
7877
79- info := New(FgWhite, BgGreen).SprintFunc()
80- fmt.Printf("this %s rocks!\n", info("package"))
78+ info := New(FgWhite, BgGreen).SprintFunc()
79+ fmt.Printf("this %s rocks!\n", info("package"))
8180
8281Windows support is enabled by default. All Print functions work as intended.
83- However only for color.SprintXXX functions, user should use fmt.FprintXXX and
82+ However, only for color.SprintXXX functions, user should use fmt.FprintXXX and
8483set the output to color.Output:
8584
86- fmt.Fprintf(color.Output, "Windows support: %s", color.GreenString("PASS"))
85+ fmt.Fprintf(color.Output, "Windows support: %s", color.GreenString("PASS"))
8786
88- info := New(FgWhite, BgGreen).SprintFunc()
89- fmt.Fprintf(color.Output, "this %s rocks!\n", info("package"))
87+ info := New(FgWhite, BgGreen).SprintFunc()
88+ fmt.Fprintf(color.Output, "this %s rocks!\n", info("package"))
9089
9190Using with existing code is possible. Just use the Set() method to set the
9291standard output to the given parameters. That way a rewrite of an existing
9392code is not required.
9493
95- // Use handy standard colors.
96- color.Set(color.FgYellow)
94+ // Use handy standard colors.
95+ color.Set(color.FgYellow)
9796
98- fmt.Println("Existing text will be now in Yellow")
99- fmt.Printf("This one %s\n", "too")
97+ fmt.Println("Existing text will be now in Yellow")
98+ fmt.Printf("This one %s\n", "too")
10099
101- color.Unset() // don't forget to unset
100+ color.Unset() // don't forget to unset
102101
103- // You can mix up parameters
104- color.Set(color.FgMagenta, color.Bold)
105- defer color.Unset() // use it in your function
102+ // You can mix up parameters
103+ color.Set(color.FgMagenta, color.Bold)
104+ defer color.Unset() // use it in your function
106105
107- fmt.Println("All text will be now bold magenta.")
106+ fmt.Println("All text will be now bold magenta.")
108107
109108There might be a case where you want to disable color output (for example to
110109pipe the standard output of your app to somewhere else). `Color` has support to
111110disable colors both globally and for single color definition. For example
112111suppose you have a CLI app and a `--no-color` bool flag. You can easily disable
113112the color output with:
114113
115- var flagNoColor = flag.Bool("no-color", false, "Disable color output")
114+ var flagNoColor = flag.Bool("no-color", false, "Disable color output")
116115
117- if *flagNoColor {
118- color.NoColor = true // disables colorized output
119- }
116+ if *flagNoColor {
117+ color.NoColor = true // disables colorized output
118+ }
120119
121120You can also disable the color by setting the NO_COLOR environment variable to any value.
122121
123122It also has support for single color definitions (local). You can
124123disable/enable color output on the fly:
125124
126- c := color.New(color.FgCyan)
127- c.Println("Prints cyan text")
125+ c := color.New(color.FgCyan)
126+ c.Println("Prints cyan text")
128127
129- c.DisableColor()
130- c.Println("This is printed without any color")
128+ c.DisableColor()
129+ c.Println("This is printed without any color")
131130
132- c.EnableColor()
133- c.Println("This prints again cyan...")
131+ c.EnableColor()
132+ c.Println("This prints again cyan...")
134133*/
135134package color
0 commit comments