Skip to content

Commit

Permalink
fix: if Win10, then enable Ansi support
Browse files Browse the repository at this point in the history
  • Loading branch information
dialex committed Dec 30, 2019
1 parent 094a305 commit baa3b95
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
12 changes: 12 additions & 0 deletions pom.xml
Expand Up @@ -151,6 +151,18 @@
</build>

<dependencies>
<!-- Necessary for Windows 10 Ansi support -->
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<version>5.5.0</version>
</dependency>
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna-platform</artifactId>
<version>5.5.0</version>
</dependency>
<!-- Dependencies of test framework -->
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/com/diogonunes/jcdp/color/ColoredPrinter.java
Expand Up @@ -7,6 +7,11 @@
import com.diogonunes.jcdp.color.api.Ansi.BColor;
import com.diogonunes.jcdp.color.api.Ansi.FColor;
import com.diogonunes.jcdp.color.api.IColoredPrinter;
import com.sun.jna.Function;
import com.sun.jna.platform.win32.WinDef.BOOL;
import com.sun.jna.platform.win32.WinDef.DWORD;
import com.sun.jna.platform.win32.WinDef.DWORDByReference;
import com.sun.jna.platform.win32.WinNT.HANDLE;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
Expand Down Expand Up @@ -41,6 +46,9 @@ public ColoredPrinter(Builder builder) {
setAttribute(builder._attribute);
setForegroundColor(builder._foregroundColor);
setBackgroundColor(builder._backgroundColor);

if (System.getProperty("os.name").startsWith("Windows"))
enableWindows10AnsiSupport();
}

// =========
Expand Down Expand Up @@ -313,4 +321,27 @@ private void formattedPrint(Object msg, String ansiFormatCode, boolean appendNew
String formattedMsg = Ansi.formatMessage(output.toString(), ansiFormatCode);
System.out.print(formattedMsg);
}

/* Windows 10 supports Ansi codes. However, it's still experimental and not enabled by default.
* This method enables the necessary Windows 10 feature.
*
* More info: https://stackoverflow.com/a/51681675/675577
* Code source: https://stackoverflow.com/a/52767586/675577
* Reported issue: https://github.com/PowerShell/PowerShell/issues/11449#issuecomment-569531747
*/
private void enableWindows10AnsiSupport() {
Function GetStdHandleFunc = Function.getFunction("kernel32", "GetStdHandle");
DWORD STD_OUTPUT_HANDLE = new DWORD(-11);
HANDLE hOut = (HANDLE) GetStdHandleFunc.invoke(HANDLE.class, new Object[]{STD_OUTPUT_HANDLE});

DWORDByReference p_dwMode = new DWORDByReference(new DWORD(0));
Function GetConsoleModeFunc = Function.getFunction("kernel32", "GetConsoleMode");
GetConsoleModeFunc.invoke(BOOL.class, new Object[]{hOut, p_dwMode});

int ENABLE_VIRTUAL_TERMINAL_PROCESSING = 4;
DWORD dwMode = p_dwMode.getValue();
dwMode.setValue(dwMode.intValue() | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
Function SetConsoleModeFunc = Function.getFunction("kernel32", "SetConsoleMode");
SetConsoleModeFunc.invoke(BOOL.class, new Object[]{hOut, dwMode});
}
}

0 comments on commit baa3b95

Please sign in to comment.