/
ConsoleStream.java
91 lines (82 loc) · 3.28 KB
/
ConsoleStream.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
package net.ME1312.Galaxi.Engine.Library.Log;
import net.ME1312.Galaxi.Engine.GalaxiEngine;
import net.ME1312.Galaxi.Engine.Library.ConsoleReader;
import net.ME1312.Galaxi.Library.Container;
import net.ME1312.Galaxi.Library.Util;
import org.fusesource.jansi.Ansi;
import org.fusesource.jansi.AnsiString;
import org.jline.reader.LineReader;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.nio.charset.StandardCharsets;
import static net.ME1312.Galaxi.Engine.GalaxiOption.USE_ANSI;
/**
* Console Log Stream Class
*/
public class ConsoleStream extends OutputStream {
private LineReader jline;
private ByteArrayOutputStream buffer;
ConsoleStream(LineReader jline) {
this.jline = jline;
this.buffer = new ByteArrayOutputStream();
}
@Override
public void write(int i) {
try {
if (i == '\n') {
byte[] buffer = this.buffer.toByteArray();
this.buffer.reset();
getWindow();
if (window != null) {
window.write(buffer);
window.write(i);
}
getThread();
Container<Boolean> running = Util.reflect(GalaxiEngine.class.getDeclaredField("running"), GalaxiEngine.getInstance());
if (running.get() && thread.isAlive()) jline.callWidget(LineReader.CLEAR);
if (USE_ANSI.def()) {
jline.getTerminal().writer().print(new String(buffer, StandardCharsets.UTF_8));
jline.getTerminal().writer().println(Ansi.ansi().a(Ansi.Attribute.RESET).toString());
} else jline.getTerminal().writer().println((String) new AnsiString(new String(buffer, StandardCharsets.UTF_8)).getPlain());
if (running.get() && thread.isAlive()) {
jline.callWidget(LineReader.REDRAW_LINE);
jline.callWidget(LineReader.REDISPLAY);
}
jline.getTerminal().flush();
} else {
buffer.write(i);
}
} catch (Exception e) {}
}
private static Thread thread;
private static Thread getThread() {
if (thread == null) {
ConsoleReader reader = GalaxiEngine.getInstance().getConsoleReader();
if (reader != null) try {
Field f = ConsoleReader.class.getDeclaredField("thread");
f.setAccessible(true);
if (f.get(reader) != null) {
thread = (Thread) f.get(reader);
}
f.setAccessible(false);
} catch (Exception e) {}
}
return thread;
}
private static OutputStream window;
private static OutputStream getWindow() {
if (window == null || Util.isException(window::flush)) {
ConsoleReader reader = GalaxiEngine.getInstance().getConsoleReader();
if (reader != null) try {
Field f = ConsoleReader.class.getDeclaredField("window");
f.setAccessible(true);
if (f.get(reader) != null) {
window = (OutputStream) f.get(reader);
}
f.setAccessible(false);
} catch (Exception e) {}
}
return window;
}
}