DEV Community

Andrew (he/him)
Andrew (he/him)

Posted on

13

Coloured Terminal Output with Java

I was looking for a way to add a bit of flair to some terminal output today and found this StackOverflow post explaining how to add colour to terminal output using ANSI colour codes. Here's what the output from the below Java code looks like in my (MobaXterm / Ubuntu) shell:

Note that I didn't include underlined or bold text, both of which are also available. There is also a library by Diogo Nunes called the Java Colored Debug Printer (JCDP) which translates the ANSI codes into something that Windows command prompt can handle, making the solution cross-platform. Here's the code:

public class ColouredSystemOutPrintln {

  public static final String ANSI_RESET  = "\u001B[0m";

  public static final String ANSI_BLACK  = "\u001B[30m";
  public static final String ANSI_RED    = "\u001B[31m";
  public static final String ANSI_GREEN  = "\u001B[32m";
  public static final String ANSI_YELLOW = "\u001B[33m";
  public static final String ANSI_BLUE   = "\u001B[34m";
  public static final String ANSI_PURPLE = "\u001B[35m";
  public static final String ANSI_CYAN   = "\u001B[36m";
  public static final String ANSI_WHITE  = "\u001B[37m";

  public static final String ANSI_BRIGHT_BLACK  = "\u001B[90m";
  public static final String ANSI_BRIGHT_RED    = "\u001B[91m";
  public static final String ANSI_BRIGHT_GREEN  = "\u001B[92m";
  public static final String ANSI_BRIGHT_YELLOW = "\u001B[93m";
  public static final String ANSI_BRIGHT_BLUE   = "\u001B[94m";
  public static final String ANSI_BRIGHT_PURPLE = "\u001B[95m";
  public static final String ANSI_BRIGHT_CYAN   = "\u001B[96m";
  public static final String ANSI_BRIGHT_WHITE  = "\u001B[97m";

  public static final String[] FOREGROUNDS = {
    ANSI_BLACK, ANSI_RED, ANSI_GREEN, ANSI_YELLOW,
    ANSI_BLUE, ANSI_PURPLE, ANSI_CYAN, ANSI_WHITE,
    ANSI_BRIGHT_BLACK, ANSI_BRIGHT_RED, ANSI_BRIGHT_GREEN, ANSI_BRIGHT_YELLOW,
    ANSI_BRIGHT_BLUE, ANSI_BRIGHT_PURPLE, ANSI_BRIGHT_CYAN, ANSI_BRIGHT_WHITE 
  };

  public static final String ANSI_BG_BLACK  = "\u001B[40m";
  public static final String ANSI_BG_RED    = "\u001B[41m";
  public static final String ANSI_BG_GREEN  = "\u001B[42m";
  public static final String ANSI_BG_YELLOW = "\u001B[43m";
  public static final String ANSI_BG_BLUE   = "\u001B[44m";
  public static final String ANSI_BG_PURPLE = "\u001B[45m";
  public static final String ANSI_BG_CYAN   = "\u001B[46m";
  public static final String ANSI_BG_WHITE  = "\u001B[47m";

  public static final String ANSI_BRIGHT_BG_BLACK  = "\u001B[100m";
  public static final String ANSI_BRIGHT_BG_RED    = "\u001B[101m";
  public static final String ANSI_BRIGHT_BG_GREEN  = "\u001B[102m";
  public static final String ANSI_BRIGHT_BG_YELLOW = "\u001B[103m";
  public static final String ANSI_BRIGHT_BG_BLUE   = "\u001B[104m";
  public static final String ANSI_BRIGHT_BG_PURPLE = "\u001B[105m";
  public static final String ANSI_BRIGHT_BG_CYAN   = "\u001B[106m";
  public static final String ANSI_BRIGHT_BG_WHITE  = "\u001B[107m";

  public static final String[] BACKGROUNDS = {
    ANSI_BG_BLACK, ANSI_BG_RED, ANSI_BG_GREEN, ANSI_BG_YELLOW,
    ANSI_BG_BLUE, ANSI_BG_PURPLE, ANSI_BG_CYAN, ANSI_BG_WHITE,
    ANSI_BRIGHT_BG_BLACK, ANSI_BRIGHT_BG_RED, ANSI_BRIGHT_BG_GREEN, ANSI_BRIGHT_BG_YELLOW,
    ANSI_BRIGHT_BG_BLUE, ANSI_BRIGHT_BG_PURPLE, ANSI_BRIGHT_BG_CYAN, ANSI_BRIGHT_BG_WHITE };

  public static void main(String[] args) {

    System.out.println("\n  Default text\n");

    for (String fg : FOREGROUNDS) {
      for (String bg : BACKGROUNDS)
        System.out.print(fg + bg + "  TEST  ");
      System.out.println(ANSI_RESET);
    }

    System.out.println(ANSI_RESET + "\n  Back to default.\n");

  }

}
Enter fullscreen mode Exit fullscreen mode

This post originally appeared in a slightly different form on my (now defunct) Wordpress blog.

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (1)

Collapse
 
dialex profile image
Diogo Nunes

Thanks for mentioning my JCDP library ;)
Have you used it? If so, is there anything you would like to see improved?

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay