Permalink
Browse files

Colorizing output.

  • Loading branch information...
dblock committed Sep 3, 2011
1 parent 3dbc49d commit 3963bff40ed499aaf9c2d73785c5e27d6d63b941
@@ -29,8 +29,6 @@
import java.io.OutputStream;
import java.nio.charset.Charset;
import org.fusesource.jansi.AnsiString;
/**
* Time-stamp note that is inserted into the console output.
*
@@ -42,22 +40,15 @@
* Serialization UID.
*/
private static final long serialVersionUID = 1L;
private final OutputStream out;
private final Charset charset;
private final AnsiHtmlOutputStream out;
public AnsiColorizer(OutputStream out, Charset charset) {
this.out = out;
this.charset = charset;
this.out = new AnsiHtmlOutputStream(out);
}
@Override
protected void eol(byte[] b, int len) throws IOException {
String ansiEncodedString = new String(b, 0, len, charset);
AnsiString ansiString = new AnsiString(ansiEncodedString);
String plainString = ansiString.getPlain().toString();
byte[] plainBytes = plainString.getBytes();
out.write(plainBytes, 0, plainBytes.length);
out.writeLine(b, 0, len);
}
@Override
@@ -0,0 +1,84 @@
package hudson.plugins.ansicolor;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import org.fusesource.jansi.AnsiOutputStream;
public class AnsiHtmlOutputStream extends AnsiOutputStream {
@Override
public void close() throws IOException {
closeAttributes();
super.close();
}
private static final String ANSI_COLOR_MAP[] = { "black", "red",
"green", "yellow", "blue", "magenta", "cyan", "white", };
public AnsiHtmlOutputStream(OutputStream os) {
super(os);
}
private List<String> closingAttributes = new ArrayList<String>();
private void write(String s) throws IOException {
super.out.write(s.getBytes());
}
private void writeAttribute(String s) throws IOException {
write("<" + s + ">");
closingAttributes.add(0, s.split(" ", 2)[0]);
}
private void closeAttributes() throws IOException {
for (String attr : closingAttributes) {
write("</" + attr + ">");
}
closingAttributes.clear();
}
public void writeLine(byte[] buf, int offset, int len) throws IOException {
write(buf, offset, len);
closeAttributes();
}
@Override
protected void processSetAttribute(int attribute) throws IOException {
switch (attribute) {
case ATTRIBUTE_INTENSITY_BOLD:
writeAttribute("b");
break;
case ATTRIBUTE_INTENSITY_NORMAL:
closeAttributes();
break;
case ATTRIBUTE_UNDERLINE:
writeAttribute("u");
break;
case ATTRIBUTE_UNDERLINE_OFF:
closeAttributes();
break;
case ATTRIBUTE_NEGATIVE_ON:
break;
case ATTRIBUTE_NEGATIVE_Off:
break;
}
}
@Override
protected void processAttributeRest() throws IOException {
closeAttributes();
}
@Override
protected void processSetForegroundColor(int color) throws IOException {
writeAttribute("font color=\"" + ANSI_COLOR_MAP[color] + "\"");
}
@Override
protected void processSetBackgroundColor(int color) throws IOException {
writeAttribute("span style=\"background-color: " + ANSI_COLOR_MAP[color] + "\"");
}
}
@@ -37,25 +37,43 @@
*/
public class AnsiColorizerTest {
/**
* @throws IOException
*/
@Test
public void testNoMarkup() throws IOException {
assertThat(colorize("line"), is("line"));
}
@Test
public void testClear() throws IOException {
assertThat(colorize(""), is(""));
assertThat(colorize("hello world"), is("hello world"));
}
private String colorize(String text) throws IOException {
StringOutputStream out = new StringOutputStream();
AnsiColorizer colorizer = new AnsiColorizer(out, Charset.defaultCharset());
colorizer.eol(text.getBytes(), text.length());
return out.toString();
}
/**
* @throws IOException
*/
@Test
public void testNoMarkup() throws IOException {
assertThat(colorize("line"), is("line"));
}
@Test
public void testClear() throws IOException {
assertThat(colorize(""), is(""));
assertThat(colorize("hello world"), is("hello world"));
}
@Test
public void testBold() throws IOException {
assertThat(colorize("hello world"), is("<b>hello world</b>"));
}
@Test
public void testGreen() throws IOException {
assertThat(colorize("hello world"),
is("<font color=\"green\">hello world</font>"));
}
@Test
public void testGreenOnWhite() throws IOException {
assertThat(
colorize("hello world"),
is("<span style=\"background-color: white\"><font color=\"green\">hello world</font></span>"));
}
private String colorize(String text) throws IOException {
StringOutputStream out = new StringOutputStream();
AnsiColorizer colorizer = new AnsiColorizer(out, Charset
.defaultCharset());
colorizer.eol(text.getBytes(), text.length());
return out.toString();
}
}

0 comments on commit 3963bff

Please sign in to comment.