Class Ansi
- java.lang.Object
-
- com.strobel.io.Ansi
-
public class Ansi extends java.lang.ObjectAnsi coloring support is provided by this class.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.
same can be done as below:out("hello ansi world")String msg = ansi.
Ansi Support:colorize("hello ansi world"); // msg is original string wrapped with ansi control sequences System.out.println(msg);Ansi might not be supported on all systems. Ansi is mostly supported by all unix operating systems.
SUPPORTEDis 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=trueor-DAnsi=false
ifSUPPORTEDis 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 doSystem.out.print("hello ansi world")
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classAnsi.AnsiColorstatic classAnsi.Attributethis enum represents the attribute of textstatic classAnsi.Colorthis enum represents the color of text
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.StringENDprivate static java.lang.StringPREFIXprivate static java.lang.StringSEPARATORprivate java.lang.Stringstartprivate static java.lang.StringSUFFIXstatic booleanSUPPORTEDspecifies whether ansi is supported or not.private static java.lang.StringXTERM_256_SEPARATOR
-
Constructor Summary
Constructors Constructor Description Ansi(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.Ansi(java.lang.String format)Creates new instanceof of ansi with specified format.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.Stringcolorize(java.lang.String message)Wraps givenmessagewith special ansi control sequences and returns itvoiderr(java.lang.String message)Prints colorizedmessagetoSystem.errvoiderrFormat(java.lang.String format, java.lang.Object... args)Prints formatted and colorizedformattoSystem.errvoiderrLine(java.lang.String message)Prints colorizedmessagetoSystem.errfollowed by newlinevoidformat(java.io.PrintStream ps, java.lang.String format, java.lang.Object... args)Prints formatted and colorizedmessageto specifiedps.private voidinit(Ansi.Attribute attr, Ansi.AnsiColor foreground, Ansi.AnsiColor background)voidout(java.lang.String message)Prints colorizedmessagetoSystem.outvoidoutFormat(java.lang.String format, java.lang.Object... args)Prints formatted and colorizedformattoSystem.outvoidoutLine(java.lang.String message)Prints colorizedmessagetoSystem.outfollowed by newlinevoidprint(java.io.PrintStream ps, java.lang.String message)Prints colorizedmessageto specifiedps.voidprintln(java.io.PrintStream ps, java.lang.String message)Prints colorizedmessageto specifiedpsfollowed by newline.java.lang.StringtoString()The string representation of this object.
-
-
-
Field Detail
-
SUPPORTED
public static final boolean SUPPORTED
specifies 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
private static final java.lang.String PREFIX
- See Also:
- Constant Field Values
-
SUFFIX
private static final java.lang.String SUFFIX
- See Also:
- Constant Field Values
-
XTERM_256_SEPARATOR
private static final java.lang.String XTERM_256_SEPARATOR
- See Also:
- Constant Field Values
-
SEPARATOR
private static final java.lang.String SEPARATOR
- See Also:
- Constant Field Values
-
END
private static final java.lang.String END
- See Also:
- Constant Field Values
-
start
private java.lang.String start
-
-
Constructor Detail
-
Ansi
public Ansi(Ansi.Attribute attr, Ansi.Color foreground, Ansi.Color background)
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
public Ansi(Ansi.Attribute attr, Ansi.AnsiColor foreground, Ansi.AnsiColor background)
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
public Ansi(java.lang.String format)
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 Detail
-
init
private void init(Ansi.Attribute attr, Ansi.AnsiColor foreground, Ansi.AnsiColor background)
-
toString
public java.lang.String toString()
The string representation of this object. This string will be the same that is expected byAnsi(String)- Overrides:
toStringin classjava.lang.Object- Returns:
- string representation of this object
-
colorize
public java.lang.String colorize(java.lang.String message)
Wraps givenmessagewith special ansi control sequences and returns it
-
print
public void print(java.io.PrintStream ps, java.lang.String message)- Parameters:
ps- stream to printmessage- message to be colorized
-
println
public void println(java.io.PrintStream ps, java.lang.String message)Prints colorizedmessageto specifiedpsfollowed by newline.if
SUPPORTEDis false, it prints rawmessagetopsfollowed by newline.- Parameters:
ps- stream to printmessage- message to be colorized
-
format
public void format(java.io.PrintStream ps, java.lang.String format, java.lang.Object... args)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
public void out(java.lang.String message)
Prints colorizedmessagetoSystem.out- Parameters:
message- message to be colorized
-
outLine
public void outLine(java.lang.String message)
Prints colorizedmessagetoSystem.outfollowed by newline- Parameters:
message- message to be colorized
-
outFormat
public void outFormat(java.lang.String format, java.lang.Object... args)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
public void err(java.lang.String message)
Prints colorizedmessagetoSystem.err- Parameters:
message- message to be colorized
-
errLine
public void errLine(java.lang.String message)
Prints colorizedmessagetoSystem.errfollowed by newline- Parameters:
message- message to be colorized
-
errFormat
public void errFormat(java.lang.String format, java.lang.Object... args)Prints formatted and colorizedformattoSystem.err- Parameters:
format- A format string whose output to be colorizedargs- Arguments referenced by the format specifiers in the format
-
-