|
21 | 21 | *
|
22 | 22 | * @author Rafael Luis Ibasco
|
23 | 23 | */
|
24 |
| -public class Console { |
| 24 | +public final class Console { |
25 | 25 |
|
26 | 26 | /** Constant <code>RESET="\u001B[0m"</code> */
|
27 | 27 | public static final String RESET = "\u001B[0m";
|
@@ -158,52 +158,113 @@ public static Colorize colorize() {
|
158 | 158 | return new Colorize();
|
159 | 159 | }
|
160 | 160 |
|
| 161 | + public static Colorize colorize(boolean allowPrinting) { |
| 162 | + return new Colorize(allowPrinting); |
| 163 | + } |
| 164 | + |
161 | 165 | public static class Colorize {
|
162 | 166 |
|
163 | 167 | private final StringBuilder builder;
|
164 | 168 |
|
| 169 | + private final boolean allowPrinting; |
| 170 | + |
| 171 | + private boolean allowColors; |
| 172 | + |
165 | 173 | private Colorize() {
|
166 |
| - builder = new StringBuilder(); |
| 174 | + this(Properties.isVerbose()); |
167 | 175 | }
|
168 | 176 |
|
169 |
| - public Colorize red() { |
170 |
| - builder.append(RED); |
| 177 | + private Colorize(boolean allowPrinting) { |
| 178 | + this.builder = new StringBuilder(); |
| 179 | + this.allowPrinting = allowPrinting; |
| 180 | + this.allowColors = true; |
| 181 | + } |
| 182 | + |
| 183 | + public Colorize enableColors() { |
| 184 | + this.allowColors = true; |
171 | 185 | return this;
|
172 | 186 | }
|
173 | 187 |
|
174 |
| - public Colorize yellow() { |
175 |
| - builder.append(YELLOW); |
| 188 | + public Colorize disableColors() { |
| 189 | + this.allowColors = false; |
176 | 190 | return this;
|
177 | 191 | }
|
178 | 192 |
|
| 193 | + public Colorize red() { |
| 194 | + return append(RED); |
| 195 | + } |
| 196 | + |
| 197 | + public Colorize red(String format, Object... args) { |
| 198 | + return append(RED, format, args); |
| 199 | + } |
| 200 | + |
| 201 | + public Colorize yellow() { |
| 202 | + return append(YELLOW); |
| 203 | + } |
| 204 | + |
| 205 | + public Colorize yellow(String format, Object... args) { |
| 206 | + return append(YELLOW, format, args); |
| 207 | + } |
| 208 | + |
| 209 | + public Colorize purple() { |
| 210 | + return append(PURPLE); |
| 211 | + } |
| 212 | + |
| 213 | + public Colorize purple(String format, Object... args) { |
| 214 | + return append(PURPLE, format, args); |
| 215 | + } |
| 216 | + |
179 | 217 | public Colorize cyan() {
|
180 |
| - builder.append(CYAN); |
| 218 | + return append(CYAN); |
| 219 | + } |
| 220 | + |
| 221 | + public Colorize cyan(String format, Object... args) { |
| 222 | + return append(CYAN, format, args); |
| 223 | + } |
| 224 | + |
| 225 | + public Colorize line() { |
| 226 | + builder.append(System.lineSeparator()); |
181 | 227 | return this;
|
182 | 228 | }
|
183 | 229 |
|
184 | 230 | public Colorize reset() {
|
185 |
| - builder.append(RESET); |
186 |
| - return this; |
| 231 | + return append(RESET); |
| 232 | + } |
| 233 | + |
| 234 | + public Colorize reset(String format, Object... args) { |
| 235 | + return append(RESET, format, args); |
187 | 236 | }
|
188 | 237 |
|
189 | 238 | public Colorize black() {
|
190 |
| - builder.append(BLACK); |
191 |
| - return this; |
| 239 | + return append(BLACK); |
| 240 | + } |
| 241 | + |
| 242 | + public Colorize black(String format, Object... args) { |
| 243 | + return append(BLACK, format, args); |
192 | 244 | }
|
193 | 245 |
|
194 | 246 | public Colorize white() {
|
195 |
| - builder.append(WHITE); |
196 |
| - return this; |
| 247 | + return append(WHITE); |
| 248 | + } |
| 249 | + |
| 250 | + public Colorize white(String format, Object... args) { |
| 251 | + return append(WHITE, format, args); |
197 | 252 | }
|
198 | 253 |
|
199 | 254 | public Colorize blue() {
|
200 |
| - builder.append(BLUE); |
201 |
| - return this; |
| 255 | + return append(BLUE); |
| 256 | + } |
| 257 | + |
| 258 | + public Colorize blue(String format, Object... args) { |
| 259 | + return append(BLUE, format, args); |
202 | 260 | }
|
203 | 261 |
|
204 | 262 | public Colorize green() {
|
205 |
| - builder.append(GREEN); |
206 |
| - return this; |
| 263 | + return append(GREEN); |
| 264 | + } |
| 265 | + |
| 266 | + public Colorize green(String format, Object... args) { |
| 267 | + return append(GREEN, format, args); |
207 | 268 | }
|
208 | 269 |
|
209 | 270 | public Colorize text(String text) {
|
@@ -232,28 +293,40 @@ public Colorize clear() {
|
232 | 293 | }
|
233 | 294 |
|
234 | 295 | public void print() {
|
235 |
| - if (Properties.isVerbose()) |
| 296 | + if (allowPrinting) |
236 | 297 | System.out.print(builder);
|
237 | 298 | }
|
238 | 299 |
|
239 | 300 | public void println() {
|
240 |
| - if (Properties.isVerbose()) |
| 301 | + if (allowPrinting) |
241 | 302 | System.out.println(builder);
|
242 | 303 | }
|
243 | 304 |
|
244 | 305 | public void printErr() {
|
245 |
| - if (Properties.isVerbose()) |
| 306 | + if (allowPrinting) |
246 | 307 | System.err.print(builder);
|
247 | 308 | }
|
248 | 309 |
|
249 | 310 | public void printErrln() {
|
250 |
| - if (Properties.isVerbose()) |
| 311 | + if (allowPrinting) |
251 | 312 | System.err.println(builder);
|
252 | 313 | }
|
253 | 314 |
|
254 | 315 | @Override
|
255 | 316 | public String toString() {
|
256 | 317 | return builder.toString();
|
257 | 318 | }
|
| 319 | + |
| 320 | + private Colorize append(String color) { |
| 321 | + return append(color, null); |
| 322 | + } |
| 323 | + |
| 324 | + private Colorize append(String color, String format, Object... args) { |
| 325 | + if (allowColors) |
| 326 | + builder.append(color); |
| 327 | + if (!Strings.isBlank(format)) |
| 328 | + builder.append(String.format(format, args)); |
| 329 | + return this; |
| 330 | + } |
258 | 331 | }
|
259 | 332 | }
|
0 commit comments