Sat. Apr 27th, 2024

When it comes to enhancing the visual appeal of console applications, one powerful and straightforward technique is to use ANSI color codes. These codes allow you to apply various colors to your text and backgrounds, making your output more vibrant and readable. In Java, you can achieve this by utilizing the ANSI escape sequences along with a set of predefined color constants.

Please see my original post on ANSIColors. You can find the ANSIColors class there. Now to make things simpler, I am introducing the AnsiOutput class, used to wrap text quickly and neater than implied with ANSIColors. Below you can find the sample code, it is simply status methods that wrap the input string with the color and the reset.

The specific relevant content for this request, if necessary, delimited with characters: Making use of ANSI Colors in text output is a great way to be able to call attention to specific blocks of text. Use red to indicate something important.

public class AnsiOutput {

    public static final String RESET = ANSIColors.RESET;

    // Regular text colors
    public static String black(String input) {
        return ANSIColors.BLACK + input + RESET;
    }

    public static String red(String input) {
        return ANSIColors.RED + input + RESET;
    }

    public static String green(String input) {
        return ANSIColors.GREEN + input + RESET;
    }

    public static String yellow(String input) {
        return ANSIColors.YELLOW + input + RESET;
    }

    public static String blue(String input) {
        return ANSIColors.BLUE + input + RESET;
    }

    public static String magenta(String input) {
        return ANSIColors.MAGENTA + input + RESET;
    }

    public static String cyan(String input) {
        return ANSIColors.CYAN + input + RESET;
    }

    public static String white(String input) {
        return ANSIColors.WHITE + input + RESET;
    }

    // Bright text colors
    public static String brightBlack(String input) {
        return ANSIColors.BRIGHT_BLACK + input + RESET;
    }

    public static String brightRed(String input) {
        return ANSIColors.BRIGHT_RED + input + RESET;
    }

    public static String brightGreen(String input) {
        return ANSIColors.BRIGHT_GREEN + input + RESET;
    }

    public static String brightYellow(String input) {
        return ANSIColors.BRIGHT_YELLOW + input + RESET;
    }

    public static String brightBlue(String input) {
        return ANSIColors.BRIGHT_BLUE + input + RESET;
    }

    public static String brightMagenta(String input) {
        return ANSIColors.BRIGHT_MAGENTA + input + RESET;
    }

    public static String brightCyan(String input) {
        return ANSIColors.BRIGHT_CYAN + input + RESET;
    }

    public static String brightWhite(String input) {
        return ANSIColors.BRIGHT_WHITE + input + RESET;
    }

    // Background colors
    public static String backgroundBlack(String input) {
        return ANSIColors.BACKGROUND_BLACK + input + RESET;
    }

    public static String backgroundRed(String input) {
        return ANSIColors.BACKGROUND_RED + input + RESET;
    }

    public static String backgroundGreen(String input) {
        return ANSIColors.BACKGROUND_GREEN + input + RESET;
    }

    public static String backgroundYellow(String input) {
        return ANSIColors.BACKGROUND_YELLOW + input + RESET;
    }

    public static String backgroundBlue(String input) {
        return ANSIColors.BACKGROUND_BLUE + input + RESET;
    }

    public static String backgroundMagenta(String input) {
        return ANSIColors.BACKGROUND_MAGENTA + input + RESET;
    }

    public static String backgroundCyan(String input) {
        return ANSIColors.BACKGROUND_CYAN + input + RESET;
    }

    public static String backgroundWhite(String input) {
        return ANSIColors.BACKGROUND_WHITE + input + RESET;
    }

    // Bright background colors
    public static String brightBackgroundBlack(String input) {
        return ANSIColors.BRIGHT_BACKGROUND_BLACK + input + RESET;
    }

    public static String brightBackgroundRed(String input) {
        return ANSIColors.BRIGHT_BACKGROUND_RED + input + RESET;
    }

    public static String brightBackgroundGreen(String input) {
        return ANSIColors.BRIGHT_BACKGROUND_GREEN + input + RESET;
    }

    public static String brightBackgroundYellow(String input) {
        return ANSIColors.BRIGHT_BACKGROUND_YELLOW + input + RESET;
    }

    public static String brightBackgroundBlue(String input) {
        return ANSIColors.BRIGHT_BACKGROUND_BLUE + input + RESET;
    }

    public static String brightBackgroundMagenta(String input) {
        return ANSIColors.BRIGHT_BACKGROUND_MAGENTA + input + RESET;
    }

    public static String brightBackgroundCyan(String input) {
        return ANSIColors.BRIGHT_BACKGROUND_CYAN + input + RESET;
    }

    public static String brightBackgroundWhite(String input) {
        return ANSIColors.BRIGHT_BACKGROUND_WHITE + input + RESET;
    }
}

By Jeffery Miller

I am known for being able to quickly decipher difficult problems to assist development teams in producing a solution. I have been called upon to be the Team Lead for multiple large-scale projects. I have a keen interest in learning new technologies, always ready for a new challenge.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.