I know there has to be a better way for me to do this. I have an inputstream that is being updated from a connection to a server and as it is read it is converted to char's and put into a StringBuffer. From the StringBuffer I look for the ESC character or ASCII #27. When I find that character I pass a substring on to another method to look though and remove the text before that from the stringbuffer. In the substring I search for 'm', the terminating character, if it isn't found I haven't done anything yet (I have no idea but it will pass it back to the be added to the display). In that substring I extract the numbers after checking if ther is a simicolon in it (signifing more than one number in the code) and according to what I find I set the color (ExtraColor is just Java.awt.Color with an extra field for boolean isBold).
The problem with the current one is I know it contains a few
wtf's and it sometimes leaves the 'm' behind and doesn't always get the color right. Would someone look though it and see where I've gone wrong? I know this is terribly long explanation and thank you for reading it.
private void manageColor()
{
boolean done = false;
while(!done)
{
int index = sb.indexOf(char27.toString()+ "[");
if(index != 0)
{
StringBuffer temp = new StringBuffer(sb.substring(0,index));
cf.appendDisplay(temp.toString());
System.err.println(temp.toString());
}
sb = new StringBuffer(sb.substring(index));
if(debug) System.err.println("Color: " + sb.substring(2,sb.indexOf("m")+1));
extraColor col = findColor(sb.substring(2,sb.indexOf("m")+1).toString());
if(col == null)
cf.resetColor();
else if(col.isBright)
cf.boldColor(col.toColor());
else
cf.setColor(col.toColor());
//cut off last set of char27 [#;##
sb = new StringBuffer(sb.substring(sb.indexOf("m")));
if(sb.indexOf(char27.toString()+ "[") == -1)
{
done = true;
cf.appendDisplay(sb.toString());
}
if(!done)
//safe to cut off m
sb = new StringBuffer(sb.substring(1));
}
}
private extraColor findColor(String escape)
{
boolean isSimicolened = false;
boolean bold = false;
int color;
extraColor col;
for(int i = 0; i<escape.length(); i++)
{
if(escape.charAt(i) == ';')
isSimicolened = true;
}
if(!isSimicolened)
{
if(debug) System.err.println("\tescape: "+escape);
color = extractNum(escape);
}
else
{
if(debug) System.err.println("\tescape: "+escape);
bold = (escape.charAt(0) == '0') ? false : true;
color = extractNum(escape.substring(2));
}
switch (color)
{
case 0:
col = null; //means revert to previous color
break;
case 30:
col = new extraColor(Color.GRAY, bold); //nonstandard, but nessecary
break;
case 31:
col = new extraColor(Color.RED, bold);
break;
case 32:
col = new extraColor(Color.GREEN, bold);
break;
case 33:
col = new extraColor(Color.YELLOW, bold);
break;
case 34:
col = new extraColor(Color.BLUE, bold);
break;
case 35:
col = new extraColor(Color.MAGENTA, bold);
break;
case 36:
col = new extraColor(Color.CYAN, bold);
break;
case 37:
col = new extraColor(Color.WHITE, bold);
break;
case 39:
col = new extraColor(Color.WHITE, bold);
break;
default:
col = new extraColor(Color.WHITE);
}
return col;
}
private int extractNum(String seq)
{
StringBuilder value = new StringBuilder();
for(int i = 0; i >< seq.length(); i++)
{
if(Character.isDigit(seq.charAt(i)))
value.append(seq.charAt(i));
if(seq.charAt(i) == 'm')
i = 10; //always greater than seq.length
}
return Integer.parseInt(value.toString());
}