From 0bb44cdc9541beeb9c78927c663d16b6d737eb09 Mon Sep 17 00:00:00 2001 From: Guillaume Nodet Date: Wed, 2 Dec 2020 07:49:27 +0100 Subject: [PATCH] [MNG-6380] Use the new jansi AnsiMode to control the whether ansi sequences are stripped or let through --- .../shared/utils/logging/MessageUtils.java | 34 +++++++++++++------ 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/apache/maven/shared/utils/logging/MessageUtils.java b/src/main/java/org/apache/maven/shared/utils/logging/MessageUtils.java index 74b85a42..8b0d9cee 100644 --- a/src/main/java/org/apache/maven/shared/utils/logging/MessageUtils.java +++ b/src/main/java/org/apache/maven/shared/utils/logging/MessageUtils.java @@ -21,6 +21,7 @@ import org.fusesource.jansi.Ansi; import org.fusesource.jansi.AnsiConsole; +import org.fusesource.jansi.AnsiMode; /** * Colored message utils, to manage colors consistently across plugins (only if Maven version is at least 3.5.0). @@ -81,17 +82,13 @@ public static void systemUninstall() // hook can only set when JANSI is true if ( shutdownHook != null ) { - // if out and system_out are same instance again, ansi is assumed to be uninstalled - if ( AnsiConsole.out == AnsiConsole.system_out ) + try { - try - { - Runtime.getRuntime().removeShutdownHook( shutdownHook ); - } - catch ( IllegalStateException ex ) - { - // ignore - VM is already shutting down - } + Runtime.getRuntime().removeShutdownHook( shutdownHook ); + } + catch ( IllegalStateException ex ) + { + // ignore - VM is already shutting down } } } @@ -113,7 +110,19 @@ public static void setColorEnabled( boolean flag ) { if ( JANSI ) { + AnsiConsole.out().setMode( flag ? AnsiMode.Force : AnsiMode.Strip ); Ansi.setEnabled( flag ); + System.setProperty( AnsiConsole.JANSI_MODE, + flag ? AnsiConsole.JANSI_MODE_FORCE : AnsiConsole.JANSI_MODE_STRIP ); + boolean installed = AnsiConsole.isInstalled(); + while ( AnsiConsole.isInstalled() ) + { + AnsiConsole.systemUninstall(); + } + if ( installed ) + { + AnsiConsole.systemInstall(); + } } } @@ -194,7 +203,10 @@ public void run() { synchronized ( STARTUP_SHUTDOWN_MONITOR ) { - doSystemUninstall(); + while ( AnsiConsole.isInstalled() ) + { + doSystemUninstall(); + } } } };