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
- Host: GitHub
- URL: https://github.com/helpermethod/termplates
- Owner: helpermethod
- Created: 2018-12-12T17:20:52.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2021-11-30T11:40:49.000Z (about 4 years ago)
- Last Synced: 2025-01-29T02:59:43.154Z (11 months ago)
- Topics: cli, command-line, escape-codes, mustache
- Language: Java
- Homepage:
- Size: 75.2 KB
- Stars: 3
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# TERMplates
[](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` |