@@ -42,10 +42,20 @@ public class AnsiConsole {
42
42
public static final PrintStream system_err = System .err ;
43
43
public static final PrintStream err ;
44
44
45
+ private static final boolean IS_WINDOWS = System .getProperty ("os.name" ).toLowerCase ().contains ("win" );
46
+
47
+ private static final boolean IS_CYGWIN = IS_WINDOWS
48
+ && System .getenv ("PWD" ) != null
49
+ && System .getenv ("PWD" ).startsWith ("/" )
50
+ && !"cygwin" .equals (System .getenv ("TERM" ));
51
+
52
+ private static final boolean IS_MINGW = IS_WINDOWS
53
+ && System .getenv ("MSYSTEM" ) != null
54
+ && System .getenv ("MSYSTEM" ).startsWith ("MINGW" );
55
+
45
56
static {
46
57
String charset = Charset .defaultCharset ().name ();
47
- String os = System .getProperty ("os.name" );
48
- if (os .startsWith ("Windows" ) && !isXterm ()) {
58
+ if (IS_WINDOWS && !IS_CYGWIN && !IS_MINGW ) {
49
59
int codepage = Kernel32 .GetConsoleOutputCP ();
50
60
//http://docs.oracle.com/javase/6/docs/technotes/guides/intl/encoding.doc.html
51
61
if (Charset .isSupported ("ms" + codepage )) {
@@ -71,15 +81,15 @@ public static OutputStream wrapOutputStream(final OutputStream stream) {
71
81
try {
72
82
return wrapOutputStream (stream , STDOUT_FILENO );
73
83
} catch (Throwable ignore ) {
74
- return wrapOutputStream (stream , 0 );
84
+ return wrapOutputStream (stream , 1 );
75
85
}
76
86
}
77
87
78
88
public static OutputStream wrapErrorOutputStream (final OutputStream stream ) {
79
89
try {
80
90
return wrapOutputStream (stream , STDERR_FILENO );
81
91
} catch (Throwable ignore ) {
82
- return wrapOutputStream (stream , 0 );
92
+ return wrapOutputStream (stream , 2 );
83
93
}
84
94
}
85
95
@@ -97,8 +107,7 @@ public static OutputStream wrapOutputStream(final OutputStream stream, int filen
97
107
return new AnsiOutputStream (stream );
98
108
}
99
109
100
- String os = System .getProperty ("os.name" );
101
- if (os .startsWith ("Windows" ) && !isXterm ()) {
110
+ if (IS_WINDOWS && !IS_CYGWIN && !IS_MINGW ) {
102
111
103
112
// On windows we know the console does not interpret ANSI codes..
104
113
try {
@@ -119,7 +128,7 @@ public static OutputStream wrapOutputStream(final OutputStream stream, int filen
119
128
boolean forceColored = Boolean .getBoolean ("jansi.force" );
120
129
// If we can detect that stdout is not a tty.. then setup
121
130
// to strip the ANSI sequences..
122
- if (!isXterm () && ! forceColored && isatty (fileno ) == 0 ) {
131
+ if (!forceColored && isatty (fileno ) == 0 ) {
123
132
return new AnsiOutputStream (stream );
124
133
}
125
134
} catch (Throwable ignore ) {
@@ -140,11 +149,6 @@ public void close() throws IOException {
140
149
};
141
150
}
142
151
143
- private static boolean isXterm () {
144
- String term = System .getenv ("TERM" );
145
- return term != null && term .startsWith ("xterm" );
146
- }
147
-
148
152
/**
149
153
* If the standard out natively supports ANSI escape codes, then this just
150
154
* returns System.out, otherwise it will provide an ANSI aware PrintStream
0 commit comments