-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b1d63a9
commit 6a4f810
Showing
15 changed files
with
533 additions
and
221 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ out/ | |
.bsp/ | ||
.metals/ | ||
.vscode/ | ||
.idea/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 0 additions & 24 deletions
24
jni-graalvm/src/io/github/alexarchambault/windowsansi/WindowsAnsiSubstitutions.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
native-graalvm/src/io/github/alexarchambault/windowsansi/Placeholder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package io.github.alexarchambault.windowsansi; | ||
|
||
/** | ||
* Empty public class to make javadoc happy | ||
*/ | ||
public final class Placeholder { | ||
private Placeholder() {} | ||
} |
54 changes: 54 additions & 0 deletions
54
native/src/io/github/alexarchambault/nativeterm/NativeTerminal.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package io.github.alexarchambault.nativeterm; | ||
|
||
import io.github.alexarchambault.isterminal.IsTerminal; | ||
import io.github.alexarchambault.nativeterm.internal.UnixTerm; | ||
import io.github.alexarchambault.nativeterm.internal.WindowsTerm; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* Use these methods to query the terminal size and enable ANSI output in it | ||
*/ | ||
public final class NativeTerminal { | ||
private NativeTerminal() {} | ||
|
||
final static boolean isWindows; | ||
|
||
static { | ||
isWindows = System.getProperty("os.name") | ||
.toLowerCase(java.util.Locale.ROOT) | ||
.contains("windows"); | ||
} | ||
|
||
/** | ||
* Gets the terminal size | ||
* | ||
* This uses an {@code ioctl} call on Linux / Mac, and a {@code kernel32.dll} method | ||
* on Windows. Both are done via JNI, using libraries that ship with jansi. | ||
* | ||
* @return the terminal size | ||
*/ | ||
public static TerminalSize getSize() { | ||
if (isWindows) | ||
return WindowsTerm.getSize(); | ||
return UnixTerm.getSize(true); | ||
} | ||
|
||
/** | ||
* Enables ANSI terminal output (only needed on Windows) | ||
* | ||
* It is safe to call this method on non-Windows systems. It simply | ||
* returns true in that case. | ||
* | ||
* Under-the-hood, this calls a {@code kernel32.dll} method, via JNI, | ||
* using libraries that ship with jansi. | ||
* | ||
* @throws IOException if anything goes wrong | ||
* @return Whether ANSI output is enabled | ||
*/ | ||
public static boolean setupAnsi() throws IOException { | ||
if (isWindows && IsTerminal.isTerminal()) | ||
return WindowsTerm.setupAnsi(); | ||
return true; | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
native/src/io/github/alexarchambault/nativeterm/NativeTerminalFallbacks.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package io.github.alexarchambault.nativeterm; | ||
|
||
import io.github.alexarchambault.isterminal.IsTerminal; | ||
import io.github.alexarchambault.nativeterm.internal.ScriptRunner; | ||
import io.github.alexarchambault.nativeterm.internal.WindowsTermScripts; | ||
|
||
import java.io.IOException; | ||
|
||
import static io.github.alexarchambault.nativeterm.NativeTerminal.isWindows; | ||
|
||
/** | ||
* Methods to query the terminal size and enable ANSI output in it like in {@link NativeTerminal} | ||
*/ | ||
public final class NativeTerminalFallbacks { | ||
|
||
private NativeTerminalFallbacks() {} | ||
|
||
/** | ||
* Gets the terminal size | ||
* | ||
* This runs {@code tput} via an external process on Linux / Mac, and runs | ||
* a PowerShell script that calls {@code kernel32.dll} methods on Windows | ||
* | ||
* @throws InterruptedException if the PowerShell sub-process gets interrupted | ||
* @throws IOException if anything goes wrong | ||
* @return the terminal size | ||
*/ | ||
public static TerminalSize getSize() throws InterruptedException, IOException { | ||
if (!IsTerminal.isTerminal()) | ||
throw new IllegalArgumentException("Cannot get terminal size without a terminal"); | ||
if (isWindows) { | ||
String output = ScriptRunner.runPowerShellScript(WindowsTermScripts.getConsoleDimScript).trim(); | ||
String[] lines = output.split("\\r?\\n"); | ||
String lastLine = lines[lines.length - 1]; | ||
if (lastLine.startsWith("Error:")) | ||
throw new IOException(lastLine); | ||
if (lastLine.startsWith("Size: ")) { | ||
lastLine = lastLine.substring("Size: ".length()); | ||
String[] split = lastLine.split("\\s+"); | ||
return new TerminalSize(Integer.parseInt(split[0]), Integer.parseInt(split[1])); | ||
} | ||
throw new IOException("Invalid output from PowerShell script that gets terminal size: '" + output + "'"); | ||
} | ||
else { | ||
return new TerminalSize(ScriptRunner.runTput("cols"), ScriptRunner.runTput("lines")); | ||
} | ||
} | ||
|
||
/** | ||
* Enables ANSI terminal output (only needed on Windows) | ||
* | ||
* It is safe to call this method on non-Windows systems. It simply | ||
* returns true in that case. | ||
* | ||
* Under-the-hood, this calls a {@code kernel32.dll} method, via a PowerShell script, | ||
* run in an external process. | ||
* | ||
* @throws IOException if anything goes wrong | ||
* @throws InterruptedException if the PowerShell sub-process gets interrupted | ||
* @return Whether ANSI output is enabled | ||
*/ | ||
public static boolean setupAnsi() throws InterruptedException, IOException { | ||
if (isWindows && IsTerminal.isTerminal()) { | ||
String output = ScriptRunner.runPowerShellScript(WindowsTermScripts.enableAnsiScript); | ||
String[] lines = output.split("\\r?\\n"); | ||
String lastLine = lines[lines.length - 1]; | ||
return Boolean.parseBoolean(lastLine); | ||
} | ||
return true; | ||
} | ||
} |
Oops, something went wrong.