-
Notifications
You must be signed in to change notification settings - Fork 12
Colored Output
Outputting color text consists of inserting ANSI color codes into a chosen string or text in some fashion and transmitting it to the client from the server.
ANSI Color codes are stored in a hashmap tying their names in lowercase to the equivalent code needed to indicate a change to that ANSI terminal color. Both the key and the value are strings. There are several methods, of which some may be redundant or slightly confusing. Their usage is explained, somewhat, below.
public String colorCode(String cc) { ... }
In order to use this, you pass a string names (e.g. "red") to the colorCode function which
returns a string containing the desired color code.
colorCode("red"); would return "\033[31m"
colors.get( ... );
This approach directly accesses the color codes stored in the hashmap as noted at the top
of this section. It is dependent on the existence of that hashmap under that name. Ideally
one would use the colorCode function above as it allows the flexibility of some other storage
mechanism or variable name as well as perhaps the use of a different color coding system.
Colors.<color name, all caps>.toString()
This method utilizes an enum class containing all the ANSI colors, its toString() method
synthesizes the appropriate code from the prefix, the color code itself, and the suffix
necessary for ANSI color.
public String colors(String arg, String cc) { ... }
Using this function is a great deal simply in some ways, you hand it the string to be
colored and the name of a color that exists and you get back a string with a color code
at either end. The front has the color code for the color specified and the back end the
one for white as a sort of color rest.
To get a colored string output, one would approach this as follows:
Using 's.write(...);' or 'client.write(...);' or even some version of 'send(...)' you would place a line of code within the string to output section such as:
send(colors.get("red") + "RED" + colors.get("white"), client);
OR
send(colors("RED", "red"), client);