An open API service indexing awesome lists of open source software.

https://github.com/helpermethod/termplates

Mustache Templates for the Commandline
https://github.com/helpermethod/termplates

cli command-line escape-codes mustache

Last synced: 9 months ago
JSON representation

Mustache Templates for the Commandline

Awesome Lists containing this project

README

          

# TERMplates

[![Download](https://api.bintray.com/packages/helpermethod/maven/termplates/images/download.svg)](https://bintray.com/helpermethod/maven/termplates/_latestVersion)

`TERMplates` is a small wrapper around [mustache.java](https://github.com/spullara/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

```xml


bintray
http://dl.bintray.com/helpermethod/maven

com.github.helpermethod
termplates
0.1.0

```

### Gradle

```groovy
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`.

```java
var model = new Map.of("name","TERMplates");

System.out.println(Termplates.renderInline("Hello {{name}}!", model));
```

Colors can be accessed by using the {{term}} namespace.

```java
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](#ansi-escape-codes).
).

```java
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`.

```hbs
{{#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.

```java
// 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` |