Skip to content

Instantly share code, notes, and snippets.

@j4rv
Last active February 9, 2022 00:08
Show Gist options
  • Save j4rv/c5c20c51107de5226e06ae670eb88fe9 to your computer and use it in GitHub Desktop.
Save j4rv/c5c20c51107de5226e06ae670eb88fe9 to your computer and use it in GitHub Desktop.
package com.jarv.colorprinter
/**
*
* Valid parameters:
*
* Colors:
* "BLACK", "RED", "GREEN", "YELLOW", "BLUE", "PURPLE", "CYAN", "WHITE"
* Only the last color parameter will be used
*
* Emphasis:
* "REGULAR", "BOLD", "ITALIC", "UNDERLINE"
* Warning: Italic is not widely supported
*
* Others:
* "BACKGROUND" -> The color will be applied as a background
* "BRIGHT" -> The color will be brighter *
*
* ColorPrinter has a PrintStream variable that you can set. Its default value is <code>System.out</code>.
*
* Current limitations:
* You cant choose a color for the font and another for the background.
*
* @author Jose.A Rojo Vidal
*
*/
class ColorPrinter {
static PrintStream printStream = System.out
private ColorPrinter() {}
static final String RESET_COLORS_CODE = "\033[0m"
// Valid parameters
private static final HashSet<String> VALID_COLOR_PARAMS =
["BLACK", "RED", "GREEN", "YELLOW", "BLUE", "PURPLE", "CYAN", "WHITE"]
private static final HashSet<String> VALID_EMPHASIS_PARAMS =
["REGULAR", "BOLD", "ITALIC", "UNDERLINE"]
private static final String BG_PARAM = "BACKGROUND"
private static final String BRIGHT_PARAM = "BRIGHT"
// Parameter to numeric ANSI code
private static final HashMap<String, Integer> COLORS_TO_CODE =
["BLACK" : 30,
"RED" : 31,
"GREEN" : 32,
"YELLOW" : 33,
"BLUE" : 34,
"PURPLE" : 35,
"CYAN" : 36,
"WHITE" : 37]
private static final HashMap<String, Integer> EMPHASIS_TO_CODE =
["REGULAR" : 0,
"BOLD" : 1,
"ITALIC" : 3, // Not widely supported. Sometimes treated as inverse.
"UNDERLINE" : 4]
// Public methods
static String getANSICode(final String... parameters){
String color = "WHITE"
String emphasis = "REGULAR"
boolean bright = false
boolean background = false
for (param in parameters) {
if (VALID_COLOR_PARAMS.contains(param)) {
color = param
continue
}
if (VALID_EMPHASIS_PARAMS.contains(param)) {
emphasis = param
continue
}
if (BRIGHT_PARAM.equals(param)) {
bright = true
continue
}
if (BG_PARAM.equals(param)) {
background = true
continue
}
print("[ColorPrinter warning] Unsupported parameter: $param", "RED", "BOLD", "BRIGHT")
}
final String emphasisCode = EMPHASIS_TO_CODE.get(emphasis)
final String colorCode = getColorCode(color, background, bright)
return "\033[${emphasisCode};${colorCode}m"
}
static void print(final Object obj, final String... parameters) {
printStream.print(colorize(obj, parameters))
}
static void println(final Object obj, final String... parameters) {
printStream.println(colorize(obj, parameters))
}
static void append(final Object obj, final String... parameters) {
printStream.append(colorize(obj, parameters))
}
static void flush(){
printStream.flush()
}
// Private methods
private static String getColorCode(final String color, final boolean background, final boolean bright) {
return COLORS_TO_CODE.get(color) + (background ? 10 : 0) + (bright ? 60 : 0)
}
private static String colorize(final Object obj, final String... parameters) {
if (parameters.length == 0){
return obj.toString()
}
String ansiStart = getANSICode(parameters)
return ansiStart + obj + RESET_COLORS_CODE
}
}
package com.jarv.colorprinter
ColorPrinter.println("Text without formatting")
ColorPrinter.println("Test bold red", "RED", "BOLD")
ColorPrinter.println("Test green bg", "GREEN", "BACKGROUND")
ColorPrinter.println(5L, "GREEN", "BACKGROUND")
ColorPrinter.println(2.65f, "GREEN", "BACKGROUND")
ColorPrinter.println("Test underlined yellow", "YELLOW", "UNDERLINE")
ColorPrinter.println("Test bright yellow", "BRIGHT", "YELLOW")
ColorPrinter.append("Append", "RED", "BRIGHT")
ColorPrinter.append("Test", "YELLOW", "BRIGHT")
ColorPrinter.append(6, "GREEN", "BRIGHT")
ColorPrinter.append(true, "CYAN", "BRIGHT")
ColorPrinter.append(null, "BLUE", "BRIGHT")
ColorPrinter.append("\n")
ColorPrinter.flush()
File f = new File("testfile.txt")
FileOutputStream fos = new FileOutputStream(f)
ColorPrinter.printStream = new PrintStream(fos)
ColorPrinter.println("Printing to a file", "RED")
ColorPrinter.printStream = System.out
ColorPrinter.println("Test green bg with typo", "GREEN", "BACKGORUND")
// Rpg character status print
ColorPrinter.print("[Maria the Plumber]", "CYAN", "BRIGHT")
ColorPrinter.print(" [26/50]hp ", "RED", "BOLD", "BRIGHT")
ColorPrinter.println("[38/60]mp ", "BLUE", "BOLD", "BRIGHT")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment