Search code examples
javaspring-shell

Spring shell colorized output


I want to colorize my console output in spring-shell. Something like:

System.out.println("\red; red text");

I know about colorized JUL logging output, but it's not enought.

How I can do that?


Solution

  • You can use Ansi values in your output.

    Here is my class with some color constants:

    public class AnsiConstants {
      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_GRAY = "\u001B[37m";
      public static final String ANSI_WHITE = "\u001B[37;1m"; 
    }
    

    Your example can be generated with:

    System.out.println(ANSI_RED + "red text" + ANSI_RESET);