Permalink
Browse files

We can now strip ANSI text.

  • Loading branch information...
dblock committed Sep 2, 2011
1 parent 89fdd5a commit 6508e2d7f0f4f6c48578b9f24c07d66672a4bcac
@@ -25,7 +25,6 @@
import hudson.Extension;
import hudson.Launcher;
import hudson.console.LineTransformationOutputStream;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.BuildListener;
@@ -35,6 +34,7 @@
import java.io.IOException;
import java.io.OutputStream;
import org.fusesource.jansi.Ansi;
import org.kohsuke.stapler.DataBoundConstructor;
/**
@@ -71,48 +71,7 @@ public Environment setUp(AbstractBuild build, Launcher launcher,
@SuppressWarnings("unchecked")
@Override
public OutputStream decorateLogger(AbstractBuild build, OutputStream logger) {
return new AnsiColorOutputStream(logger);
}
/**
* Output stream that writes each line to the provided delegate output stream
* after inserting a {@link AnsiColorizer}.
*/
private static class AnsiColorOutputStream extends
LineTransformationOutputStream {
/**
* The delegate output stream.
*/
private final OutputStream delegate;
/**
* Create a new {@link AnsiColorOutputStream}.
*
* @param delegate
* the delegate output stream
*/
private AnsiColorOutputStream(OutputStream delegate) {
this.delegate = delegate;
}
/**
* {@inheritDoc}
*/
@Override
protected void eol(byte[] b, int len) throws IOException {
new AnsiColorizer().encodeTo(delegate);
delegate.write(b, 0, len);
}
/**
* {@inheritDoc}
*/
@Override
public void close() throws IOException {
super.close();
delegate.close();
}
return new AnsiColorizer(logger, build.getCharset());
}
/**
@@ -23,36 +23,44 @@
*/
package hudson.plugins.ansicolor;
import hudson.MarkupText;
import hudson.console.ConsoleAnnotator;
import hudson.console.ConsoleNote;
import hudson.console.LineTransformationOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset;
import org.fusesource.jansi.AnsiString;
/**
* Time-stamp note that is inserted into the console output.
*
* @author Daniel Doubrovkine
*/
public final class AnsiColorizer extends ConsoleNote<Object> {
/**
* Serialization UID.
*/
private static final long serialVersionUID = 1L;
/**
* Create a new {@link AnsiColorNote}.
*/
AnsiColorizer() {
}
/**
* {@inheritDoc}
*/
@Override
public ConsoleAnnotator<Object> annotate(Object context, MarkupText text,
int charPos) {
return null;
}
public final class AnsiColorizer extends LineTransformationOutputStream {
/**
* Serialization UID.
*/
private static final long serialVersionUID = 1L;
private final OutputStream out;
@SuppressWarnings("unused")
private final Charset charset;
public AnsiColorizer(OutputStream out, Charset charset) {
this.out = out;
this.charset = charset;
}
@Override
protected void eol(byte[] b, int len) throws IOException {
AnsiString ansiString = new AnsiString(new String(b));
out.write(ansiString.getPlain().toString().getBytes(), 0, ansiString.length());
}
@Override
public void close() throws IOException {
super.close();
out.close();
}
}
@@ -25,9 +25,11 @@
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import hudson.MarkupText;
import hudson.console.ConsoleNote;
import java.io.IOException;
import java.nio.charset.Charset;
import org.codehaus.plexus.util.StringOutputStream;
import org.junit.Test;
/**
@@ -36,20 +38,23 @@
public class AnsiColorizerTest {
/**
* @throws IOException
*/
@Test
public void AnsiColorizer() {
assertThat(annotate("line", new AnsiColorizer()),
is("line"));
public void testNoMarkup() throws IOException {
assertThat(colorize("line"), is("line"));
}
@Test
public void testClear() throws IOException {
assertThat(colorize("hello world"), is("hello world"));
}
@SuppressWarnings("unchecked")
private String annotate(String text, ConsoleNote... notes) {
Object context = new Object();
MarkupText markupText = new MarkupText(text);
for (ConsoleNote note : notes) {
note.annotate(context, markupText, 0);
}
return markupText.toString(true);
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 6508e2d

Please sign in to comment.