Skip to content

Commit bb3d538

Browse files
committed
ANSI output stripping does not work if TERM is xterm, fixes #83
1 parent 3c82c33 commit bb3d538

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

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

+16-12
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,20 @@ public class AnsiConsole {
4242
public static final PrintStream system_err = System.err;
4343
public static final PrintStream err;
4444

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+
4556
static {
4657
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) {
4959
int codepage = Kernel32.GetConsoleOutputCP();
5060
//http://docs.oracle.com/javase/6/docs/technotes/guides/intl/encoding.doc.html
5161
if (Charset.isSupported("ms" + codepage)) {
@@ -71,15 +81,15 @@ public static OutputStream wrapOutputStream(final OutputStream stream) {
7181
try {
7282
return wrapOutputStream(stream, STDOUT_FILENO);
7383
} catch (Throwable ignore) {
74-
return wrapOutputStream(stream, 0);
84+
return wrapOutputStream(stream, 1);
7585
}
7686
}
7787

7888
public static OutputStream wrapErrorOutputStream(final OutputStream stream) {
7989
try {
8090
return wrapOutputStream(stream, STDERR_FILENO);
8191
} catch (Throwable ignore) {
82-
return wrapOutputStream(stream, 0);
92+
return wrapOutputStream(stream, 2);
8393
}
8494
}
8595

@@ -97,8 +107,7 @@ public static OutputStream wrapOutputStream(final OutputStream stream, int filen
97107
return new AnsiOutputStream(stream);
98108
}
99109

100-
String os = System.getProperty("os.name");
101-
if (os.startsWith("Windows") && !isXterm()) {
110+
if (IS_WINDOWS && !IS_CYGWIN && !IS_MINGW) {
102111

103112
// On windows we know the console does not interpret ANSI codes..
104113
try {
@@ -119,7 +128,7 @@ public static OutputStream wrapOutputStream(final OutputStream stream, int filen
119128
boolean forceColored = Boolean.getBoolean("jansi.force");
120129
// If we can detect that stdout is not a tty.. then setup
121130
// to strip the ANSI sequences..
122-
if (!isXterm() && !forceColored && isatty(fileno) == 0) {
131+
if (!forceColored && isatty(fileno) == 0) {
123132
return new AnsiOutputStream(stream);
124133
}
125134
} catch (Throwable ignore) {
@@ -140,11 +149,6 @@ public void close() throws IOException {
140149
};
141150
}
142151

143-
private static boolean isXterm() {
144-
String term = System.getenv("TERM");
145-
return term != null && term.startsWith("xterm");
146-
}
147-
148152
/**
149153
* If the standard out natively supports ANSI escape codes, then this just
150154
* returns System.out, otherwise it will provide an ANSI aware PrintStream

0 commit comments

Comments
 (0)