-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathConsole.java
139 lines (109 loc) · 3.82 KB
/
Console.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package project.ing.soft.cli;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.ptr.IntByReference;
import com.sun.jna.win32.StdCallLibrary;
import java.io.PrintStream;
import java.util.Arrays;
/**
* This class handles output for the ConsoleUI.
* Special handling for this is needed as UTF-8 output in Windows is unreliable
* due to a broken unicode page.
*
* @author Håvard Slettvold, D.Parravicini
*/
public class Console extends PrintStream {
private Kernel32 instance = null;
private Pointer handle;
/**
* Console
* @param defaultOut as back-up printStream if the attempt to print using
* kernel32.dll endpoints fails
*/
public Console(PrintStream defaultOut) {
super(defaultOut);
String os = System.getProperty("os.name").toLowerCase();
if (os.startsWith("win")) {
instance = Native.loadLibrary("kernel32", Kernel32.class);
//in order to select UTF-8 page https://docs.microsoft.com/en-us/windows/desktop/intl/code-page-identifiers
instance.SetConsoleCP(65001);
handle = instance.GetStdHandle(-11);
//enable virtual terminal output processing
instance.SetConsoleMode(handle, 7);
}
}
/**
* Interface to call method from kernel32.dll
*/
public interface Kernel32 extends StdCallLibrary {
Pointer GetStdHandle(int nStdHandle);
boolean WriteConsoleW(Pointer hConsoleOutput, char[] lpBuffer, int nNumberOfCharsToWrite,
IntByReference lpNumberOfCharsWritten, Pointer lpReserved);
boolean SetConsoleCP(long wCodePageID);
boolean SetConsoleMode(Pointer hConsoleHandle, int dwMode);
}
/**
* main print method
* @param message to be printed. It tries to print using Kernel32.dll
* if an error is raised it uses the default print stream
* passed as an argument to {@link Console}
*/
@Override
public void print(String message) {
if (!attemptWindowsPrint(message)) {
super.print(message);
}
}
@Override
public void print(Object obj) {
print(obj.toString());
}
@Override
public void println(String message) {
print(message.concat(System.getProperty("line.separator")));
}
@Override
public void println(Object obj) {
println(obj.toString());
}
/**
* for reference on ansi escape sequence http://ascii-table.com/ansi-escape-sequences-vt-100.php
*/
public void clear(){
println("\u001B[2J");
}
public void saveCursorPosition(){
print("\u001B[s");
}
public void restoreCursorPosition(){
print("\u001B[u");
}
public void setCursorPosition(int row, int col){
print(String.format("\u001B[%d;%dH", row, col));
}
/**
* Attempts to print text to the Windows console.
* @param message Message to print
* @return True if text was correctly printed to a windows console
*/
private synchronized boolean attemptWindowsPrint(String message) {
boolean successful = false;
if (instance != null) {
char[] buffer = message.toCharArray();
IntByReference lpNumberOfCharsWritten = new IntByReference();
successful = instance.WriteConsoleW(handle, buffer, buffer.length, lpNumberOfCharsWritten, null);
}
return successful;
}
/**
* Main to test utf-8 capabilities
* @param args used as a print arguments
*/
public static void main(String[] args){
Console c = new Console(System.out);
c.println(Arrays.toString(args));
c.println( "⚀");
c.println("\u001B[31mit's me mario\u001B[0m");
c.print("java èéøÞǽлљΣæča");
}
}