Skip to content

Commit

Permalink
Add a few methods to be able to render code names more easily, fixes #14
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Apr 14, 2017
1 parent 2616142 commit dbf2e8c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 14 deletions.
53 changes: 39 additions & 14 deletions jansi/src/main/java/org/fusesource/jansi/AnsiRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,22 +117,47 @@ public static Appendable render(final String input, Appendable target) throws IO
}

public static String render(final String text, final String... codes) {
Ansi ansi = Ansi.ansi();
for (String name : codes) {
Code code = Code.valueOf(name.toUpperCase(Locale.ENGLISH));

if (code.isColor()) {
if (code.isBackground()) {
ansi = ansi.bg(code.getColor());
} else {
ansi = ansi.fg(code.getColor());
}
} else if (code.isAttribute()) {
ansi = ansi.a(code.getAttribute());
}
return render(Ansi.ansi(), codes)
.a(text).reset().toString();
}

/**
* Renders {@link Code} names as an ANSI escape string.
* @param codes The code names to render
* @return an ANSI escape string.
*/
public static String renderCodes(final String... codes) {
return render(Ansi.ansi(), codes).toString();
}

/**
* Renders {@link Code} names as an ANSI escape string.
* @param codes A space separated list of code names to render
* @return an ANSI escape string.
*/
public static String renderCodes(final String codes) {
return renderCodes(codes.split("\\s"));
}

private static Ansi render(Ansi ansi, String... names) {
for (String name : names) {
render(ansi, name);
}
return ansi;
}

return ansi.a(text).reset().toString();
private static Ansi render(Ansi ansi, String name) {
Code code = Code.valueOf(name.toUpperCase(Locale.ENGLISH));
if (code.isColor()) {
if (code.isBackground()) {
ansi.bg(code.getColor());
} else {
ansi.fg(code.getColor());
}
} else if (code.isAttribute()) {
ansi.a(code.getAttribute());
}
return ansi;
}

public static boolean test(final String text) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ public void testRender() {
assertEquals(ansi().bold().a("foo").reset().toString(), str);
}

@Test
public void testRenderCodes() {
String str = renderCodes("bold red");
System.out.println(str);
assertEquals(ansi().bold().fg(Color.RED).toString(), str);
}

@Test
public void testRender2() {
String str = render("@|bold,red foo|@");
Expand Down

0 comments on commit dbf2e8c

Please sign in to comment.