Skip to content

Commit

Permalink
#124 detect console handle from stderr separately from stdout
Browse files Browse the repository at this point in the history
  • Loading branch information
hboutemy authored and gnodet committed May 14, 2018
1 parent acd9ed2 commit e2ac629
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
4 changes: 2 additions & 2 deletions jansi/src/main/java/org/fusesource/jansi/AnsiConsole.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public static OutputStream wrapOutputStream(final OutputStream stream, int filen
// On windows we know the console does not interpret ANSI codes..
try {
jansiOutputType = JansiOutputType.WINDOWS;
return new WindowsAnsiOutputStream(stream);
return new WindowsAnsiOutputStream(stream, fileno == STDOUT_FILENO);
} catch (Throwable ignore) {
// this happens when JNA is not in the path.. or
// this happens when the stdout is being redirected to a file.
Expand Down Expand Up @@ -204,7 +204,7 @@ public static PrintStream wrapPrintStream(final PrintStream ps, int fileno) {
// On windows we know the console does not interpret ANSI codes..
try {
jansiOutputType = JansiOutputType.WINDOWS;
return new WindowsAnsiPrintStream(ps);
return new WindowsAnsiPrintStream(ps, fileno == STDOUT_FILENO);
} catch (Throwable ignore) {
// this happens when JNA is not in the path.. or
// this happens when the stdout is being redirected to a file.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import static org.fusesource.jansi.internal.Kernel32.GetConsoleScreenBufferInfo;
import static org.fusesource.jansi.internal.Kernel32.GetStdHandle;
import static org.fusesource.jansi.internal.Kernel32.SMALL_RECT;
import static org.fusesource.jansi.internal.Kernel32.STD_ERROR_HANDLE;
import static org.fusesource.jansi.internal.Kernel32.STD_OUTPUT_HANDLE;
import static org.fusesource.jansi.internal.Kernel32.ScrollConsoleScreenBuffer;
import static org.fusesource.jansi.internal.Kernel32.SetConsoleCursorPosition;
Expand All @@ -54,7 +55,9 @@
*/
public final class WindowsAnsiOutputStream extends AnsiOutputStream { // expected diff with WindowsAnsiPrintStream.java

private static final long console = GetStdHandle(STD_OUTPUT_HANDLE);
private static final long stdout_handle = GetStdHandle(STD_OUTPUT_HANDLE);
private static final long stderr_handle = GetStdHandle(STD_ERROR_HANDLE);
private final long console;

private static final short FOREGROUND_BLACK = 0;
private static final short FOREGROUND_YELLOW = (short) (FOREGROUND_RED | FOREGROUND_GREEN);
Expand Down Expand Up @@ -97,12 +100,17 @@ public final class WindowsAnsiOutputStream extends AnsiOutputStream { // expecte
private short savedX = -1;
private short savedY = -1;

public WindowsAnsiOutputStream(OutputStream os) throws IOException { // expected diff with WindowsAnsiPrintStream.java
public WindowsAnsiOutputStream(OutputStream os, boolean stdout) throws IOException { // expected diff with WindowsAnsiPrintStream.java
super(os); // expected diff with WindowsAnsiPrintStream.java
this.console = stdout ? stdout_handle : stderr_handle;
getConsoleInfo();
originalColors = info.attributes;
}

public WindowsAnsiOutputStream(OutputStream os) throws IOException { // expected diff with WindowsAnsiPrintStream.java
this(os, true); // expected diff with WindowsAnsiPrintStream.java
}

private void getConsoleInfo() throws IOException {
out.flush(); // expected diff with WindowsAnsiPrintStream.java
if (GetConsoleScreenBufferInfo(console, info) == 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import static org.fusesource.jansi.internal.Kernel32.GetStdHandle;
import static org.fusesource.jansi.internal.Kernel32.SMALL_RECT;
import static org.fusesource.jansi.internal.Kernel32.STD_OUTPUT_HANDLE;
import static org.fusesource.jansi.internal.Kernel32.STD_ERROR_HANDLE;
import static org.fusesource.jansi.internal.Kernel32.ScrollConsoleScreenBuffer;
import static org.fusesource.jansi.internal.Kernel32.SetConsoleCursorPosition;
import static org.fusesource.jansi.internal.Kernel32.SetConsoleTextAttribute;
Expand All @@ -54,7 +55,9 @@
*/
public final class WindowsAnsiPrintStream extends AnsiPrintStream { // expected diff with WindowsAnsiOutputStream.java

private static final long console = GetStdHandle(STD_OUTPUT_HANDLE);
private static final long stdout_handle = GetStdHandle(STD_OUTPUT_HANDLE);
private static final long stderr_handle = GetStdHandle(STD_ERROR_HANDLE);
private final long console;

private static final short FOREGROUND_BLACK = 0;
private static final short FOREGROUND_YELLOW = (short) (FOREGROUND_RED | FOREGROUND_GREEN);
Expand Down Expand Up @@ -97,12 +100,17 @@ public final class WindowsAnsiPrintStream extends AnsiPrintStream { // expected
private short savedX = -1;
private short savedY = -1;

public WindowsAnsiPrintStream(PrintStream ps) throws IOException { // expected diff with WindowsAnsiOutputStream.java
public WindowsAnsiPrintStream(PrintStream ps, boolean stdout) throws IOException { // expected diff with WindowsAnsiOutputStream.java
super(ps); // expected diff with WindowsAnsiOutputStream.java
this.console = stdout ? stdout_handle : stderr_handle;
getConsoleInfo();
originalColors = info.attributes;
}

public WindowsAnsiPrintStream(PrintStream ps) throws IOException { // expected diff with WindowsAnsiOutputStream.java
this(ps, true); // expected diff with WindowsAnsiOutputStream.java
}

private void getConsoleInfo() throws IOException {
ps.flush(); // expected diff with WindowsAnsiOutputStream.java
if (GetConsoleScreenBufferInfo(console, info) == 0) {
Expand Down

0 comments on commit e2ac629

Please sign in to comment.