Class Ansi
To print "hello ansi world" in bold with blue foreground and white background:
Ansi ansi = new
Ansi(Ansi.Attribute.BRIGHT, Ansi.Color.BLUE, Ansi.Color.WHITE); ansi.out("hello ansi world")
same can be done as below: String msg = ansi.colorize("hello ansi world"); // msg is original string wrapped with ansi
control sequences System.out.println(msg);
Ansi Support: Ansi might not be supported on all systems. Ansi is mostly supported by all unix operating systems.
SUPPORTED
is a final boolean, that can be used to check whether your console supports Ansi format;
Ansi class uses simple checks to decide whether ansi is
supported or not. Sometimes it may do wrong guess. In such cases you can override its decision using following system property: -DAnsi=true or
-DAnsi=false
if SUPPORTED is false, any ansi method will not produce ansi control sequences. so you can safely use:
ansi.out("hello ansi world") irrespective of ansi is supported or not. if ansi is not supported, this will simply do
System.out.print("hello ansi world")
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classstatic enumthis enum represents the attribute of textstatic enumthis enum represents the color of text -
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionAnsi(Ansi.Attribute attr, Ansi.AnsiColor foreground, Ansi.AnsiColor background) Creates new instanceof Ansi.Ansi(Ansi.Attribute attr, Ansi.Color foreground, Ansi.Color background) Creates new instanceof Ansi.Creates new instanceof of ansi with specified format. -
Method Summary
Modifier and TypeMethodDescriptionWraps givenmessagewith special ansi control sequences and returns itvoidPrints colorizedmessagetoSystem.errvoidPrints formatted and colorizedformattoSystem.errvoidPrints colorizedmessagetoSystem.errfollowed by newlinevoidformat(PrintStream ps, String format, Object... args) Prints formatted and colorizedmessageto specifiedps.private voidinit(Ansi.Attribute attr, Ansi.AnsiColor foreground, Ansi.AnsiColor background) voidPrints colorizedmessagetoSystem.outvoidPrints formatted and colorizedformattoSystem.outvoidPrints colorizedmessagetoSystem.outfollowed by newlinevoidprint(PrintStream ps, String message) Prints colorizedmessageto specifiedps.voidprintln(PrintStream ps, String message) Prints colorizedmessageto specifiedpsfollowed by newline.toString()The string representation of this object.
-
Field Details
-
SUPPORTED
public static final boolean SUPPORTEDspecifies whether ansi is supported or not.
when this is false, it doesn't colorize given strings, rather than simply returns the given strings
It tries best effort to guess whether ansi is supported or not. But you can override this value using system property "Ansi" (-DAnsi=true/false) -
PREFIX
- See Also:
-
SUFFIX
- See Also:
-
XTERM_256_SEPARATOR
- See Also:
-
SEPARATOR
- See Also:
-
END
- See Also:
-
start
-
-
Constructor Details
-
Ansi
Creates new instanceof Ansi.- Parameters:
attr- attribute of text, null means don't changeforeground- foreground color of text, null means don't changebackground- background color of text, null means don't change
-
Ansi
Creates new instanceof Ansi.- Parameters:
attr- attribute of text, null means don't changeforeground- foreground color of text, null means don't changebackground- background color of text, null means don't change
-
Ansi
Creates new instanceof of ansi with specified format.The format syntax is
Attribute[;Foreground[;Background]]
i.e, semicolon(;) separated values, where tokens are attribute, foreground and background respectively.
if any non-trailing token in value is null, you still need to specify empty value. for example:DIM;;GREEN # foreground is not specified
-
-
Method Details
-
init
-
toString
The string representation of this object. This string will be the same that is expected byAnsi(String) -
colorize
-
print
- Parameters:
ps- stream to printmessage- message to be colorized
-
println
Prints colorizedmessageto specifiedpsfollowed by newline.if
SUPPORTEDis false, it prints rawmessagetopsfollowed by newline.- Parameters:
ps- stream to printmessage- message to be colorized
-
format
Prints formatted and colorizedmessageto specifiedps.if
SUPPORTEDis false, it prints formattedmessagetops- Parameters:
ps- stream to printformat- A format string whose output to be colorizedargs- Arguments referenced by the format specifiers in the format
-
out
Prints colorizedmessagetoSystem.out- Parameters:
message- message to be colorized
-
outLine
Prints colorizedmessagetoSystem.outfollowed by newline- Parameters:
message- message to be colorized
-
outFormat
Prints formatted and colorizedformattoSystem.out- Parameters:
format- A format string whose output to be colorizedargs- Arguments referenced by the format specifiers in the format
-
err
Prints colorizedmessagetoSystem.err- Parameters:
message- message to be colorized
-
errLine
Prints colorizedmessagetoSystem.errfollowed by newline- Parameters:
message- message to be colorized
-
errFormat
Prints formatted and colorizedformattoSystem.err- Parameters:
format- A format string whose output to be colorizedargs- Arguments referenced by the format specifiers in the format
-