Skip to content

Commit 9338527

Browse files
committed
#124 detect console handle from stderr separately from stdout
1 parent 2a505ba commit 9338527

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public static OutputStream wrapOutputStream(final OutputStream stream, int filen
126126
// On windows we know the console does not interpret ANSI codes..
127127
try {
128128
jansiOutputType = JansiOutputType.WINDOWS;
129-
return new WindowsAnsiOutputStream(stream);
129+
return new WindowsAnsiOutputStream(stream, fileno == STDOUT_FILENO);
130130
} catch (Throwable ignore) {
131131
// this happens when JNA is not in the path.. or
132132
// this happens when the stdout is being redirected to a file.
@@ -204,7 +204,7 @@ public static PrintStream wrapPrintStream(final PrintStream ps, int fileno) {
204204
// On windows we know the console does not interpret ANSI codes..
205205
try {
206206
jansiOutputType = JansiOutputType.WINDOWS;
207-
return new WindowsAnsiPrintStream(ps);
207+
return new WindowsAnsiPrintStream(ps, fileno == STDOUT_FILENO);
208208
} catch (Throwable ignore) {
209209
// this happens when JNA is not in the path.. or
210210
// this happens when the stdout is being redirected to a file.

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

+10-2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import static org.fusesource.jansi.internal.Kernel32.GetConsoleScreenBufferInfo;
3030
import static org.fusesource.jansi.internal.Kernel32.GetStdHandle;
3131
import static org.fusesource.jansi.internal.Kernel32.SMALL_RECT;
32+
import static org.fusesource.jansi.internal.Kernel32.STD_ERROR_HANDLE;
3233
import static org.fusesource.jansi.internal.Kernel32.STD_OUTPUT_HANDLE;
3334
import static org.fusesource.jansi.internal.Kernel32.ScrollConsoleScreenBuffer;
3435
import static org.fusesource.jansi.internal.Kernel32.SetConsoleCursorPosition;
@@ -54,7 +55,9 @@
5455
*/
5556
public final class WindowsAnsiOutputStream extends AnsiOutputStream { // expected diff with WindowsAnsiPrintStream.java
5657

57-
private static final long console = GetStdHandle(STD_OUTPUT_HANDLE);
58+
private static final long stdout_handle = GetStdHandle(STD_OUTPUT_HANDLE);
59+
private static final long stderr_handle = GetStdHandle(STD_ERROR_HANDLE);
60+
private final long console;
5861

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

100-
public WindowsAnsiOutputStream(OutputStream os) throws IOException { // expected diff with WindowsAnsiPrintStream.java
103+
public WindowsAnsiOutputStream(OutputStream os, boolean stdout) throws IOException { // expected diff with WindowsAnsiPrintStream.java
101104
super(os); // expected diff with WindowsAnsiPrintStream.java
105+
this.console = stdout ? stdout_handle : stderr_handle;
102106
getConsoleInfo();
103107
originalColors = info.attributes;
104108
}
105109

110+
public WindowsAnsiOutputStream(OutputStream os) throws IOException { // expected diff with WindowsAnsiPrintStream.java
111+
this(os, true); // expected diff with WindowsAnsiPrintStream.java
112+
}
113+
106114
private void getConsoleInfo() throws IOException {
107115
out.flush(); // expected diff with WindowsAnsiPrintStream.java
108116
if (GetConsoleScreenBufferInfo(console, info) == 0) {

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

+10-2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import static org.fusesource.jansi.internal.Kernel32.GetStdHandle;
3131
import static org.fusesource.jansi.internal.Kernel32.SMALL_RECT;
3232
import static org.fusesource.jansi.internal.Kernel32.STD_OUTPUT_HANDLE;
33+
import static org.fusesource.jansi.internal.Kernel32.STD_ERROR_HANDLE;
3334
import static org.fusesource.jansi.internal.Kernel32.ScrollConsoleScreenBuffer;
3435
import static org.fusesource.jansi.internal.Kernel32.SetConsoleCursorPosition;
3536
import static org.fusesource.jansi.internal.Kernel32.SetConsoleTextAttribute;
@@ -54,7 +55,9 @@
5455
*/
5556
public final class WindowsAnsiPrintStream extends AnsiPrintStream { // expected diff with WindowsAnsiOutputStream.java
5657

57-
private static final long console = GetStdHandle(STD_OUTPUT_HANDLE);
58+
private static final long stdout_handle = GetStdHandle(STD_OUTPUT_HANDLE);
59+
private static final long stderr_handle = GetStdHandle(STD_ERROR_HANDLE);
60+
private final long console;
5861

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

100-
public WindowsAnsiPrintStream(PrintStream ps) throws IOException { // expected diff with WindowsAnsiOutputStream.java
103+
public WindowsAnsiPrintStream(PrintStream ps, boolean stdout) throws IOException { // expected diff with WindowsAnsiOutputStream.java
101104
super(ps); // expected diff with WindowsAnsiOutputStream.java
105+
this.console = stdout ? stdout_handle : stderr_handle;
102106
getConsoleInfo();
103107
originalColors = info.attributes;
104108
}
105109

110+
public WindowsAnsiPrintStream(PrintStream ps) throws IOException { // expected diff with WindowsAnsiOutputStream.java
111+
this(ps, true); // expected diff with WindowsAnsiOutputStream.java
112+
}
113+
106114
private void getConsoleInfo() throws IOException {
107115
ps.flush(); // expected diff with WindowsAnsiOutputStream.java
108116
if (GetConsoleScreenBufferInfo(console, info) == 0) {

0 commit comments

Comments
 (0)