Last week, Slobber asked on client channel if it was possible to add colors to output in Batclient triggers. Some time ago, I explored the possibilities for this, and made some code to use in my plugin project. The project itself is far from finished, but I thought it'd be useful to share this bit of code which I use in my base class for my trigger handlers.

There's two ways to use this: print colored text-lines to a client window (println), or add ansi colors to a String (addColor).
I've only tested the println method, wrote the addColor method for this post and it should work in my opinion.

I tried to attach the .java class file, but couldn't get it done with the forum application, so you'll have to copy paste it from the second code box below.

To use the code:
- get the BaseHandler compiled
- import it in your class (or extend your class with BaseHandler)
- Instantiate a BaseHandler object
- Call the method you need.

For example:
[codebox]
package myplugin;
import biz.noorlander.batclient.BaseHandler;
import com.mythicscape.batclient.interfaces.ClientGUI;

public class MyClass {

private static BaseHandler myB = new BaseHandler();

public MyClass(ClientGUI myGui) {
super();
myB.setGUI(myGui);
}

public void myMethod() {
String myColoredText = myB.addColor("Some text here", BaseHandler.ANSI_LIGHT_RED);
myB.println("generic","Echo to client in green!", ANSI_GREEN);
}

}
[/codebox]

The code:

[codebox]package biz.noorlander.batclient;

import com.mythicscape.batclient.interfaces.ClientGUI;

public class BaseHandler {
private static final char escapeCode = (char)27;
private static final String ANSI_BOLD_ON = "[1m";
private static final String ANSI_BOLD_OFF = "[22m";
private static final String ANSI_DEFAULT = "[0m";

private static String[] colorCodes = {"[0m","[30m","[31m","[32m","[33m","[34m","[35m","[36m","[37m","[38m","[39m"};

public static final int ANSI_BLACK = 1;
public static final int ANSI_RED = 2;
public static final int ANSI_GREEN = 3;
public static final int ANSI_YELLOW = 4;
public static final int ANSI_BLUE = 5;
public static final int ANSI_MAGENTA = 6;
public static final int ANSI_CYAN = 7;
public static final int ANSI_WHITE = 8;

public static final int ANSI_LIGHT_BLACK = 11;
public static final int ANSI_LIGHT_RED = 12;
public static final int ANSI_LIGHT_GREEN = 13;
public static final int ANSI_LIGHT_YELLOW = 14;
public static final int ANSI_LIGHT_BLUE = 15;
public static final int ANSI_LIGHT_MAGENTA = 16;
public static final int ANSI_LIGHT_CYAN = 17;
public static final int ANSI_LIGHT_WHITE = 18;

protected static ClientGUI batGUI = null;

public static void setGUI(ClientGUI myGui) {
batGUI = myGui;
}

public static void error(String text) {
if (batGUI != null)
batGUI.printText("generic", "ERROR: "+text + "\n");
}

public static void println(String channel, String text, int colorId) {
if (batGUI != null)
if (colorId == 0) {
batGUI.printText(channel, text + "\n");
} else {
if (colorId > 10)
batGUI.printText(channel, escapeCode + ANSI_BOLD_ON + escapeCode + colorCodes[colorId - 10] + text + escapeCode + ANSI_BOLD_OFF + escapeCode + ANSI_DEFAULT +"\n");
else
batGUI.printText(channel, escapeCode + colorCodes[colorId] + text + escapeCode + ANSI_DEFAULT + "\n");

}
}

public static String addColor(String text, int colorId) {
if (colorId < 1 || (colorId > 8 && colorId < 11) || colorId > 18) {
// Invalid code, return original text
return text;
}
if (colorId > 10) // "light" color so turn on ansi bold
return escapeCode + ANSI_BOLD_ON + escapeCode + colorCodes[colorId - 10] + text + escapeCode + ANSI_BOLD_OFF + escapeCode + ANSI_DEFAULT;
else
return escapeCode + colorCodes[colorId] + text + escapeCode + ANSI_DEFAULT;
}
}
[/codebox]