Skip to content

Commit

Permalink
Refactor org.fusesource.jansi.AnsiRenderer.render(String) into a new
Browse files Browse the repository at this point in the history
method org.fusesource.jansi.AnsiRenderer.render(String, Appendable). I
want to use this from Log4j.
  • Loading branch information
garydgregory authored and gnodet committed Mar 15, 2017
1 parent fd83740 commit fd67379
Showing 1 changed file with 42 additions and 22 deletions.
64 changes: 42 additions & 22 deletions jansi/src/main/java/org/fusesource/jansi/AnsiRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.fusesource.jansi;

import java.io.IOException;
import java.util.Locale;

import org.fusesource.jansi.Ansi.Attribute;
Expand Down Expand Up @@ -61,7 +62,26 @@ private AnsiRenderer() {
}

static public String render(final String input) throws IllegalArgumentException {
StringBuffer buff = new StringBuffer();
try {
return render(input, new StringBuilder()).toString();
} catch (IOException e) {
// Cannot happen because StringBuilder does not throw IOException
throw new IllegalArgumentException(e);
}
}

/**
* Renders the given input to the target Appendable.
*
* @param input
* source to render
* @param target
* render onto this target Appendable.
* @return the given Appendable
* @throws IOException
* If an I/O error occurs
*/
static public Appendable render(final String input, Appendable target) throws IOException {

int i = 0;
int j, k;
Expand All @@ -70,32 +90,32 @@ static public String render(final String input) throws IllegalArgumentException
j = input.indexOf(BEGIN_TOKEN, i);
if (j == -1) {
if (i == 0) {
return input;
} else {
buff.append(input.substring(i, input.length()));
return buff.toString();
target.append(input);
return target;
}
} else {
buff.append(input.substring(i, j));
k = input.indexOf(END_TOKEN, j);
target.append(input.substring(i, input.length()));
return target;
}
target.append(input.substring(i, j));
k = input.indexOf(END_TOKEN, j);

if (k == -1) {
return input;
} else {
j += BEGIN_TOKEN_LEN;
String spec = input.substring(j, k);
if (k == -1) {
target.append(input);
return target;
}
j += BEGIN_TOKEN_LEN;
String spec = input.substring(j, k);

String[] items = spec.split(CODE_TEXT_SEPARATOR, 2);
if (items.length == 1) {
return input;
}
String replacement = render(items[1], items[0].split(CODE_LIST_SEPARATOR));
String[] items = spec.split(CODE_TEXT_SEPARATOR, 2);
if (items.length == 1) {
target.append(input);
return target;
}
String replacement = render(items[1], items[0].split(CODE_LIST_SEPARATOR));

buff.append(replacement);
target.append(replacement);

i = k + END_TOKEN_LEN;
}
}
i = k + END_TOKEN_LEN;
}
}

Expand Down

0 comments on commit fd67379

Please sign in to comment.