[
https://issues.apache.org/jira/browse/GROOVY-12784?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18146793#comment-18146793
]
ASF GitHub Bot commented on GROOVY-12784:
-----------------------------------------
daniellansun commented on code in PR #3309:
URL: https://github.com/apache/groovy/pull/3309#discussion_r4616106391
##########
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:
`AnsiConsole.out()` / `err()` always run `doInstall()` before this method
can see that the mode is `Strip`, so a piped or CI `new IO()` still loads the
JNI library and, on JDK 24+, prints a native-access warning, then throws the
console away. Could we skip `AnsiConsole` when there is no terminal and
`jansi.mode` is not `force`?
The `System.out == AnsiConsole.sysOut()` check also only sees a replacement
made after `AnsiConsole` was initialized. A host such as Ant that replaced
`System.out` earlier passes the check, and on a TTY `AnsiConsole.out()` then
writes to `terminal.output()` rather than through the host stream, which would
bypass Ant's logger, `-quiet` and `-logfile`. Could we wrap the caller-provided
stream, or otherwise refuse `AnsiConsole.out()` when the stream is not known to
be the JVM's original one?
> 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)