How to Style Java Console Output? ๐ŸŽจ

ยท

3 min read

How to Style Java Console Output? ๐ŸŽจ
  • Are you bored of looking at the same black-and-white console output in your terminal? Well, then let's start learning how to style our console output.

What do you need to start styling?

  • To style the output we don't need any external library or jar files. We will be using ANSI Escape Codes. But there are libraries out there to help you style your console output.

  • All we need to do is to add these Escape Codes in front of the text you wanted to be styled.

public class StylingConsoleOutput {
    public static void main(String[] args) {
        int table = 12;
        for (int number = 1; number < 10; number++) {
            System.out.print("\033[1;35m");
            System.out.println(table + " * " + number + " = " + table * number);
            System.out.print("\033[0m");//Used for resetting the changes.
        }
    }
}
  • The output of the above code looks something like this,

  • We can do multiple stylings on a single line of console output as well.

  • The below code-snippet contains the list of stylings available. Create an enum and copy & paste the below code to your enum and start using it in your project.

package com.ananth;

public enum Color {

    RESET("\033[0m"),

    // Regular Colors. Normal color, no bold, background color etc.
    BLACK("\033[0;30m"),    // BLACK
    RED("\033[0;31m"),      // RED
    GREEN("\033[0;32m"),    // GREEN
    YELLOW("\033[0;33m"),   // YELLOW
    BLUE("\033[0;34m"),     // BLUE
    MAGENTA("\033[0;35m"),  // MAGENTA
    CYAN("\033[0;36m"),     // CYAN
    WHITE("\033[0;37m"),    // WHITE

    // Bold
    BLACK_BOLD("\033[1;30m"),   // BLACK
    RED_BOLD("\033[1;31m"),     // RED
    GREEN_BOLD("\033[1;32m"),   // GREEN
    YELLOW_BOLD("\033[1;33m"),  // YELLOW
    BLUE_BOLD("\033[1;34m"),    // BLUE
    MAGENTA_BOLD("\033[1;35m"), // MAGENTA
    CYAN_BOLD("\033[1;36m"),    // CYAN
    WHITE_BOLD("\033[1;37m"),   // WHITE

    // Underline
    BLACK_UNDERLINED("\033[4;30m"),     // BLACK
    RED_UNDERLINED("\033[4;31m"),       // RED
    GREEN_UNDERLINED("\033[4;32m"),     // GREEN
    YELLOW_UNDERLINED("\033[4;33m"),    // YELLOW
    BLUE_UNDERLINED("\033[4;34m"),      // BLUE
    MAGENTA_UNDERLINED("\033[4;35m"),   // MAGENTA
    CYAN_UNDERLINED("\033[4;36m"),      // CYAN
    WHITE_UNDERLINED("\033[4;37m"),     // WHITE

    // Background
    BLACK_BACKGROUND("\033[40m"),   // BLACK
    RED_BACKGROUND("\033[41m"),     // RED
    GREEN_BACKGROUND("\033[42m"),   // GREEN
    YELLOW_BACKGROUND("\033[43m"),  // YELLOW
    BLUE_BACKGROUND("\033[44m"),    // BLUE
    MAGENTA_BACKGROUND("\033[45m"), // MAGENTA
    CYAN_BACKGROUND("\033[46m"),    // CYAN
    WHITE_BACKGROUND("\033[47m"),   // WHITE

    // High Intensity
    BLACK_BRIGHT("\033[0;90m"),     // BLACK
    RED_BRIGHT("\033[0;91m"),       // RED
    GREEN_BRIGHT("\033[0;92m"),     // GREEN
    YELLOW_BRIGHT("\033[0;93m"),    // YELLOW
    BLUE_BRIGHT("\033[0;94m"),      // BLUE
    MAGENTA_BRIGHT("\033[0;95m"),   // MAGENTA
    CYAN_BRIGHT("\033[0;96m"),      // CYAN
    WHITE_BRIGHT("\033[0;97m"),     // WHITE

    // Bold High Intensity
    BLACK_BOLD_BRIGHT("\033[1;90m"),    // BLACK
    RED_BOLD_BRIGHT("\033[1;91m"),      // RED
    GREEN_BOLD_BRIGHT("\033[1;92m"),    // GREEN
    YELLOW_BOLD_BRIGHT("\033[1;93m"),   // YELLOW
    BLUE_BOLD_BRIGHT("\033[1;94m"),     // BLUE
    MAGENTA_BOLD_BRIGHT("\033[1;95m"),  // MAGENTA
    CYAN_BOLD_BRIGHT("\033[1;96m"),     // CYAN
    WHITE_BOLD_BRIGHT("\033[1;97m"),    // WHITE

    // High Intensity backgrounds
    BLACK_BACKGROUND_BRIGHT("\033[0;100m"),     // BLACK
    RED_BACKGROUND_BRIGHT("\033[0;101m"),       // RED
    GREEN_BACKGROUND_BRIGHT("\033[0;102m"),     // GREEN
    YELLOW_BACKGROUND_BRIGHT("\033[0;103m"),    // YELLOW
    BLUE_BACKGROUND_BRIGHT("\033[0;104m"),      // BLUE
    MAGENTA_BACKGROUND_BRIGHT("\033[0;105m"),   // MAGENTA
    CYAN_BACKGROUND_BRIGHT("\033[0;106m"),      // CYAN
    WHITE_BACKGROUND_BRIGHT("\033[0;107m");     // WHITE

    private final String code;

    Color(String code) {
        this.code = code;
    }

    @Override
    public String toString() {
        return code;
    }
}
  • The below code was used to create the header image of this blog.

  • You must end the print statement with the RESET ANSII Code, or else it will be applied to the following console outputs.


public class StylingConsoleOutput {
    public static void main(String[] args) {
        System.out.println("Plain Simple Output ๐Ÿ˜•");
        System.out.println(Color.BLACK + "" + Color.YELLOW_BACKGROUND + "Hello, I am Bharath!" + Color.RESET);
        System.out.println(Color.CYAN_BOLD + "Styled Text!" + Color.RESET);
        System.out.println(Color.GREEN_UNDERLINED + "Underline Text!" + Color.RESET);
        System.out.println(Color.MAGENTA_BOLD + "" + Color.BLACK_BACKGROUND + "Bold Text! with Black Background!" + Color.RESET);
        System.out.println(Color.BLUE_BOLD_BRIGHT + "Blue Bold Bright Text!" + Color.RESET);
        System.out.println(Color.RED_BACKGROUND_BRIGHT + "Red Background Bright Text!" + Color.RESET);
    }
}

Credits

Hope you liked this blog.

Did you find this article valuable?

Support Bharath Kalyan's Blog by becoming a sponsor. Any amount is appreciated!

ย