Skip to content

Commit cf69386

Browse files
committed
added explicit result of AnsiConsole system install on stdout&stderr
1 parent 0484150 commit cf69386

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

jansi/src/main/java/org/fusesource/jansi/AnsiConsole.java

+21
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ public class AnsiConsole {
5454
&& System.getenv("MSYSTEM") != null
5555
&& System.getenv("MSYSTEM").startsWith("MINGW");
5656

57+
private static JansiOutputType jansiOutputType;
58+
static final JansiOutputType JANSI_STDOUT_TYPE;
59+
static final JansiOutputType JANSI_STDERR_TYPE;
5760
static {
5861
String charset = Charset.defaultCharset().name();
5962
if (IS_WINDOWS && !IS_CYGWIN && !IS_MINGW) {
@@ -67,7 +70,9 @@ public class AnsiConsole {
6770
}
6871
try {
6972
out = new PrintStream(wrapOutputStream(system_out), false, charset);
73+
JANSI_STDOUT_TYPE = jansiOutputType;
7074
err = new PrintStream(wrapErrorOutputStream(system_err), false, charset);
75+
JANSI_STDERR_TYPE = jansiOutputType;
7176
} catch (UnsupportedEncodingException e) {
7277
throw new RuntimeException(e);
7378
}
@@ -99,26 +104,30 @@ public static OutputStream wrapOutputStream(final OutputStream stream, int filen
99104
// If the jansi.passthrough property is set, then don't interpret
100105
// any of the ansi sequences.
101106
if (Boolean.getBoolean("jansi.passthrough")) {
107+
jansiOutputType = JansiOutputType.PASSTHROUGH;
102108
return stream;
103109
}
104110

105111
// If the jansi.strip property is set, then we just strip the
106112
// the ansi escapes.
107113
if (Boolean.getBoolean("jansi.strip")) {
114+
jansiOutputType = JansiOutputType.STRIP_ANSI;
108115
return new AnsiOutputStream(stream);
109116
}
110117

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

113120
// On windows we know the console does not interpret ANSI codes..
114121
try {
122+
jansiOutputType = JansiOutputType.WINDOWS;
115123
return new WindowsAnsiOutputStream(stream);
116124
} catch (Throwable ignore) {
117125
// this happens when JNA is not in the path.. or
118126
// this happens when the stdout is being redirected to a file.
119127
}
120128

121129
// Use the ANSIOutputStream to strip out the ANSI escape sequences.
130+
jansiOutputType = JansiOutputType.STRIP_ANSI;
122131
return new AnsiOutputStream(stream);
123132
}
124133

@@ -130,6 +139,7 @@ public static OutputStream wrapOutputStream(final OutputStream stream, int filen
130139
// If we can detect that stdout is not a tty.. then setup
131140
// to strip the ANSI sequences..
132141
if (!forceColored && isatty(fileno) == 0) {
142+
jansiOutputType = JansiOutputType.STRIP_ANSI;
133143
return new AnsiOutputStream(stream);
134144
}
135145
} catch (Throwable ignore) {
@@ -140,6 +150,7 @@ public static OutputStream wrapOutputStream(final OutputStream stream, int filen
140150
// By default we assume your Unix tty can handle ANSI codes.
141151
// Just wrap it up so that when we get closed, we reset the
142152
// attributes.
153+
jansiOutputType = JansiOutputType.RESET_ANSI_AT_CLOSE;
143154
return new FilterOutputStream(stream) {
144155
@Override
145156
public void close() throws IOException {
@@ -198,4 +209,14 @@ synchronized public static void systemUninstall() {
198209
}
199210
}
200211

212+
/**
213+
* Type of output installed by AnsiConsole.
214+
*/
215+
enum JansiOutputType {
216+
PASSTHROUGH, // just pass through, ANSI escape codes are supposed to be supported by terminal
217+
STRIP_ANSI, // strip ANSI escape codes (since not a terminal)
218+
WINDOWS, // detect ANSI escape codes and transform Jansi-supported ones into a Windows API to get desired effect
219+
// (since ANSI escape codes are not natively supported by Windows terminals like cmd.exe or PowerShell)
220+
RESET_ANSI_AT_CLOSE // like pass through but reset ANSI attributes when closing
221+
};
201222
}

jansi/src/main/java/org/fusesource/jansi/AnsiMain.java

+6
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,12 @@ public static void main(String... args) throws IOException {
8383
diagnoseTty(true); // System.err
8484

8585
AnsiConsole.systemInstall();
86+
87+
System.out.println();
88+
89+
System.out.println("Jansi System.out mode: " + AnsiConsole.JANSI_STDOUT_TYPE);
90+
System.out.println("Jansi System.err mode: " + AnsiConsole.JANSI_STDERR_TYPE);
91+
8692
try {
8793
if (args.length == 0) {
8894
printJansiLogoDemo();

0 commit comments

Comments
 (0)