@@ -54,6 +54,9 @@ public class AnsiConsole {
54
54
&& System .getenv ("MSYSTEM" ) != null
55
55
&& System .getenv ("MSYSTEM" ).startsWith ("MINGW" );
56
56
57
+ private static JansiOutputType jansiOutputType ;
58
+ static final JansiOutputType JANSI_STDOUT_TYPE ;
59
+ static final JansiOutputType JANSI_STDERR_TYPE ;
57
60
static {
58
61
String charset = Charset .defaultCharset ().name ();
59
62
if (IS_WINDOWS && !IS_CYGWIN && !IS_MINGW ) {
@@ -67,7 +70,9 @@ public class AnsiConsole {
67
70
}
68
71
try {
69
72
out = new PrintStream (wrapOutputStream (system_out ), false , charset );
73
+ JANSI_STDOUT_TYPE = jansiOutputType ;
70
74
err = new PrintStream (wrapErrorOutputStream (system_err ), false , charset );
75
+ JANSI_STDERR_TYPE = jansiOutputType ;
71
76
} catch (UnsupportedEncodingException e ) {
72
77
throw new RuntimeException (e );
73
78
}
@@ -99,26 +104,30 @@ public static OutputStream wrapOutputStream(final OutputStream stream, int filen
99
104
// If the jansi.passthrough property is set, then don't interpret
100
105
// any of the ansi sequences.
101
106
if (Boolean .getBoolean ("jansi.passthrough" )) {
107
+ jansiOutputType = JansiOutputType .PASSTHROUGH ;
102
108
return stream ;
103
109
}
104
110
105
111
// If the jansi.strip property is set, then we just strip the
106
112
// the ansi escapes.
107
113
if (Boolean .getBoolean ("jansi.strip" )) {
114
+ jansiOutputType = JansiOutputType .STRIP_ANSI ;
108
115
return new AnsiOutputStream (stream );
109
116
}
110
117
111
118
if (IS_WINDOWS && !IS_CYGWIN && !IS_MINGW ) {
112
119
113
120
// On windows we know the console does not interpret ANSI codes..
114
121
try {
122
+ jansiOutputType = JansiOutputType .WINDOWS ;
115
123
return new WindowsAnsiOutputStream (stream );
116
124
} catch (Throwable ignore ) {
117
125
// this happens when JNA is not in the path.. or
118
126
// this happens when the stdout is being redirected to a file.
119
127
}
120
128
121
129
// Use the ANSIOutputStream to strip out the ANSI escape sequences.
130
+ jansiOutputType = JansiOutputType .STRIP_ANSI ;
122
131
return new AnsiOutputStream (stream );
123
132
}
124
133
@@ -130,6 +139,7 @@ public static OutputStream wrapOutputStream(final OutputStream stream, int filen
130
139
// If we can detect that stdout is not a tty.. then setup
131
140
// to strip the ANSI sequences..
132
141
if (!forceColored && isatty (fileno ) == 0 ) {
142
+ jansiOutputType = JansiOutputType .STRIP_ANSI ;
133
143
return new AnsiOutputStream (stream );
134
144
}
135
145
} catch (Throwable ignore ) {
@@ -140,6 +150,7 @@ public static OutputStream wrapOutputStream(final OutputStream stream, int filen
140
150
// By default we assume your Unix tty can handle ANSI codes.
141
151
// Just wrap it up so that when we get closed, we reset the
142
152
// attributes.
153
+ jansiOutputType = JansiOutputType .RESET_ANSI_AT_CLOSE ;
143
154
return new FilterOutputStream (stream ) {
144
155
@ Override
145
156
public void close () throws IOException {
@@ -198,4 +209,14 @@ synchronized public static void systemUninstall() {
198
209
}
199
210
}
200
211
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
+ };
201
222
}
0 commit comments