Added AnsiEscapeCodeSequence: better support for ANSI Escape codes.

This commit is contained in:
Jonathan Bernard 2016-02-10 01:55:55 -06:00
parent dcef7512ce
commit bbef072f8b
2 changed files with 150 additions and 1 deletions

View File

@ -14,7 +14,7 @@ apply plugin: "ch.raffael.pegdown-doclet"
apply plugin: "maven"
group = "com.jdbernard"
version = "4.2"
version = "4.3"
repositories {
mavenLocal()

View File

@ -0,0 +1,149 @@
/**
* # AnsiEscapeCodeSequence
* @author Jonathan Bernard (jdbernard@gmail.com)
* @org jdbernard.com/util/AnsiEscapeCodeSequence
* @copyright 2016 Jonathan Bernard
*/
package com.jdbernard.util;
/**
* The AnsiEscapeCodeSequence class is an extended wrapper around
* [ANSI escape codes].
*
* [ANSI escape codes]: http://en.wikipedia.org/wiki/ANSI_escape_code
*/
public class AnsiEscapeCodeSequence {
public StringBuilder value = new StringBuilder();
public static final String CSI = "\u001b[";
public static enum Colors {
BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE, EXT, DEFAULT }
public static enum Erase { ToEnd, ToBeginning, All }
public String toString() { return value.toString(); }
// Multiple methods for various forms of SGR
public AnsiEscapeCodeSequence color(Colors fg, Colors bg, boolean bright) {
value.append(CSI);
value.append(bright ? "1" : "22");
if (fg != null) {
value.append(";3");
value.append(Integer.toString(fg.ordinal())); }
if (bg != null) {
value.append(";4");
value.append(Integer.toString(bg.ordinal())); }
value.append("m");
return this; }
public AnsiEscapeCodeSequence resetText() {
value.append(CSI).append("0m");
return this; }
public AnsiEscapeCodeSequence blinkSlow() {
value.append(CSI).append("5m");
return this; }
public AnsiEscapeCodeSequence blinkFast() {
value.append(CSI).append("6m");
return this; }
public AnsiEscapeCodeSequence invertColors() {
value.append(CSI).append("7m");
return this; }
public AnsiEscapeCodeSequence strikethrough() {
value.append(CSI).append("9m");
return this; }
// Other ANSI escape codes
public AnsiEscapeCodeSequence cursorUp() { return cursorUp(1); }
public AnsiEscapeCodeSequence cursorUp(int cells) {
value.append(CSI).append(Integer.toString(cells)).append("A");
return this; }
public AnsiEscapeCodeSequence cursorDown() { return cursorDown(1); }
public AnsiEscapeCodeSequence cursorDown(int cells) {
value.append(CSI).append(Integer.toString(cells)).append("B");
return this; }
public AnsiEscapeCodeSequence cursorForward() { return cursorForward(1); }
public AnsiEscapeCodeSequence cursorForward(int cells) {
value.append(CSI).append(Integer.toString(cells)).append("C");
return this; }
public AnsiEscapeCodeSequence cursorBackward() { return cursorBackward(1); }
public AnsiEscapeCodeSequence cursorBackward(int cells) {
value.append(CSI).append(Integer.toString(cells)).append("D");
return this; }
public AnsiEscapeCodeSequence cursorNextLine() { return cursorNextLine(1); }
public AnsiEscapeCodeSequence cursorNextLine(int lines) {
value.append(CSI).append(Integer.toString(lines)).append("E");
return this; }
public AnsiEscapeCodeSequence cursorPrevLine() { return cursorPrevLine(1); }
public AnsiEscapeCodeSequence cursorPrevLine(int lines) {
value.append(CSI).append(Integer.toString(lines)).append("F");
return this; }
public AnsiEscapeCodeSequence cursorHorizontalAbsolute(int column) {
value.append(CSI).append(Integer.toString(column)).append("G");
return this; }
public AnsiEscapeCodeSequence cursorPosition(int row, int column) {
value.append(CSI)
.append(Integer.toString(row))
.append(";")
.append(Integer.toString(column))
.append("H");
return this; }
public AnsiEscapeCodeSequence eraseDisplay() {
return eraseDisplay(Erase.ToEnd); }
public AnsiEscapeCodeSequence eraseDisplay(Erase how) {
value.append(CSI)
.append(Integer.toString(how.ordinal()))
.append("J");
return this; }
public AnsiEscapeCodeSequence eraseLine() { return eraseLine(Erase.ToEnd); }
public AnsiEscapeCodeSequence eraseLine(Erase how) {
value.append(CSI)
.append(Integer.toString(how.ordinal()))
.append("K");
return this; }
public AnsiEscapeCodeSequence scrollUp() { return scrollUp(1); }
public AnsiEscapeCodeSequence scrollUp(int lines) {
value.append(CSI).append(Integer.toString(lines)).append("S");
return this; }
public AnsiEscapeCodeSequence scrollDown() { return scrollDown(1); }
public AnsiEscapeCodeSequence scrollDown(int lines) {
value.append(CSI).append(Integer.toString(lines)).append("T");
return this; }
public AnsiEscapeCodeSequence cursorHVPosition(int row, int column) {
value.append(CSI)
.append(Integer.toString(row))
.append(";")
.append(Integer.toString(column))
.append("f");
return this; }
public AnsiEscapeCodeSequence saveCursor() {
value.append(CSI).append("s");
return this; }
public AnsiEscapeCodeSequence restoreCursor() {
value.append(CSI).append("u");
return this; }
}