https://github.com/nomemory/ansiscape
Color your output using Ansi Escape codes
https://github.com/nomemory/ansiscape
Last synced: over 1 year ago
JSON representation
Color your output using Ansi Escape codes
- Host: GitHub
- URL: https://github.com/nomemory/ansiscape
- Owner: nomemory
- Created: 2017-11-07T09:35:51.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-11-15T09:27:20.000Z (over 8 years ago)
- Last Synced: 2025-03-22T06:41:38.677Z (over 1 year ago)
- Language: Java
- Homepage:
- Size: 62.5 KB
- Stars: 17
- Watchers: 1
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# AnsiScape
**AnsiScape** is a simple Java library that allows the user to format the output of the applications using [ANSI Escape Codes](https://en.wikipedia.org/wiki/ANSI_escape_code).
Library is available in [jcenter()](https://bintray.com/nomemory/maven/ansiscape).
**gradle:**
```groovy
repositories {
jcenter()
}
dependencies {
compile 'net.andreinc.ansiscape:ansiscape:0.0.2'
}
```
**maven:**
```xml
jcenter
https://jcenter.bintray.com/
net.andreinc.ansiscape
ansiscape
0.0.2
```
### Example (1)
```java
AnsiScape asciiScape = new AnsiScape();
String formatted = asciiScape.format("{b Some Bold Text {u Also Underlined}}");
String formatted2 = asciiScape.format("Romanian Flag {redBg }{yellowBg }{blueBg }");
System.out.println(formatted);
System.out.println(formatted2);
```
The output:

### Example (2)
In this example we are defining our own escape codes classes (in a way similar to CSS classes):
```java
AnsiScapeContext context = new AnsiScapeContext();
// Create new escape classes that can be used as tags inside the text
AnsiClass title = AnsiClass.withName("title").add(AnsiSequence.BOLD);
AnsiClass url = AnsiClass.withName("url").add(AnsiSequence.UNDERLINE, AnsiSequence.BLUE);
AnsiClass text = AnsiClass.withName("text").add(AnsiSequence.RED);
context.add(title).add(url).add(text);
AnsiScape ansiScape = new AnsiScape(context);
String format = ansiScape.format("{title Bold title}\n" +
"-{text Some url: {url www.google.com}};\n" +
"-{text Some other url: {url {redBg www.redbackground.com}}}");
System.out.println(format);
```
The output:

### Supported classes
By default the following escape classes can be used:
| Escape Class | Description |
| ------------- | -------------|
| `{b }` | Bold text |
| `{bold }` | Bold text |
| `{dim }` | Dim text |
| `{u }` | Underlined text |
| `{underline }` | Underlined text |
| `{blink }` | Blink text |
| `{reverse }` | Reverse text |
| `{blank }` | Blank |
| `{overstrike }` | Overstrike text |
| `{reset }` | Resets everything inside tag |
| `{black }` | Black foreground |
| `{blackBg }` | Black background |
| `{red }` | Red foreground |
| `{redBg }` | Red foreground |
| `{green }` | Green foreground |
| `{greenBg }` | Green background |
| `{yellow }` | Yellow foreground |
| `{yellowBg }` | Yellow background |
| `{blue }` | Blue foreground |
| `{blueBg }` | Blue background |
| `{magenta }` | Magenta foreground |
| `{magentaBg }` | Magenta background |
| `{cyan }` | Cyan foreground |
| `{cyanBg }` | Cyan background |
| `{white }` | White foreground |
| `{whiteBg }` | White background |
### Notes
- Library woks well with Linux and Mac terminals. It wasn't tested on Windows, but as far as I know it won't work on Windows XP or older `cmd.exe` terminals without additional hacks ;
- In this stage the library is **experimental** - there are no unit tests for the moment;
- Some terminals (like IntelliJ Output Console) do not implement all the ascii codes.