jlibs.core.lang
Class Ansi

java.lang.Object
  extended by jlibs.core.lang.Ansi

public class Ansi
extends Object

Ansi 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.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")

Author:
Santhosh Kumar T
See Also:
AnsiFormatter

Nested Class Summary
static class Ansi.Attribute
          this enum represents the attribute of text
static class Ansi.Color
          this enum represents the color of text
 
Field Summary
static boolean SUPPORTED
          specifies whether ansi is supported or not.
 
Constructor Summary
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.
 
Method Summary
 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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

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)

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 change
foreground - foreground color of text, null means don't change
background - background color of text, null means don't change

Ansi

public Ansi(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
 

Parameters:
format -
Method Detail

toString

public String toString()
The string representation of this object. This string will be the same that is expected by Ansi(String)

Overrides:
toString in class Object
Returns:
string representation of this object

colorize

public String colorize(String message)
Wrapps given message with special ansi control sequences and returns it


print

public void print(PrintStream ps,
                  String message)
Prints colorized message to specified ps.

if SUPPORTED is false, it prints raw message to ps

Parameters:
ps - stream to print
message - message to be colorized

println

public void println(PrintStream ps,
                    String message)
Prints colorized message to specified ps followed by newline.

if SUPPORTED is false, it prints raw message to ps followed by newline.

Parameters:
ps - stream to print
message - message to be colorized

format

public void format(PrintStream ps,
                   String format,
                   Object... args)
Prints formatted and colorized message to specified ps.

if SUPPORTED is false, it prints formatted message to ps

Parameters:
ps - stream to print
format - A format string whose output to be colorized
args - Arguments referenced by the format specifiers in the format

out

public void out(String message)
Prints colorized message to System.out

Parameters:
message - message to be colorized

outln

public void outln(String message)
Prints colorized message to System.out followed by newline

Parameters:
message - message to be colorized

outFormat

public void outFormat(String format,
                      Object... args)
Prints formatted and colorized format to System.out

Parameters:
format - A format string whose output to be colorized
args - Arguments referenced by the format specifiers in the format

err

public void err(String message)
Prints colorized message to System.err

Parameters:
message - message to be colorized

errln

public void errln(String message)
Prints colorized message to System.err followed by newline

Parameters:
message - message to be colorized

errFormat

public void errFormat(String format,
                      Object... args)
Prints formatted and colorized format to System.err

Parameters:
format - A format string whose output to be colorized
args - Arguments referenced by the format specifiers in the format


Copyright © 2018. All rights reserved.