Skip to content

Instantly share code, notes, and snippets.

@dainkaplan
Last active September 16, 2024 12:30
Show Gist options
  • Save dainkaplan/4651352 to your computer and use it in GitHub Desktop.
Save dainkaplan/4651352 to your computer and use it in GitHub Desktop.
Simple ANSI colors class for terminal code for JVM written in Scala and Java (two implementations)
package org.tempura.console.util;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* Usage:
* <li>String msg = Ansi.Red.and(Ansi.BgYellow).format("Hello %s", name)</li>
* <li>String msg = Ansi.Blink.colorize("BOOM!")</li>
*
* Or, if you are adverse to that, you can use the constants directly:
* <li>String msg = new Ansi(Ansi.ITALIC, Ansi.GREEN).format("Green money")</li>
* Or, even:
* <li>String msg = Ansi.BLUE + "scientific"</li>
*
* NOTE: Nothing stops you from combining multiple FG colors or BG colors,
* but only the last one will display.
*
* @author dain
*
*/
public final class Ansi {
// Color code strings from:
// http://www.topmudsites.com/forums/mud-coding/413-java-ansi.html
public static final String SANE = "\u001B[0m";
public static final String HIGH_INTENSITY = "\u001B[1m";
public static final String LOW_INTENSITY = "\u001B[2m";
public static final String ITALIC = "\u001B[3m";
public static final String UNDERLINE = "\u001B[4m";
public static final String BLINK = "\u001B[5m";
public static final String RAPID_BLINK = "\u001B[6m";
public static final String REVERSE_VIDEO = "\u001B[7m";
public static final String INVISIBLE_TEXT = "\u001B[8m";
public static final String BLACK = "\u001B[30m";
public static final String RED = "\u001B[31m";
public static final String GREEN = "\u001B[32m";
public static final String YELLOW = "\u001B[33m";
public static final String BLUE = "\u001B[34m";
public static final String MAGENTA = "\u001B[35m";
public static final String CYAN = "\u001B[36m";
public static final String WHITE = "\u001B[37m";
public static final String BACKGROUND_BLACK = "\u001B[40m";
public static final String BACKGROUND_RED = "\u001B[41m";
public static final String BACKGROUND_GREEN = "\u001B[42m";
public static final String BACKGROUND_YELLOW = "\u001B[43m";
public static final String BACKGROUND_BLUE = "\u001B[44m";
public static final String BACKGROUND_MAGENTA = "\u001B[45m";
public static final String BACKGROUND_CYAN = "\u001B[46m";
public static final String BACKGROUND_WHITE = "\u001B[47m";
public static final Ansi HighIntensity = new Ansi(HIGH_INTENSITY);
public static final Ansi Bold = HighIntensity;
public static final Ansi LowIntensity = new Ansi(LOW_INTENSITY);
public static final Ansi Normal = LowIntensity;
public static final Ansi Italic = new Ansi(ITALIC);
public static final Ansi Underline = new Ansi(UNDERLINE);
public static final Ansi Blink = new Ansi(BLINK);
public static final Ansi RapidBlink = new Ansi(RAPID_BLINK);
public static final Ansi Black = new Ansi(BLACK);
public static final Ansi Red = new Ansi(RED);
public static final Ansi Green = new Ansi(GREEN);
public static final Ansi Yellow = new Ansi(YELLOW);
public static final Ansi Blue = new Ansi(BLUE);
public static final Ansi Magenta = new Ansi(MAGENTA);
public static final Ansi Cyan = new Ansi(CYAN);
public static final Ansi White = new Ansi(WHITE);
public static final Ansi BgBlack = new Ansi(BACKGROUND_BLACK);
public static final Ansi BgRed = new Ansi(BACKGROUND_RED);
public static final Ansi BgGreen = new Ansi(BACKGROUND_GREEN);
public static final Ansi BgYellow = new Ansi(BACKGROUND_YELLOW);
public static final Ansi BgBlue = new Ansi(BACKGROUND_BLUE);
public static final Ansi BgMagenta = new Ansi(BACKGROUND_MAGENTA);
public static final Ansi BgCyan = new Ansi(BACKGROUND_CYAN);
public static final Ansi BgWhite = new Ansi(BACKGROUND_WHITE);
final private String[] codes;
final private String codes_str;
public Ansi(String... codes) {
this.codes = codes;
String _codes_str = "";
for (String code : codes) {
_codes_str += code;
}
codes_str = _codes_str;
}
public Ansi and(Ansi other) {
List<String> both = new ArrayList<String>();
Collections.addAll(both, codes);
Collections.addAll(both, other.codes);
return new Ansi(both.toArray(new String[] {}));
}
public String colorize(String original) {
return codes_str + original + SANE;
}
public String format(String template, Object... args) {
return colorize(String.format(template, args));
}
}
package org.tempura.console.utils
/**
* Usage:
*
* import Ansi._
*
* val msg0 = (Red and BgYellow) format ("Hello %s", name)
* val msg1 = (Red + BgYellow)("Hello")
*
* Or also:
*
* val msg2 = Ansi.Red.and(Ansi.BgYellow).format("Hello %s", name)
* val msg3 = Ansi.Blink("BOOM!")
*
* Or, if you are adverse to that, you can use the constants directly:
*
* val msg4 = new Ansi(Ansi.ITALIC, Ansi.GREEN).format("Green money")
*
* Or, even:
*
* val msg = Ansi.BLUE + "scientific"
*
* NOTE: Nothing stops you from combining multiple FG colors or BG colors,
* but only the last one will display.
*
*/
object Ansi {
// Color code strings from:
// http://www.topmudsites.com/forums/mud-coding/413-java-ansi.html
val SANE = "\u001B[0m"
val HIGH_INTENSITY = "\u001B[1m"
val LOW_INTENSITY = "\u001B[2m"
val ITALIC = "\u001B[3m"
val UNDERLINE = "\u001B[4m"
val BLINK = "\u001B[5m"
val RAPID_BLINK = "\u001B[6m"
val REVERSE_VIDEO = "\u001B[7m"
val INVISIBLE_TEXT = "\u001B[8m"
val BLACK = "\u001B[30m"
val RED = "\u001B[31m"
val GREEN = "\u001B[32m"
val YELLOW = "\u001B[33m"
val BLUE = "\u001B[34m"
val MAGENTA = "\u001B[35m"
val CYAN = "\u001B[36m"
val WHITE = "\u001B[37m"
val BACKGROUND_BLACK = "\u001B[40m"
val BACKGROUND_RED = "\u001B[41m"
val BACKGROUND_GREEN = "\u001B[42m"
val BACKGROUND_YELLOW = "\u001B[43m"
val BACKGROUND_BLUE = "\u001B[44m"
val BACKGROUND_MAGENTA = "\u001B[45m"
val BACKGROUND_CYAN = "\u001B[46m"
val BACKGROUND_WHITE = "\u001B[47m"
val HighIntensity = new Ansi(HIGH_INTENSITY)
val LowIntensity = new Ansi(LOW_INTENSITY)
val Italic = new Ansi(ITALIC)
val Underline = new Ansi(UNDERLINE)
val Blink = new Ansi(BLINK)
val RapidBlink = new Ansi(RAPID_BLINK)
val Black = new Ansi(BLACK)
val Red = new Ansi(RED)
val Green = new Ansi(GREEN)
val Yellow = new Ansi(YELLOW)
val Blue = new Ansi(BLUE)
val Magenta = new Ansi(MAGENTA)
val Cyan = new Ansi(CYAN)
val White = new Ansi(WHITE)
val BgBlack = new Ansi(BACKGROUND_BLACK)
val BgRed = new Ansi(BACKGROUND_RED)
val BgGreen = new Ansi(BACKGROUND_GREEN)
val BgYellow = new Ansi(BACKGROUND_YELLOW)
val BgBlue = new Ansi(BACKGROUND_BLUE)
val BgMagenta = new Ansi(BACKGROUND_MAGENTA)
val BgCyan = new Ansi(BACKGROUND_CYAN)
val BgWhite = new Ansi(BACKGROUND_WHITE)
}
class Ansi(val codes: String*) {
private val codes_str = codes.mkString
def and(other: Ansi) = {
new Ansi(codes ++ other.codes: _*)
}
def + = and _ // Alias
def apply(original: String) = {
codes_str + original + Ansi.SANE
}
def colorize = apply _ // Alias
def format(template: String, args: Any*) = {
apply(template.format(args: _*))
}
def % = format _ // Alias
}
@GlulkAlex
Copy link

GlulkAlex commented Jun 21, 2017

Don't you think that Scala snippet is a little overkill ?
Considering standard scala.io.AnsiColor
and ANSI Style and Control Codes in scala.Console .
Fun fact
once I borrowed the same codes
for more colorful logging|debugging output in Python
and for the custom simple test suits in Node.js .

class ansi_Codes( Enum ):
    # also Using automatic (& custom) values is possible
    ReSet = 0
    Bold = 1
    Reversed = 7
    Red = 31
    Red_B = 41
    Green = 32
    Green_B = 42
    # with format() auto convert|cast to str()
    def __str__( self ):
      #
      return '\u001B[{0}m'.format( self.value )

By the way
nicely done .
I like the function composition .

@bogaktik
Copy link

Amazing code, it helps me a lot and save me bunch of time digging google.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment