Skip to content

Commit e32d817

Browse files
authored
Merge pull request #219 from adrienlauer/fix-windows-colors
Fix colors under windows
2 parents 64f9a11 + 20313c7 commit e32d817

File tree

3 files changed

+30
-43
lines changed

3 files changed

+30
-43
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
# Version 2.3.2 (2016-11-04)
1+
# Version 2.3.2 (2016-11-09)
22

3+
* [fix] Fix colors under Windows command-line
34
* [fix] Fix wrong priority of CORS filter which was below security filter.
45
* [fix] Fix binding of `X509CertificateFilter` which was bound multiple times.
56

core/src/main/java/org/seedstack/seed/core/internal/init/ConsoleManager.java

Lines changed: 26 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,21 @@
77
*/
88
package org.seedstack.seed.core.internal.init;
99

10+
import org.fusesource.jansi.AnsiConsole;
1011
import org.fusesource.jansi.AnsiOutputStream;
11-
import org.fusesource.jansi.WindowsAnsiOutputStream;
1212

1313
import java.io.FilterOutputStream;
1414
import java.io.IOException;
1515
import java.io.OutputStream;
1616
import java.io.PrintStream;
1717

18-
import static org.fusesource.jansi.internal.CLibrary.STDOUT_FILENO;
19-
import static org.fusesource.jansi.internal.CLibrary.isatty;
20-
2118
public class ConsoleManager {
22-
private volatile boolean colorSupported;
2319
private PrintStream savedOut;
2420
private PrintStream savedErr;
2521

2622
public synchronized void install() {
2723
OutputStream out = wrapOutputStream(System.out);
2824
OutputStream err = wrapOutputStream(System.err);
29-
colorSupported = isColorSupported(out) && isColorSupported(err);
3025
savedOut = System.out;
3126
System.setOut(new PrintStream(out));
3227
savedErr = System.err;
@@ -36,45 +31,50 @@ public synchronized void install() {
3631
public synchronized void uninstall() {
3732
System.setOut(savedOut);
3833
System.setErr(savedErr);
39-
colorSupported = false;
4034
}
4135

4236
public boolean isColorSupported() {
43-
return colorSupported;
44-
}
45-
46-
private boolean isColorSupported(OutputStream outputStream) {
47-
return outputStream instanceof ColorOutputStream || outputStream instanceof WindowsAnsiOutputStream;
37+
// we cannot know if color is supported
38+
return false;
4839
}
4940

5041
private OutputStream wrapOutputStream(final OutputStream stream) {
5142
try {
52-
if (isIntelliJ() || isTTY() || isCygwin()) {
43+
if (Boolean.getBoolean("jansi.passthrough")) {
44+
// honor jansi passthrough
45+
return stream;
46+
} else if (Boolean.getBoolean("jansi.strip")) {
47+
// honor jansi strip
48+
return basicOutput(stream);
49+
} else if (isXtermColor()) {
50+
// enable color in recognized XTERM color modes
51+
return ansiOutput(stream);
52+
} else if (isIntelliJ()) {
53+
// enable color under Intellij
5354
return ansiOutput(stream);
54-
} else if (isWindows()) {
55-
return windowsOutput(stream);
5655
} else {
57-
return basicOutput(stream);
56+
// let Jansi handle other detection
57+
return AnsiConsole.wrapOutputStream(stream);
5858
}
5959
} catch (Throwable e) {
60+
// If any error occurs, strip ANSI codes
6061
return basicOutput(stream);
6162
}
6263
}
6364

64-
private FilterOutputStream ansiOutput(OutputStream stream) {
65-
return new ColorOutputStream(stream);
65+
private FilterOutputStream basicOutput(OutputStream stream) {
66+
return new AnsiOutputStream(stream);
6667
}
6768

68-
private FilterOutputStream windowsOutput(OutputStream stream) {
69-
try {
70-
return new WindowsAnsiOutputStream(stream);
71-
} catch (Exception e) {
72-
return basicOutput(stream);
73-
}
69+
private FilterOutputStream ansiOutput(OutputStream stream) {
70+
return new ColorOutputStream(stream);
7471
}
7572

76-
private FilterOutputStream basicOutput(OutputStream stream) {
77-
return new AnsiOutputStream(stream);
73+
private boolean isXtermColor() {
74+
String term = System.getenv("TERM");
75+
return "xterm-256color".equals(term) ||
76+
"xterm-color".equals(term) ||
77+
"xterm".equals(term);
7878
}
7979

8080
private boolean isIntelliJ() {
@@ -86,22 +86,6 @@ private boolean isIntelliJ() {
8686
}
8787
}
8888

89-
private boolean isWindows() {
90-
return System.getProperty("os.name").startsWith("Windows");
91-
}
92-
93-
private boolean isCygwin() {
94-
return isWindows() && System.getenv("TERM") != null;
95-
}
96-
97-
private boolean isTTY() {
98-
try {
99-
return isatty(STDOUT_FILENO) != 0;
100-
} catch (Exception e) {
101-
return false;
102-
}
103-
}
104-
10589
private static class ColorOutputStream extends FilterOutputStream {
10690
private ColorOutputStream(OutputStream stream) {
10791
super(stream);

pom.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
<metrics.version>3.1.2</metrics.version>
3636
<jersey.version>1.19.1</jersey.version>
3737
<jersey2.version>2.22.2</jersey2.version>
38+
<!-- override JANSI version for now -->
39+
<jansi.version>1.14</jansi.version>
3840

3941
<compatibility.version>2.1.0</compatibility.version>
4042

0 commit comments

Comments
 (0)