2
2
3
3
import org .jetbrains .annotations .NotNull ;
4
4
import org .jetbrains .annotations .Nullable ;
5
+ import textFormatter .color .Color ;
6
+ import textFormatter .color .SimpleColor ;
5
7
6
8
import java .util .ArrayList ;
7
9
import java .util .Arrays ;
8
10
import java .util .List ;
9
11
10
12
/**
11
- * Allows to easily format text for display in a terminal.
13
+ * Allows easily formatting of text for it to be displayed in a terminal.
12
14
* <p>
13
15
* Multiple formatters can be concatenated together. This is useful for when you want to
14
16
* format a string that has multiple parts that need to be formatted differently.
@@ -18,14 +20,17 @@ public class TextFormatter {
18
20
/**
19
21
* When set to {@code false}, no formatting will be applied to text. Raw text will be generated without any
20
22
* color or formatting.
23
+ * <p>
24
+ * This will be set to {@code false} if the environment variable {@code NO_COLOR} is set.
25
+ * @see #isColorDisabledEnv()
21
26
*/
22
- public static boolean enableSequences = true ;
27
+ public static boolean enableSequences = ! TextFormatter . isColorDisabledEnv () ;
23
28
24
29
/**
25
30
* The default color that should be used when no foreground color is specified (if {@link #startWithDefaultColorIfNotDefined}
26
31
* is set to {@code true}), or when the foreground color is reset.
27
32
*/
28
- public static @ NotNull Color defaultColor = Color .BRIGHT_WHITE ;
33
+ public static @ NotNull Color defaultColor = SimpleColor .BRIGHT_WHITE ;
29
34
30
35
/**
31
36
* When set to {@code true}, the default color will be used when no foreground color is specified.
@@ -103,7 +108,7 @@ public static TextFormatter create() {
103
108
* @return a new {@link TextFormatter} with the specified contents and the error formatting
104
109
* */
105
110
public static @ NotNull TextFormatter error (@ NotNull String msg ) {
106
- return TextFormatter .of (msg , Color .BLACK , Color .BRIGHT_RED ).addFormat (FormatOption .BOLD );
111
+ return TextFormatter .of (msg , SimpleColor .BLACK , SimpleColor .BRIGHT_RED ).addFormat (FormatOption .BOLD );
107
112
}
108
113
109
114
@@ -287,7 +292,7 @@ else if (TextFormatter.startWithDefaultColorIfNotDefined && this.parent == null)
287
292
288
293
/**
289
294
* Returns the {@link Color} that should properly reset the foreground color. This is determined by looking at the
290
- * parent formatters. If no parent formatter has a foreground color, then {@link Color #BRIGHT_WHITE} is returned.
295
+ * parent formatters. If no parent formatter has a foreground color, then {@link SimpleColor #BRIGHT_WHITE} is returned.
291
296
* @return the {@link Color} that should properly reset the foreground color
292
297
*/
293
298
private @ Nullable Color getResetFgColor () {
@@ -359,19 +364,40 @@ else if (TextFormatter.startWithDefaultColorIfNotDefined && this.parent == null)
359
364
}
360
365
361
366
/**
362
- * Returns a string with a terminal sequence with the specified code .
363
- * (e.g. {@code "ESC[<code here>m"})
367
+ * Returns a string with a terminal sequence with the specified values, separated by a semicolon .
368
+ * (e.g. {@code "ESC[<values here>m"})
364
369
* <p>
365
370
* If {@link #debug} is set to {@code true}, then the text "ESC" will be used instead of the actual escape
366
371
* character.
367
372
* </p>
368
- * @param code The code of the sequence.
369
- * @return a string with a terminal sequence with the specified code
373
+ * If {@link #enableSequences} is set to {@code false}, then an empty string will be returned.
374
+ * @param values The values to add to the terminal sequence.
375
+ * @return a string with a terminal sequence with the specified values
370
376
*/
371
- static @ NotNull String getSequence (int code ) {
377
+ public static @ NotNull String getSequence (@ NotNull Object ... values ) {
378
+ if (!TextFormatter .enableSequences )
379
+ return "" ;
380
+
381
+ var joined = String .join (
382
+ ";" ,
383
+ Arrays .stream (values )
384
+ .map (Object ::toString )
385
+ .toArray (String []::new )
386
+ );
387
+
372
388
if (TextFormatter .debug )
373
- return "ESC[" + code + "]" ;
374
- return "" + ESC + '[' + code + 'm' ;
389
+ return "ESC[" + joined + "]" ;
390
+ return "" + ESC + '[' + joined + 'm' ;
391
+ }
392
+
393
+ /**
394
+ * Returns whether there is an environment variable that specifies that
395
+ * the terminal does not support color.
396
+ * <a href="https://no-color.org/">NO_COLOR.org</a>
397
+ * @return {@code true} if the terminal supports color
398
+ */
399
+ public static boolean isColorDisabledEnv () {
400
+ return System .getenv ("NO_COLOR" ) != null ;
375
401
}
376
402
377
403
/**
0 commit comments