Debian Codegraph @

pkg/color/example_test.go

raw
1 package color_test
2
3 import (
4 "fmt"
5 "os"
6
7 "github.com/nicholas-fedor/shoutrrr/pkg/color"
8 )
9
10 // Example demonstrates basic color printing using helper functions.
11 // The helper functions use the default configuration internally.
12 func Example() {
13 // Disable color via environment variable for predictable test output
14 os.Setenv("NO_COLOR", "true")
15
16 defer os.Unsetenv("NO_COLOR")
17
18 fmt.Println(color.RedString("This is red text"))
19 fmt.Println(color.GreenString("This is green text"))
20 fmt.Println(color.BlueString("This is blue text"))
21 fmt.Println(color.YellowString("This is yellow text"))
22
23 // Output:
24 // This is red text
25 // This is green text
26 // This is blue text
27 // This is yellow text
28 }
29
30 // ExampleRGB demonstrates using RGB colors for foreground and background.
31 // Uses the default configuration via helper functions.
32 func ExampleRGB() {
33 // Disable color via environment variable for predictable test output
34 os.Setenv("NO_COLOR", "true")
35
36 defer os.Unsetenv("NO_COLOR")
37
38 orange := color.RGB(255, 128, 0)
39 fmt.Println(orange.Sprint("Orange foreground text"))
40
41 blueBg := color.BgRGB(0, 0, 255)
42 fmt.Println(blueBg.Sprint("Text with blue background"))
43
44 custom := color.RGB(128, 64, 255).AddBgRGB(255, 255, 0)
45 fmt.Println(custom.Sprint("Purple text on yellow background"))
46
47 // Output:
48 // Orange foreground text
49 // Text with blue background
50 // Purple text on yellow background
51 }
52
53 // ExampleNew demonstrates creating and mixing custom colors.
54 // Uses the default configuration via the New() constructor.
55 func ExampleNew() {
56 // Disable color via environment variable for predictable test output
57 os.Setenv("NO_COLOR", "true")
58
59 defer os.Unsetenv("NO_COLOR")
60
61 // Create a bold red color
62 boldRed := color.New(color.FgRed, color.Bold)
63 fmt.Println(boldRed.Sprint("Bold red text"))
64
65 // Mix colors by adding attributes
66 underlinedGreen := color.New(color.FgGreen).Add(color.Underline)
67 fmt.Println(underlinedGreen.Sprint("Underlined green text"))
68
69 // Combine multiple attributes
70 fancy := color.New(color.FgCyan, color.BgBlack, color.Bold, color.Underline)
71 fmt.Println(fancy.Sprint("Fancy cyan text"))
72
73 // Output:
74 // Bold red text
75 // Underlined green text
76 // Fancy cyan text
77 }
78
79 // ExampleColor_PrintFunc demonstrates using custom print functions.
80 // Uses the default configuration via the New() constructor.
81 func ExampleColor_PrintFunc() {
82 // Disable color via environment variable for predictable test output
83 os.Setenv("NO_COLOR", "true")
84
85 defer os.Unsetenv("NO_COLOR")
86
87 // Create custom print functions
88 warn := color.New(color.FgYellow).PrintlnFunc()
89 err := color.New(color.FgRed, color.Bold).PrintfFunc()
90
91 warn("This is a warning")
92 err("Error code: %d\n", 404)
93
94 // Custom string function
95 info := color.New(color.FgBlue).SprintFunc()
96 message := "Info: " + info("system ready")
97 fmt.Println(message)
98
99 // Output:
100 // This is a warning
101 // Error code: 404
102 // Info: system ready
103 }
104
105 // ExampleColor_SprintFunc demonstrates string functions for mixing with non-colored text.
106 // Uses the default configuration via the New() constructor.
107 func ExampleColor_SprintFunc() {
108 // Disable color via environment variable for predictable test output
109 os.Setenv("NO_COLOR", "true")
110
111 defer os.Unsetenv("NO_COLOR")
112
113 // Create color string functions
114 red := color.New(color.FgRed).SprintFunc()
115 green := color.New(color.FgGreen).SprintFunc()
116 blue := color.New(color.FgBlue).SprintFunc()
117
118 // Mix colored strings with regular text
119 status := fmt.Sprintf("Status: %s | %s | %s",
120 red("ERROR"),
121 green("SUCCESS"),
122 blue("INFO"))
123
124 fmt.Println(status)
125
126 // Using SprintfFunc for formatted colored strings
127 coloredPrintf := color.New(color.FgMagenta).SprintfFunc()
128 details := "Details: " + coloredPrintf("Value is %d", 42)
129 fmt.Println(details)
130
131 // Output:
132 // Status: ERROR | SUCCESS | INFO
133 // Details: Value is 42
134 }
135
136 // ExampleNewWithConfig demonstrates creating a Color with a custom Config
137 // for thread-safe, instance-based color management.
138 func ExampleNewWithConfig() {
139 // Create a custom configuration with color disabled for predictable output
140 cfg := &color.Config{
141 NoColor: true,
142 Output: os.Stdout,
143 }
144
145 // Create color instances with the custom config
146 red := color.NewWithConfig(cfg, color.FgRed)
147 green := color.NewWithConfig(cfg, color.FgGreen)
148 blue := color.NewWithConfig(cfg, color.FgBlue)
149
150 fmt.Println(red.Sprint("Red text (disabled)"))
151 fmt.Println(green.Sprint("Green text (disabled)"))
152 fmt.Println(blue.Sprint("Blue text (disabled)"))
153
154 // Output:
155 // Red text (disabled)
156 // Green text (disabled)
157 // Blue text (disabled)
158 }
159
160 // ExampleConfig demonstrates using the Config struct for thread-safe
161 // color configuration that can be passed to color constructors.
162 func ExampleConfig() {
163 // Create a custom configuration
164 cfg := &color.Config{
165 NoColor: false,
166 Output: os.Stdout,
167 }
168
169 // The configuration provides safe defaults
170 fmt.Fprintf(cfg.Output, "Output is stdout: %v\n", cfg.Output == os.Stdout)
171 fmt.Fprintf(cfg.Output, "NoColor value: %v\n", cfg.NoColor)
172
173 // Output:
174 // Output is stdout: true
175 // NoColor value: false
176 }
177
178 // ExampleColor_setUnset demonstrates using Set and Unset methods on a Color instance
179 // for temporary color changes in a thread-safe manner.
180 func ExampleColor_setUnset() {
181 // Create a configuration with color enabled but output to stdout
182 cfg := &color.Config{
183 NoColor: true, // Disabled for predictable test output
184 Output: os.Stdout,
185 }
186
187 // Create a color instance with the config
188 c := color.NewWithConfig(cfg, color.FgRed)
189
190 fmt.Print("Normal text, ")
191 c.Set()
192 fmt.Fprint(cfg.Output, "red text")
193 c.Unset()
194 fmt.Println(", back to normal")
195
196 // Output:
197 // Normal text, red text, back to normal
198 }
199
200 // ExampleColor_DisableColor demonstrates how to disable colors on individual
201 // Color instances using the thread-safe DisableColor method.
202 func ExampleColor_DisableColor() {
203 // Disable color globally via environment variable
204 os.Setenv("NO_COLOR", "true")
205
206 defer os.Unsetenv("NO_COLOR")
207
208 // Create color with default config (which respects NO_COLOR)
209 red := color.New(color.FgRed)
210 fmt.Println(red.Sprint("This should not be red"))
211
212 // Create a color with explicit config
213 cfg := &color.Config{
214 NoColor: true,
215 Output: os.Stdout,
216 }
217 yellow := color.NewWithConfig(cfg, color.FgYellow)
218 fmt.Println(yellow.Sprint("This yellow text is disabled"))
219
220 // Output:
221 // This should not be red
222 // This yellow text is disabled
223 }