-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUI.java
65 lines (53 loc) · 1.72 KB
/
UI.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
import java.io.Console;
import java.util.Scanner;
class UI {
private Console console;
private boolean legacyMode = true;
private Scanner legacyScanner;
UI() {
console = System.console();
if (console == null) {
Log("WARNING: Failed to init console - fall-back to standard in-/output (i.e. among others things your password won't be hidden when typing on the prompt - be careful)\n");
legacyScanner = new Scanner(System.in);
} else {
// init off console worked
legacyMode = false;
}
}
void Log(String msg) {
Log(msg, true);
}
void Log(String msg, boolean newLine) {
if (legacyMode) {
System.out.print(msg + (newLine ? "\n" : ""));
} else {
console.writer().print(msg + (newLine ? "\n" : ""));
console.flush();
}
}
String getLine(String query) {
return getLine(query, null, false);
}
String getLine(String query, boolean isPassword) {
return getLine(query, null, isPassword);
}
String getLine(String query, String defaultValue, boolean isPassword) {
StringBuilder input = new StringBuilder();
if (defaultValue != null) {
Log(query + "[" + defaultValue + "] > ", false);
} else {
Log(query + " > ", false);
}
if (isPassword && !legacyMode) {
for (char c : console.readPassword()) {
input.append(c);
}
} else if (!legacyMode) {
input.append(console.readLine());
} else {
// legacy mode
input.append(legacyScanner.nextLine());
}
return input.toString();
}
}