Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 613
Alexey Kutuzov Avatar asked Feb 28 '13 11:02

Alexey Kutuzov


2 Answers

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);
like image 104
Vlad Avatar answered Oct 13 '22 11:10

Vlad


If you are based on Spring Boot using AnsiOutput with AnsiColor should work. Example:

@SpringBootApplication
public class Application {

  @EventListener
  public void onApplicationReady(ApplicationReadyEvent event) {
    System.out.println(AnsiOutput.toString(
        AnsiColor.GREEN, "Service started successfully", AnsiColor.DEFAULT));
  }

  @EventListener
  public void onApplicationFailed(ApplicationFailedEvent event) {
    System.err.println(AnsiOutput.toString(
        AnsiColor.RED, "Service startup failed", AnsiColor.DEFAULT));
  }

  public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
  }

}
like image 35
Attila T Avatar answered Oct 13 '22 11:10

Attila T