Skip to content

Instantly share code, notes, and snippets.

@sabihoshi
Last active October 8, 2019 13:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sabihoshi/295d9ca7ddb104484066a05362940bc2 to your computer and use it in GitHub Desktop.
Save sabihoshi/295d9ca7ddb104484066a05362940bc2 to your computer and use it in GitHub Desktop.

Console.java

Snippet to easily invoke terminal escapes. Follow the code down here #Console.java

Install JNA

JNA

  1. In IntelliJ, go to File -> Project Structure`
  2. Click on Project Settings -> Libraries
  3. Press the + button -> From Maven...
  4. Type the following libraries and add them.
    • net.java.dev.jna:jna-platform:5.4.0
    • net.java.dev.jna:jna:5.4.0

How to Use

  1. Create a new class, name it Console.java
  2. Paste the code down below
  3. Type Console.viewColors(); in any line to see if it works.
  4. Run your code in IntelliJ or press Ctrl + F5.
  5. After running, copy the first line you see in the console. Compile
  6. Open cmd and paste the code there. It should look similar to this.
C:\Users\Kao> cd C:\Users\Kao\IdeaProjects\kt\Week 7
C:\Users\Kao\IdeaProjects\kt\Week 7> "C:\Program Files\JetBrains\IntelliJ IDEA 2019.2\jbr\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2019.2\lib\idea_rt.jar=55445:C:\Program Files\JetBrains\IntelliJ IDEA 2019.2\bin" -Dfile.encoding=UTF-8 -classpath "C:\Users\Kao\IdeaProjects\kt\Week 7\out\production\Week 7;C:\Program Files\JetBrains\IntelliJ IDEA 2019.2\plugins\Kotlin\kotlinc\lib\kotlin-stdlib.jar;C:\Program Files\JetBrains\IntelliJ IDEA 2019.2\plugins\Kotlin\kotlinc\lib\kotlin-reflect.jar;C:\Program Files\JetBrains\IntelliJ IDEA 2019.2\plugins\Kotlin\kotlinc\lib\kotlin-test.jar;C:\Program Files\JetBrains\IntelliJ IDEA 2019.2\plugins\Kotlin\kotlinc\lib\kotlin-stdlib-jdk7.jar;C:\Program Files\JetBrains\IntelliJ IDEA 2019.2\plugins\Kotlin\kotlinc\lib\kotlin-stdlib-jdk8.jar;C:\Users\Kao\.m2\repository\net\java\dev\jna\jna-platform\5.4.0\jna-platform-5.4.0.jar;C:\Users\Kao\.m2\repository\net\java\dev\jna\jna\5.4.0\jna-5.4.0.jar" com.hizamakura.kao.ScrabbleKt

gotoXY

Jumps the cursor to the x and y position.

Console.gotoXY(5, 5);

cls

Clears the entire screen.

Console.cls();

home

Moves the cursor at the top left.

Console.home();

eos

Clears everything starting from the cursor.

Console.eos();

eol

Clears text starting from cursor to the end of line.

Console.eol();

color

Sets the color of the text according to the number.

/*
    0 = Black        8 = Gray
    1 = Blue         9 = Light Blue
    2 = Green       10 = Light Green
    3 = Aqua        11 = Light Aqua
    4 = Red         12 = Light Red
    5 = Purple      13 = Light Purple
    6 = Yellow      14 = Light Yellow
    7 = White       15 = Bright White
*/
Console.color(3);

Colors

import com.sun.jna.*;
import com.sun.jna.platform.win32.WinDef.*;
import com.sun.jna.platform.win32.WinNT.HANDLE;
public class Console {
private static boolean VT_ENABLED = false;
private static String ESC = "\u001B[";
private static void vt100() {
if (VT_ENABLED) return;
if (System.getProperty("os.name").startsWith("Windows")) {
// Set output mode to handle virtual terminal sequences
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});
DWORD dwMode = p_dwMode.getValue();
int ENABLE_VIRTUAL_TERMINAL_PROCESSING = 4;
dwMode.setValue(dwMode.intValue() | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
Function SetConsoleModeFunc = Function.getFunction("kernel32", "SetConsoleMode");
SetConsoleModeFunc.invoke(BOOL.class, new Object[]{hOut, dwMode});
VT_ENABLED = true;
}
}
public static void cls() {
home();
eos();
}
public static void gotoXY(int x, int y) {
vt100();
System.out.print(ESC + y + ";" + x + "H");
}
public static void home() {
vt100();
System.out.print(ESC + "H");
}
public static void eos() {
vt100();
System.out.print(ESC + "J");
}
public static void eol() {
vt100();
System.out.print(ESC + "K");
}
public static void color(int color) {
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});
DWORD COLOR = new DWORD(color);
Function SetConsoleTextAttribute = Function.getFunction("kernel32", "SetConsoleTextAttribute");
SetConsoleTextAttribute.invoke(BOOL.class, new Object[]{hOut, COLOR});
}
public static void viewColors() {
for (int i = 0; i < 16 * 16; i++) {
color(i);
var output = i + " = " + getBackground(i) + " BG & " + getForeground(i) + " Text";
System.out.println(output);
}
}
private static String getForeground(int i) {
return getColor(i % 15);
}
private static String getBackground(int i) {
return getColor(i / 15);
}
private static String getColor(int i) {
switch (i) {
case 0: return "Black";
case 1: return "Blue";
case 2: return "Green";
case 3: return "Aqua";
case 4: return "Red";
case 5: return "Purple";
case 6: return "Yellow";
case 7: return "White";
case 8: return "Gray";
case 9: return "Light Blue";
case 10: return "Light Green";
case 11: return "Light Aqua";
case 12: return "Light Red";
case 13: return "Light Purple";
case 14: return "Light Yellow";
case 15: return "Bright White";
default: return "";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment