public class Ansi extends Object
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")
AnsiFormatter| Modifier and Type | Class and Description |
|---|---|
static class |
Ansi.Attribute
this enum represents the attribute of text
|
static class |
Ansi.Color
this enum represents the color of text
|
| Modifier and Type | Field and Description |
|---|---|
static boolean |
SUPPORTED
specifies whether ansi is supported or not.
|
| Constructor and Description |
|---|
Ansi(Ansi.Attribute attr,
Ansi.Color foreground,
Ansi.Color background)
Creates new instanceof Ansi.
|
Ansi(String format)
Creates new instanceof of ansi with specified format.
|
| Modifier and Type | Method and Description |
|---|---|
String |
colorize(String message)
Wrapps given
message with special ansi control sequences and returns it |
void |
err(String message)
Prints colorized
message to System.err |
void |
errFormat(String format,
Object... args)
Prints formatted and colorized
format to System.err |
void |
errln(String message)
Prints colorized
message to System.err followed by newline |
void |
format(PrintStream ps,
String format,
Object... args)
Prints formatted and colorized
message to specified ps. |
void |
out(String message)
Prints colorized
message to System.out |
void |
outFormat(String format,
Object... args)
Prints formatted and colorized
format to System.out |
void |
outln(String message)
Prints colorized
message to System.out followed by newline |
void |
print(PrintStream ps,
String message)
Prints colorized
message to specified ps. |
void |
println(PrintStream ps,
String message)
Prints colorized
message to specified ps followed by newline. |
String |
toString()
The string representation of this object.
|
public static final boolean SUPPORTED
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)
public Ansi(Ansi.Attribute attr, Ansi.Color foreground, Ansi.Color background)
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 changepublic Ansi(String format)
The format syntax is
Attribute[;Foreground[;Background]]i.e, semicolon(;) separated values, where tokens are attribute, foreground and background respectively.
DIM;;GREEN # foreground is not specified
format - public String toString()
Ansi(String)public String colorize(String message)
message with special ansi control sequences and returns itpublic void print(PrintStream ps, String message)
ps - stream to printmessage - message to be colorizedpublic void println(PrintStream ps, String message)
message to specified ps followed by newline.
if SUPPORTED is false, it prints raw message to ps followed by newline.
ps - stream to printmessage - message to be colorizedpublic void format(PrintStream ps, String format, Object... args)
message to specified ps.
if SUPPORTED is false, it prints formatted message to ps
ps - stream to printformat - A format string whose output to be colorizedargs - Arguments referenced by the format specifiers in the formatpublic void out(String message)
message to System.outmessage - message to be colorizedpublic void outln(String message)
message to System.out followed by newlinemessage - message to be colorizedpublic void outFormat(String format, Object... args)
format to System.outformat - A format string whose output to be colorizedargs - Arguments referenced by the format specifiers in the formatpublic void err(String message)
message to System.errmessage - message to be colorizedpublic void errln(String message)
message to System.err followed by newlinemessage - message to be colorizedpublic void errFormat(String format, Object... args)
format to System.errformat - A format string whose output to be colorizedargs - Arguments referenced by the format specifiers in the formatCopyright © 2021. All rights reserved.