Debian Codegraph @

pkg/color/helpers.go

raw
1 package color
2
3 import "strings"
4
5 // Blackf is a convenient helper function to print with black foreground. A
6 // newline is appended to format by default.
7 func Blackf(format string, a ...any) {
8 colorPrint(format, FgBlack, a...)
9 }
10
11 // BlackString is a convenient helper function to return a string with black
12 // foreground.
13 func BlackString(format string, a ...any) string {
14 return colorString(format, FgBlack, a...)
15 }
16
17 // Bluef is a convenient helper function to print with blue foreground. A
18 // newline is appended to format by default.
19 func Bluef(format string, a ...any) {
20 colorPrint(format, FgBlue, a...)
21 }
22
23 // BlueString is a convenient helper function to return a string with blue
24 // foreground.
25 func BlueString(format string, a ...any) string {
26 return colorString(format, FgBlue, a...)
27 }
28
29 // Cyanf is a convenient helper function to print with cyan foreground. A
30 // newline is appended to format by default.
31 func Cyanf(format string, a ...any) {
32 colorPrint(format, FgCyan, a...)
33 }
34
35 // CyanString is a convenient helper function to return a string with cyan
36 // foreground.
37 func CyanString(format string, a ...any) string {
38 return colorString(format, FgCyan, a...)
39 }
40
41 // Greenf is a convenient helper function to print with green foreground. A
42 // newline is appended to format by default.
43 func Greenf(format string, a ...any) {
44 colorPrint(format, FgGreen, a...)
45 }
46
47 // GreenString is a convenient helper function to return a string with green
48 // foreground.
49 func GreenString(format string, a ...any) string {
50 return colorString(format, FgGreen, a...)
51 }
52
53 // HiBlackf is a convenient helper function to print with hi-intensity black foreground. A
54 // newline is appended to format by default.
55 func HiBlackf(format string, a ...any) {
56 colorPrint(format, FgHiBlack, a...)
57 }
58
59 // HiBlackString is a convenient helper function to return a string with hi-intensity black
60 // foreground.
61 func HiBlackString(format string, a ...any) string {
62 return colorString(format, FgHiBlack, a...)
63 }
64
65 // HiBluef is a convenient helper function to print with hi-intensity blue foreground. A
66 // newline is appended to format by default.
67 func HiBluef(format string, a ...any) {
68 colorPrint(format, FgHiBlue, a...)
69 }
70
71 // HiBlueString is a convenient helper function to return a string with hi-intensity blue
72 // foreground.
73 func HiBlueString(format string, a ...any) string {
74 return colorString(format, FgHiBlue, a...)
75 }
76
77 // HiCyanf is a convenient helper function to print with hi-intensity cyan foreground. A
78 // newline is appended to format by default.
79 func HiCyanf(format string, a ...any) {
80 colorPrint(format, FgHiCyan, a...)
81 }
82
83 // HiCyanString is a convenient helper function to return a string with hi-intensity cyan
84 // foreground.
85 func HiCyanString(format string, a ...any) string {
86 return colorString(format, FgHiCyan, a...)
87 }
88
89 // HiGreenf is a convenient helper function to print with hi-intensity green foreground. A
90 // newline is appended to format by default.
91 func HiGreenf(format string, a ...any) {
92 colorPrint(format, FgHiGreen, a...)
93 }
94
95 // HiGreenString is a convenient helper function to return a string with hi-intensity green
96 // foreground.
97 func HiGreenString(format string, a ...any) string {
98 return colorString(format, FgHiGreen, a...)
99 }
100
101 // HiMagentaf is a convenient helper function to print with hi-intensity magenta foreground.
102 // A newline is appended to format by default.
103 func HiMagentaf(format string, a ...any) {
104 colorPrint(format, FgHiMagenta, a...)
105 }
106
107 // HiMagentaString is a convenient helper function to return a string with hi-intensity magenta
108 // foreground.
109 func HiMagentaString(format string, a ...any) string {
110 return colorString(format, FgHiMagenta, a...)
111 }
112
113 // HiRedf is a convenient helper function to print with hi-intensity red foreground. A
114 // newline is appended to format by default.
115 func HiRedf(format string, a ...any) {
116 colorPrint(format, FgHiRed, a...)
117 }
118
119 // HiRedString is a convenient helper function to return a string with hi-intensity red
120 // foreground.
121 func HiRedString(format string, a ...any) string {
122 return colorString(format, FgHiRed, a...)
123 }
124
125 // HiYellowf is a convenient helper function to print with hi-intensity yellow foreground.
126 // A newline is appended to format by default.
127 func HiYellowf(format string, a ...any) {
128 colorPrint(format, FgHiYellow, a...)
129 }
130
131 // HiYellowString is a convenient helper function to return a string with hi-intensity yellow
132 // foreground.
133 func HiYellowString(format string, a ...any) string {
134 return colorString(format, FgHiYellow, a...)
135 }
136
137 // HiWhitef is a convenient helper function to print with hi-intensity white foreground. A
138 // newline is appended to format by default.
139 func HiWhitef(format string, a ...any) {
140 colorPrint(format, FgHiWhite, a...)
141 }
142
143 // HiWhiteString is a convenient helper function to return a string with hi-intensity white
144 // foreground.
145 func HiWhiteString(format string, a ...any) string {
146 return colorString(format, FgHiWhite, a...)
147 }
148
149 // Magentaf is a convenient helper function to print with magenta foreground.
150 // A newline is appended to format by default.
151 func Magentaf(format string, a ...any) {
152 colorPrint(format, FgMagenta, a...)
153 }
154
155 // MagentaString is a convenient helper function to return a string with magenta
156 // foreground.
157 func MagentaString(format string, a ...any) string {
158 return colorString(format, FgMagenta, a...)
159 }
160
161 // Redf is a convenient helper function to print with red foreground. A
162 // newline is appended to format by default.
163 func Redf(format string, a ...any) {
164 colorPrint(format, FgRed, a...)
165 }
166
167 // RedString is a convenient helper function to return a string with red
168 // foreground.
169 func RedString(format string, a ...any) string {
170 return colorString(format, FgRed, a...)
171 }
172
173 // Whitef is a convenient helper function to print with white foreground. A
174 // newline is appended to format by default.
175 func Whitef(format string, a ...any) {
176 colorPrint(format, FgWhite, a...)
177 }
178
179 // WhiteString is a convenient helper function to return a string with white
180 // foreground.
181 func WhiteString(format string, a ...any) string {
182 return colorString(format, FgWhite, a...)
183 }
184
185 // Yellowf is a convenient helper function to print with yellow foreground.
186 // A newline is appended to format by default.
187 func Yellowf(format string, a ...any) {
188 colorPrint(format, FgYellow, a...)
189 }
190
191 // YellowString is a convenient helper function to return a string with yellow
192 // foreground.
193 func YellowString(format string, a ...any) string {
194 return colorString(format, FgYellow, a...)
195 }
196
197 // colorPrint prints formatted text with the specified color attribute.
198 // Uses the default configuration for backward compatibility.
199 func colorPrint(format string, attribute Attribute, arguments ...any) {
200 c := New(attribute)
201
202 if !strings.HasSuffix(format, "\n") {
203 format += "\n"
204 }
205
206 if len(arguments) == 0 {
207 _, _ = c.Print(format)
208 } else {
209 _, _ = c.Printf(format, arguments...)
210 }
211 }
212
213 // colorString returns a formatted string with the specified color attribute.
214 // Uses the default configuration for backward compatibility.
215 func colorString(format string, attribute Attribute, arguments ...any) string {
216 c := New(attribute)
217
218 if len(arguments) == 0 {
219 return c.SprintFunc()(format)
220 }
221
222 return c.SprintfFunc()(format, arguments...)
223 }