Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Version 2.3.2 (2016-11-04)
# Version 2.3.2 (2016-11-09)

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,21 @@
*/
package org.seedstack.seed.core.internal.init;

import org.fusesource.jansi.AnsiConsole;
import org.fusesource.jansi.AnsiOutputStream;
import org.fusesource.jansi.WindowsAnsiOutputStream;

import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;

import static org.fusesource.jansi.internal.CLibrary.STDOUT_FILENO;
import static org.fusesource.jansi.internal.CLibrary.isatty;

public class ConsoleManager {
private volatile boolean colorSupported;
private PrintStream savedOut;
private PrintStream savedErr;

public synchronized void install() {
OutputStream out = wrapOutputStream(System.out);
OutputStream err = wrapOutputStream(System.err);
colorSupported = isColorSupported(out) && isColorSupported(err);
savedOut = System.out;
System.setOut(new PrintStream(out));
savedErr = System.err;
Expand All @@ -36,45 +31,50 @@ public synchronized void install() {
public synchronized void uninstall() {
System.setOut(savedOut);
System.setErr(savedErr);
colorSupported = false;
}

public boolean isColorSupported() {
return colorSupported;
}

private boolean isColorSupported(OutputStream outputStream) {
return outputStream instanceof ColorOutputStream || outputStream instanceof WindowsAnsiOutputStream;
// we cannot know if color is supported
return false;
}

private OutputStream wrapOutputStream(final OutputStream stream) {
try {
if (isIntelliJ() || isTTY() || isCygwin()) {
if (Boolean.getBoolean("jansi.passthrough")) {
// honor jansi passthrough
return stream;
} else if (Boolean.getBoolean("jansi.strip")) {
// honor jansi strip
return basicOutput(stream);
} else if (isXtermColor()) {
// enable color in recognized XTERM color modes
return ansiOutput(stream);
} else if (isIntelliJ()) {
// enable color under Intellij
return ansiOutput(stream);
} else if (isWindows()) {
return windowsOutput(stream);
} else {
return basicOutput(stream);
// let Jansi handle other detection
return AnsiConsole.wrapOutputStream(stream);
}
} catch (Throwable e) {
// If any error occurs, strip ANSI codes
return basicOutput(stream);
}
}

private FilterOutputStream ansiOutput(OutputStream stream) {
return new ColorOutputStream(stream);
private FilterOutputStream basicOutput(OutputStream stream) {
return new AnsiOutputStream(stream);
}

private FilterOutputStream windowsOutput(OutputStream stream) {
try {
return new WindowsAnsiOutputStream(stream);
} catch (Exception e) {
return basicOutput(stream);
}
private FilterOutputStream ansiOutput(OutputStream stream) {
return new ColorOutputStream(stream);
}

private FilterOutputStream basicOutput(OutputStream stream) {
return new AnsiOutputStream(stream);
private boolean isXtermColor() {
String term = System.getenv("TERM");
return "xterm-256color".equals(term) ||
"xterm-color".equals(term) ||
"xterm".equals(term);
}

private boolean isIntelliJ() {
Expand All @@ -86,22 +86,6 @@ private boolean isIntelliJ() {
}
}

private boolean isWindows() {
return System.getProperty("os.name").startsWith("Windows");
}

private boolean isCygwin() {
return isWindows() && System.getenv("TERM") != null;
}

private boolean isTTY() {
try {
return isatty(STDOUT_FILENO) != 0;
} catch (Exception e) {
return false;
}
}

private static class ColorOutputStream extends FilterOutputStream {
private ColorOutputStream(OutputStream stream) {
super(stream);
Expand Down
2 changes: 2 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
<metrics.version>3.1.2</metrics.version>
<jersey.version>1.19.1</jersey.version>
<jersey2.version>2.22.2</jersey2.version>
<!-- override JANSI version for now -->
<jansi.version>1.14</jansi.version>

<compatibility.version>2.1.0</compatibility.version>

Expand Down