Skip to content

Instantly share code, notes, and snippets.

@TopHatCroat
Created December 27, 2017 11:52
Show Gist options
  • Save TopHatCroat/fdb1808c6ea549a8da156770c47782bd to your computer and use it in GitHub Desktop.
Save TopHatCroat/fdb1808c6ea549a8da156770c47782bd to your computer and use it in GitHub Desktop.
public class TermHelper {
public static final String ESC = "\033[";
public static void clearScreen() {
System.out.print(ESC + "2J");
}
public static void moveTo(int x, int y) {
System.out.print(ESC + y + ";" + x + "H");
}
public static void print(String s) {
System.out.print(s);
}
public static void print(String s, int x, int y) {
moveTo(x, y);
System.out.print(s);
}
public static void clear(int x, int y) {
moveTo(x, y);
save();
setForegroundColor(Color.WHITE);
setBackgorundColor(Color.BLACK);
System.out.print(" ");
restore();
}
public static void save() {
System.out.print(ESC + "7");
}
public static void restore() {
System.out.print(ESC + "8");
}
public static void hideCursor() {
System.out.println(ESC + "?25h");
}
public static void showCursor() {
System.out.println(ESC + "?25l");
}
public static void setForegroundColor(Color color) {
switch (color) {
case BLACK:
System.out.print(ESC + "30" + "m");
break;
case WHITE:
System.out.print(ESC + "37" + "m");
break;
case RED:
System.out.print(ESC + "31" + "m");
break;
case GREEN:
System.out.print(ESC + "32" + "m");
break;
case YELLOW:
System.out.print(ESC + "33" + "m");
break;
case BLUE:
System.out.print(ESC + "34" + "m");
break;
case MAGENTA:
System.out.print(ESC + "35" + "m");
break;
case CYAN:
System.out.print(ESC + "36" + "m");
break;
}
}
public static void setBackgorundColor(Color color) {
switch (color) {
case BLACK:
System.out.print(ESC + "40" + "m");
break;
case WHITE:
System.out.print(ESC + "47" + "m");
break;
case RED:
System.out.print(ESC + "41" + "m");
break;
case GREEN:
System.out.print(ESC + "42" + "m");
break;
case YELLOW:
System.out.print(ESC + "43" + "m");
break;
case BLUE:
System.out.print(ESC + "44" + "m");
break;
case MAGENTA:
System.out.print(ESC + "45" + "m");
break;
case CYAN:
System.out.print(ESC + "46" + "m");
break;
}
}
public enum Color {
BLACK, WHITE, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment