/
ConsoleStream.java
82 lines (73 loc) · 2.88 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
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.Log.StringOutputStream;
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 StringOutputStream {
private LineReader jline;
ConsoleStream(LineReader jline) {
this.jline = jline;
}
@Override
public void write(String s) {
try {
getWindow();
if (window != null) {
window.write(s.getBytes(StandardCharsets.UTF_8));
}
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(s.replace("\n", Ansi.ansi().a(Ansi.Attribute.RESET) + "\n"));
} else jline.getTerminal().writer().println((String) new AnsiString(s).getPlain());
if (running.get() && thread.isAlive()) {
jline.callWidget(LineReader.REDRAW_LINE);
jline.callWidget(LineReader.REDISPLAY);
}
jline.getTerminal().flush();
} 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;
}
}