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?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]