TERMplates
is a small wrapper around mustache.java which adds support for
ANSI escape codes to render colors and text decorations.
- Colors Apply foreground and background colors.
- Text Decorations Underline and bolden text.
- TTY detection Ignore ANSI escape codes when not started from an interactive commandline.
<repositories>
<repository>
<id>bintray</id>
<url>http://dl.bintray.com/helpermethod/maven</url>
</repository>
</repositories>
<dependency>
<groupId>com.github.helpermethod</groupId>
<artifactId>termplates</artifactId>
<version>0.1.0</version>
</dependency>
repositories {
maven {
url "https://dl.bintray.com/helpermethod/maven"
}
}
compile 'com.github.helpermethod:termplates:0.1.0'
The static renderInline
method takes a Mustache template of type String
and a model of type Map
or Object
and returns the rendered template as a String
.
var model = new Map.of("name","TERMplates");
System.out.println(Termplates.renderInline("Hello {{name}}!", model));
Colors can be accessed by using the {{term}} namespace.
System.out.println(Termplates.renderInline("Hello {{#term.red}}{{name}}{{/term.red}}", Map.of("name", "TERMplates")));
Colors and text decorations can be combined by nesting them (for a full list of supported escape sequences, see ANSI Escape Codes. ).
System.out.println(Termplates.renderInline("Hello {{#term.bold}}{{#term.red}}{{name}}{{/term.red}}{{term.bold}}", Map.of("name", "TERMplates")));
For rendering more complex templates, create a file ending on .mustache
under src/main/resources/templates
, e.g.
movies.mustache
.
Use the static renderFile
method to render the file containing the method.
// note that you reference the file only by its prefix, i.e. "movies", not "movies.mustache"
System.out.println(renderFile("movies", Map.of("movies", List.of("Evil Dead", "Evil Dead 2", "Army Of Darkness"))));
Color | Function |
---|---|
black | term.black |
red | term.red |
green | term.green |
yellow | term.yellow |
blue | term.magenta |
magenta | term.magenta |
cyan | term.cyan |
white | term.white |
Decoration | Function |
---|---|
bold | term.bold |
underline | term.underline |
reverse | term.reverse |