From cf69386f4010a05b5b122195957dd7f09b0d92ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Boutemy?= Date: Sat, 16 Sep 2017 16:53:17 +0200 Subject: [PATCH] added explicit result of AnsiConsole system install on stdout&stderr --- .../org/fusesource/jansi/AnsiConsole.java | 21 +++++++++++++++++++ .../java/org/fusesource/jansi/AnsiMain.java | 6 ++++++ 2 files changed, 27 insertions(+) diff --git a/jansi/src/main/java/org/fusesource/jansi/AnsiConsole.java b/jansi/src/main/java/org/fusesource/jansi/AnsiConsole.java index eb8567fe..0b7ae030 100644 --- a/jansi/src/main/java/org/fusesource/jansi/AnsiConsole.java +++ b/jansi/src/main/java/org/fusesource/jansi/AnsiConsole.java @@ -54,6 +54,9 @@ public class AnsiConsole { && System.getenv("MSYSTEM") != null && System.getenv("MSYSTEM").startsWith("MINGW"); + private static JansiOutputType jansiOutputType; + static final JansiOutputType JANSI_STDOUT_TYPE; + static final JansiOutputType JANSI_STDERR_TYPE; static { String charset = Charset.defaultCharset().name(); if (IS_WINDOWS && !IS_CYGWIN && !IS_MINGW) { @@ -67,7 +70,9 @@ public class AnsiConsole { } try { out = new PrintStream(wrapOutputStream(system_out), false, charset); + JANSI_STDOUT_TYPE = jansiOutputType; err = new PrintStream(wrapErrorOutputStream(system_err), false, charset); + JANSI_STDERR_TYPE = jansiOutputType; } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } @@ -99,12 +104,14 @@ public static OutputStream wrapOutputStream(final OutputStream stream, int filen // If the jansi.passthrough property is set, then don't interpret // any of the ansi sequences. if (Boolean.getBoolean("jansi.passthrough")) { + jansiOutputType = JansiOutputType.PASSTHROUGH; return stream; } // If the jansi.strip property is set, then we just strip the // the ansi escapes. if (Boolean.getBoolean("jansi.strip")) { + jansiOutputType = JansiOutputType.STRIP_ANSI; return new AnsiOutputStream(stream); } @@ -112,6 +119,7 @@ public static OutputStream wrapOutputStream(final OutputStream stream, int filen // On windows we know the console does not interpret ANSI codes.. try { + jansiOutputType = JansiOutputType.WINDOWS; return new WindowsAnsiOutputStream(stream); } catch (Throwable ignore) { // this happens when JNA is not in the path.. or @@ -119,6 +127,7 @@ public static OutputStream wrapOutputStream(final OutputStream stream, int filen } // Use the ANSIOutputStream to strip out the ANSI escape sequences. + jansiOutputType = JansiOutputType.STRIP_ANSI; return new AnsiOutputStream(stream); } @@ -130,6 +139,7 @@ public static OutputStream wrapOutputStream(final OutputStream stream, int filen // If we can detect that stdout is not a tty.. then setup // to strip the ANSI sequences.. if (!forceColored && isatty(fileno) == 0) { + jansiOutputType = JansiOutputType.STRIP_ANSI; return new AnsiOutputStream(stream); } } catch (Throwable ignore) { @@ -140,6 +150,7 @@ public static OutputStream wrapOutputStream(final OutputStream stream, int filen // By default we assume your Unix tty can handle ANSI codes. // Just wrap it up so that when we get closed, we reset the // attributes. + jansiOutputType = JansiOutputType.RESET_ANSI_AT_CLOSE; return new FilterOutputStream(stream) { @Override public void close() throws IOException { @@ -198,4 +209,14 @@ synchronized public static void systemUninstall() { } } + /** + * Type of output installed by AnsiConsole. + */ + enum JansiOutputType { + PASSTHROUGH, // just pass through, ANSI escape codes are supposed to be supported by terminal + STRIP_ANSI, // strip ANSI escape codes (since not a terminal) + WINDOWS, // detect ANSI escape codes and transform Jansi-supported ones into a Windows API to get desired effect + // (since ANSI escape codes are not natively supported by Windows terminals like cmd.exe or PowerShell) + RESET_ANSI_AT_CLOSE // like pass through but reset ANSI attributes when closing + }; } diff --git a/jansi/src/main/java/org/fusesource/jansi/AnsiMain.java b/jansi/src/main/java/org/fusesource/jansi/AnsiMain.java index 101f62a3..3a1e6bbb 100644 --- a/jansi/src/main/java/org/fusesource/jansi/AnsiMain.java +++ b/jansi/src/main/java/org/fusesource/jansi/AnsiMain.java @@ -83,6 +83,12 @@ public static void main(String... args) throws IOException { diagnoseTty(true); // System.err AnsiConsole.systemInstall(); + + System.out.println(); + + System.out.println("Jansi System.out mode: " + AnsiConsole.JANSI_STDOUT_TYPE); + System.out.println("Jansi System.err mode: " + AnsiConsole.JANSI_STDERR_TYPE); + try { if (args.length == 0) { printJansiLogoDemo();