-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
BasicConsole.java
182 lines (160 loc) · 5.23 KB
/
BasicConsole.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package io.quarkus.dev.console;
import java.io.Console;
import java.io.IOException;
import java.io.PrintStream;
import java.nio.charset.StandardCharsets;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.logging.Level;
import java.util.logging.Logger;
public class BasicConsole extends QuarkusConsole {
private static final Logger log = Logger.getLogger(BasicConsole.class.getName());
private static final ThreadLocal<Boolean> DISABLE_FILTER = new ThreadLocal<>() {
@Override
protected Boolean initialValue() {
return false;
}
};
final Consumer<String> output;
final Supplier<Integer> input;
final boolean inputSupport;
final boolean color;
volatile boolean readingLine;
public BasicConsole(boolean color, boolean inputSupport, PrintStream printStream, Console console) {
this(color, inputSupport, (s) -> {
if (console != null) {
console.writer().print(s);
console.writer().flush();
} else {
printStream.print(s);
}
}, () -> {
try {
return System.in.read();
} catch (IOException e) {
throw new RuntimeException(e);
}
});
}
public BasicConsole(boolean color, boolean inputSupport, Consumer<String> output) {
this(color, inputSupport, output, () -> {
try {
return System.in.read();
} catch (IOException e) {
throw new RuntimeException(e);
}
});
}
public BasicConsole(boolean color, boolean inputSupport, Consumer<String> output, Supplier<Integer> inputProvider) {
this.color = color;
this.inputSupport = inputSupport;
this.output = output;
this.input = inputProvider;
if (inputSupport) {
Objects.requireNonNull(inputProvider);
Thread t = new Thread(new Runnable() {
@Override
public void run() {
while (true) {
try {
int val = input.get();
if (val == -1) {
return;
}
if (readingLine) {
//when doing a read line we want to discard the first \n
//as this was the one that was needed to activate this mode
if (val == '\n' || val == '\r') {
readingLine = false;
continue;
}
}
var handler = inputHandler;
if (handler != null) {
handler.accept(new int[] { val });
}
} catch (Exception e) {
log.log(Level.SEVERE, "Failed to read user input", e);
return;
}
}
}
}, "Quarkus Terminal Reader");
t.setDaemon(true);
t.start();
}
}
@Override
public void doReadLine() {
readingLine = true;
output.accept(">");
}
public StatusLine registerStatusLine(int priority) {
return new StatusLine() {
boolean closed;
String old;
@Override
public void setMessage(String message) {
if (closed) {
return;
}
if (message == null) {
old = null;
return;
}
if (message.equals(old)) {
return;
}
old = message;
DISABLE_FILTER.set(true);
try {
System.out.println(message);
} finally {
DISABLE_FILTER.set(false);
}
}
@Override
public void close() {
closed = true;
}
};
}
@Override
public void setPromptMessage(String prompt) {
if (!inputSupport) {
return;
}
if (prompt == null) {
return;
}
DISABLE_FILTER.set(true);
try {
System.out.println(prompt);
} finally {
DISABLE_FILTER.set(false);
}
}
@Override
public void write(boolean errorStream, String s) {
if (!shouldWrite(errorStream, s)) {
//we still test, the output filter may be recording output
if (!DISABLE_FILTER.get()) {
return;
}
}
if (!color) {
output.accept(stripAnsiCodes(s));
} else {
output.accept(s);
}
}
@Override
public void write(boolean errorStream, byte[] buf, int off, int len) {
write(errorStream, new String(buf, off, len, StandardCharsets.UTF_8));
}
@Override
public boolean isInputSupported() {
return inputSupport;
}
}