22
33import org .jetbrains .annotations .NotNull ;
44import org .jetbrains .annotations .Nullable ;
5+ import textFormatter .color .Color ;
6+ import textFormatter .color .SimpleColor ;
57
68import java .util .ArrayList ;
79import java .util .Arrays ;
810import java .util .List ;
911
1012/**
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.
1214 * <p>
1315 * Multiple formatters can be concatenated together. This is useful for when you want to
1416 * format a string that has multiple parts that need to be formatted differently.
@@ -18,14 +20,17 @@ public class TextFormatter {
1820 /**
1921 * When set to {@code false}, no formatting will be applied to text. Raw text will be generated without any
2022 * 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()
2126 */
22- public static boolean enableSequences = true ;
27+ public static boolean enableSequences = ! TextFormatter . isColorDisabledEnv () ;
2328
2429 /**
2530 * The default color that should be used when no foreground color is specified (if {@link #startWithDefaultColorIfNotDefined}
2631 * is set to {@code true}), or when the foreground color is reset.
2732 */
28- public static @ NotNull Color defaultColor = Color .BRIGHT_WHITE ;
33+ public static @ NotNull Color defaultColor = SimpleColor .BRIGHT_WHITE ;
2934
3035 /**
3136 * 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() {
103108 * @return a new {@link TextFormatter} with the specified contents and the error formatting
104109 * */
105110 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 );
107112 }
108113
109114
@@ -287,7 +292,7 @@ else if (TextFormatter.startWithDefaultColorIfNotDefined && this.parent == null)
287292
288293 /**
289294 * 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.
291296 * @return the {@link Color} that should properly reset the foreground color
292297 */
293298 private @ Nullable Color getResetFgColor () {
@@ -359,19 +364,40 @@ else if (TextFormatter.startWithDefaultColorIfNotDefined && this.parent == null)
359364 }
360365
361366 /**
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"})
364369 * <p>
365370 * If {@link #debug} is set to {@code true}, then the text "ESC" will be used instead of the actual escape
366371 * character.
367372 * </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
370376 */
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+
372388 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 ;
375401 }
376402
377403 /**
0 commit comments