Skip to content

Commit c303b8f

Browse files
Merge pull request #4 from alexarchambault/develop
Add windows-ansi-ps module
2 parents 2208f38 + cea8cf1 commit c303b8f

File tree

7 files changed

+86
-4
lines changed

7 files changed

+86
-4
lines changed

build.sbt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ lazy val shared = Def.settings(
2525
)
2626

2727

28-
lazy val core = project
28+
lazy val jni = project
2929
.settings(
3030
shared,
3131
name := "windows-ansi",
@@ -35,10 +35,10 @@ lazy val core = project
3535
)
3636
)
3737

38-
lazy val `test-cli` = project
38+
lazy val ps = project
3939
.settings(
4040
shared,
41-
skip.in(publish) := true
41+
name := "windows-ansi-ps"
4242
)
4343

4444
// root project

project/plugins.sbt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
addSbtPlugin("com.geirsson" % "sbt-ci-release" % "1.5.0")
2-
addSbtPlugin("org.xerial.sbt" % "sbt-pack" % "0.12")
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package io.github.alexarchambault.windowsansi;
2+
3+
import java.io.IOException;
4+
import java.nio.charset.StandardCharsets;
5+
import java.util.Base64;
6+
7+
public final class PowershellRunner {
8+
9+
public static void runScript(String script) throws InterruptedException, IOException {
10+
11+
String fullScript = "& {\n" +
12+
script +
13+
"\n}";
14+
15+
Base64.Encoder base64 = Base64.getEncoder();
16+
String encodedScript = base64.encodeToString(fullScript.getBytes(StandardCharsets.UTF_16LE));
17+
18+
ProcessBuilder builder = new ProcessBuilder(
19+
"powershell.exe",
20+
"-NoProfile",
21+
"-NonInteractive",
22+
"-EncodedCommand", encodedScript);
23+
builder.inheritIO();
24+
25+
int retCode = builder.start().waitFor();
26+
if (retCode != 0)
27+
throw new IOException("Error running powershell script (return code: " + retCode + ")");
28+
}
29+
30+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package io.github.alexarchambault.windowsansi;
2+
3+
import java.io.IOException;
4+
5+
public final class WindowsAnsiPs {
6+
7+
static boolean isWindows;
8+
9+
static {
10+
isWindows = System.getProperty("os.name")
11+
.toLowerCase(java.util.Locale.ROOT)
12+
.contains("windows");
13+
}
14+
15+
// adapted from https://github.com/rprichard/winpty/blob/7e59fe2d09adf0fa2aa606492e7ca98efbc5184e/misc/ConinMode.ps1
16+
static String script = "$signature = @'\n" +
17+
"[DllImport(\"kernel32.dll\", SetLastError = true)]\n" +
18+
"public static extern IntPtr GetStdHandle(int nStdHandle);\n" +
19+
"[DllImport(\"kernel32.dll\", SetLastError = true)]\n" +
20+
"public static extern uint GetConsoleMode(\n" +
21+
" IntPtr hConsoleHandle,\n" +
22+
" out uint lpMode);\n" +
23+
"[DllImport(\"kernel32.dll\", SetLastError = true)]\n" +
24+
"public static extern uint SetConsoleMode(\n" +
25+
" IntPtr hConsoleHandle,\n" +
26+
" uint dwMode);\n" +
27+
"public const int STD_OUTPUT_HANDLE = -11;\n" +
28+
"public const int ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004;\n" +
29+
"'@\n" +
30+
"\n" +
31+
"$WinAPI = Add-Type -MemberDefinition $signature `\n" +
32+
" -Name WinAPI -Namespace ConinModeScript `\n" +
33+
" -PassThru\n" +
34+
"\n" +
35+
"$handle = $WinAPI::GetStdHandle($WinAPI::STD_OUTPUT_HANDLE)\n" +
36+
"$mode = 0\n" +
37+
"$ret = $WinAPI::GetConsoleMode($handle, [ref]$mode)\n" +
38+
"if ($ret -eq 0) {\n" +
39+
" throw \"GetConsoleMode failed (is stdin a console?)\"\n" +
40+
"}\n" +
41+
"$ret = $WinAPI::SetConsoleMode($handle, $mode -bor $WinAPI::ENABLE_VIRTUAL_TERMINAL_PROCESSING)\n" +
42+
"if ($ret -eq 0) {\n" +
43+
" throw \"SetConsoleMode failed (is stdin a console?)\"\n" +
44+
"}\n";
45+
46+
// not sure what happens on Windows versions that don't support ANSI mode
47+
public static void setup() throws InterruptedException, IOException {
48+
if (isWindows) {
49+
PowershellRunner.runScript(script);
50+
}
51+
}
52+
53+
}

0 commit comments

Comments
 (0)