Skip to content

Commit

Permalink
added explicit result of AnsiConsole system install on stdout&stderr
Browse files Browse the repository at this point in the history
  • Loading branch information
hboutemy committed Sep 16, 2017
1 parent 0484150 commit cf69386
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
21 changes: 21 additions & 0 deletions jansi/src/main/java/org/fusesource/jansi/AnsiConsole.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
}
Expand Down Expand Up @@ -99,26 +104,30 @@ 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);
}

if (IS_WINDOWS && !IS_CYGWIN && !IS_MINGW) {

// 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
// this happens when the stdout is being redirected to a file.
}

// Use the ANSIOutputStream to strip out the ANSI escape sequences.
jansiOutputType = JansiOutputType.STRIP_ANSI;
return new AnsiOutputStream(stream);
}

Expand All @@ -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) {
Expand All @@ -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 {
Expand Down Expand Up @@ -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
};
}
6 changes: 6 additions & 0 deletions jansi/src/main/java/org/fusesource/jansi/AnsiMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit cf69386

Please sign in to comment.