From bbef072f8b21abfcbc751003b9ecbf74bb817e9e Mon Sep 17 00:00:00 2001 From: Jonathan Bernard Date: Wed, 10 Feb 2016 01:55:55 -0600 Subject: [PATCH] Added AnsiEscapeCodeSequence: better support for ANSI Escape codes. --- build.gradle | 2 +- .../util/AnsiEscapeCodeSequence.java | 149 ++++++++++++++++++ 2 files changed, 150 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/jdbernard/util/AnsiEscapeCodeSequence.java diff --git a/build.gradle b/build.gradle index c125a07..013d9cd 100644 --- a/build.gradle +++ b/build.gradle @@ -14,7 +14,7 @@ apply plugin: "ch.raffael.pegdown-doclet" apply plugin: "maven" group = "com.jdbernard" -version = "4.2" +version = "4.3" repositories { mavenLocal() diff --git a/src/main/java/com/jdbernard/util/AnsiEscapeCodeSequence.java b/src/main/java/com/jdbernard/util/AnsiEscapeCodeSequence.java new file mode 100644 index 0000000..d1dfa93 --- /dev/null +++ b/src/main/java/com/jdbernard/util/AnsiEscapeCodeSequence.java @@ -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; } + +}