Created
May 22, 2018 21:04
-
-
Save LIttleAncientForestKami/d04b97fcac4e81ca17106436d785ef94 to your computer and use it in GitHub Desktop.
ANSI colours in Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package pl.edu.lafk.colours; | |
enum ANSI { | |
RESET("\u001B[0m"), | |
BLACK("\u001B[30m"), | |
RED("\u001B[31m"), | |
GREEN("\u001B[32m"), | |
YELLOW("\u001B[33m"), | |
BLUE("\u001B[34m"), | |
PURPLE("\u001B[35m"), | |
CYAN("\u001B[36m"), | |
WHITE("\u001B[37m"); | |
private final String escapeCode; | |
ANSI(String ansiVal) { | |
this.escapeCode = ansiVal; | |
} | |
@Override | |
public String toString() { | |
return escapeCode; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package pl.edu.lafk.colours; | |
/** | |
* Paints String in a passed colour. | |
* TODO: Once CodingBat and the others switch to JDK9 - change this to an interface. | |
* TODO: think if I want more out of it? | |
* @see <a href="https://gist.github.com/dainkaplan/4651352" target="_top">Dain Kaplan's gist - nice idea to combine codes</a> | |
* @see <a href="https://stackoverflow.com/a/45444716/999165" target="_top">Full code list</a> | |
* @see <a href="https://fusesource.github.io/jansi/documentation/api/index.html" target="_top">JANSI library</a> | |
*/ | |
public class Painter { | |
private static String colourize(ANSI code, String s) { | |
return code + s + ANSI.RESET; | |
} | |
public static String red(String s) { | |
return colourize(ANSI.RED, s); | |
} | |
public static String yellow(String s) { | |
return colourize(ANSI.YELLOW, s); | |
} | |
public static String green(String s) { | |
return colourize(ANSI.GREEN, s); | |
} | |
public static void main(String[] args) { | |
System.out.println(red("error")); | |
System.out.println(yellow("warning")); | |
System.out.println(green("success")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment