Skip to content

helpermethod/termplates

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TERMplates

Download

TERMplates is a small wrapper around mustache.java which adds support for ANSI escape codes to render colors and text decorations.

Features

  • 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.

Installation

Maven

<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>

Gradle

repositories {
    maven {
        url  "https://dl.bintray.com/helpermethod/maven"
    }
}

compile 'com.github.helpermethod:termplates:0.1.0'

Usage

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.

{{#term.underline}}MOVIES{{/term.underline}}

{{! iterates over the `movies` array and renders each element on its own line }}
{{#movies}}
{{.}}
{{/movies}}

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"))));

ANSI Escape Codes

Foreground Colors

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

Text Decorations

Decoration Function
bold term.bold
underline term.underline
reverse term.reverse

About

Mustache Templates for the Commandline

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published