[ 
https://issues.apache.org/jira/browse/GROOVY-12784?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18147077#comment-18147077
 ] 

ASF GitHub Bot commented on GROOVY-12784:
-----------------------------------------

codeconsole commented on code in PR #3309:
URL: https://github.com/apache/groovy/pull/3309#discussion_r4624501824


##########
src/main/java/org/codehaus/groovy/tools/shell/IO.java:
##########
@@ -109,19 +106,53 @@ public IO(final InputStream inputStream, final 
OutputStream outputStream, final
     /**
      * Attempts to create an ANSI-aware writer for the supplied stream.
      *
+     * <p>Only the JVM's own standard output and error streams can render 
ANSI, through JLine's Jansi
+     * {@code AnsiConsole}, which also enables virtual terminal processing on 
Windows. A stream that is
+     * redirected to a file or a pipe gets no ANSI writer, so no escape 
sequences reach it.</p>
+     *
      * @param stream the output stream to wrap
      * @return an ANSI render writer, or {@code null} if ANSI support is
      * unavailable
      */
     protected PrintWriter tryConstructRenderWriter(OutputStream stream) {
-        // load via reflection to avoid hard-coded dependency on jansi jar
+        if (!JANSI_AVAILABLE) {
+            return null;
+        }
         try {
-            return (PrintWriter) 
InvokerHelper.invokeConstructorOf(ANSI_RENDER_WRITER, new Object[]{stream, 
true});
-        } catch (ClassNotFoundException ignore) {
+            return JansiConsole.writerFor(stream);
+        } catch (IOError | RuntimeException | LinkageError e) {
             return null;
         }
     }
 
+    private static boolean isJansiAvailable() {
+        // JLine's Jansi is optional for groovy, so look it up before 
JansiConsole links against it
+        try {
+            Class.forName(ANSI_CONSOLE, false, IO.class.getClassLoader());
+            return true;
+        } catch (ClassNotFoundException | LinkageError e) {
+            return false;
+        }
+    }
+
+    /**
+     * Refers to JLine's Jansi, and is loaded only once {@link 
#isJansiAvailable()} found it.
+     */
+    private static final class JansiConsole {
+        static PrintWriter writerFor(OutputStream stream) {
+            AnsiPrintStream console;
+            // AnsiConsole writes to the streams the JVM started with, so a 
replaced System.out keeps its own
+            if (stream == System.out && System.out == AnsiConsole.sysOut()) {
+                console = AnsiConsole.out();
+            } else if (stream == System.err && System.err == 
AnsiConsole.sysErr()) {
+                console = AnsiConsole.err();
+            } else {
+                return null;
+            }
+            return console.getMode() == AnsiMode.Strip ? null : new 
PrintWriter(console, true);

Review Comment:
   Both reproduced: on JDK 25 a piped `groovydoc` printed the restricted-method 
warning, and a forked test that replaces `System.out` before logging through 
`Logger` (the Ant path) showed the line skipping the replaced stream and going 
to the process stdout.
   
   Fixed in 9bb03d5667:
   - `IO` leaves `AnsiConsole` untouched unless `jansi.mode` is `force`, or is 
not `strip` and the JVM has a terminal (`Console.isTerminal()` where the JDK 
has it), so piped and CI runs never load JLine.
   - The constructors give plain writers again. Only an IO from the new 
`IO.forConsole()` can color, and only groovydoc's `Main` uses it, so Ant, 
Gradle and embedded use through `Logger` keep writing through the host's 
`System.out`.
   
   In 4f31e60c14 the console is also opened only by `Logger`'s first colored 
line, not when the IO is created, and not at all if `System.out` was replaced 
in between.
   
   The new forked tests cover a `System.out` replaced before `Logger` creates 
its IO and one replaced before the first line. The redirected and `strip` tests 
assert that no console was opened and that stderr has no native-access warning.





> The shell Logger never prints ANSI colors because IO looks for the old Jansi
> ----------------------------------------------------------------------------
>
>                 Key: GROOVY-12784
>                 URL: https://issues.apache.org/jira/browse/GROOVY-12784
>             Project: Groovy
>          Issue Type: Bug
>    Affects Versions: 6.0.0
>            Reporter: Scott Murphy Heiberg
>            Priority: Major
>
> {{org.codehaus.groovy.tools.shell.IO}} decides whether to render ANSI by 
> looking for {{org.fusesource.jansi.AnsiRenderWriter}}. Since GROOVY-8162 
> Groovy ships JLine's Jansi ({{org.jline:jansi}}) instead, which has no such 
> class, so {{IO.ansiSupported}} is always {{false}} and the shell {{Logger}} 
> (used by groovydoc) never colors its DEBUG, WARN and ERROR prefixes, although 
> {{Logger}} already renders them with {{org.jline.jansi.Ansi}}.
> {{IO}} should detect JLine's Jansi and render through its {{AnsiConsole}}, 
> for the JVM's own standard output and error only, so output redirected to a 
> file or a pipe stays plain. The {{jansi.mode}} system property ({{force}} / 
> {{strip}}) still applies.



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to