Skip to content

Commit

Permalink
ANSI output stripping does not work if TERM is xterm, fixes #83
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Apr 26, 2017
1 parent 3c82c33 commit bb3d538
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions jansi/src/main/java/org/fusesource/jansi/AnsiConsole.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,20 @@ public class AnsiConsole {
public static final PrintStream system_err = System.err;
public static final PrintStream err;

private static final boolean IS_WINDOWS = System.getProperty("os.name").toLowerCase().contains("win");

This comment has been minimized.

Copy link
@michael-o

michael-o Apr 27, 2017

Contributor

You should use Locale.ROOT or Locale.ENGLISH for the lowercase. Avoiding stull like Turkish dotless i.


private static final boolean IS_CYGWIN = IS_WINDOWS
&& System.getenv("PWD") != null
&& System.getenv("PWD").startsWith("/")
&& !"cygwin".equals(System.getenv("TERM"));

private static final boolean IS_MINGW = IS_WINDOWS
&& System.getenv("MSYSTEM") != null
&& System.getenv("MSYSTEM").startsWith("MINGW");

static {
String charset = Charset.defaultCharset().name();
String os = System.getProperty("os.name");
if (os.startsWith("Windows") && !isXterm()) {
if (IS_WINDOWS && !IS_CYGWIN && !IS_MINGW) {
int codepage = Kernel32.GetConsoleOutputCP();
//http://docs.oracle.com/javase/6/docs/technotes/guides/intl/encoding.doc.html
if (Charset.isSupported("ms" + codepage)) {
Expand All @@ -71,15 +81,15 @@ public static OutputStream wrapOutputStream(final OutputStream stream) {
try {
return wrapOutputStream(stream, STDOUT_FILENO);
} catch (Throwable ignore) {
return wrapOutputStream(stream, 0);
return wrapOutputStream(stream, 1);
}
}

public static OutputStream wrapErrorOutputStream(final OutputStream stream) {
try {
return wrapOutputStream(stream, STDERR_FILENO);
} catch (Throwable ignore) {
return wrapOutputStream(stream, 0);
return wrapOutputStream(stream, 2);
}
}

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

String os = System.getProperty("os.name");
if (os.startsWith("Windows") && !isXterm()) {
if (IS_WINDOWS && !IS_CYGWIN && !IS_MINGW) {

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

private static boolean isXterm() {
String term = System.getenv("TERM");
return term != null && term.startsWith("xterm");
}

/**
* If the standard out natively supports ANSI escape codes, then this just
* returns System.out, otherwise it will provide an ANSI aware PrintStream
Expand Down

1 comment on commit bb3d538

@michael-o
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks reasonable to me.

Please sign in to comment.