-
Notifications
You must be signed in to change notification settings - Fork 39
/
Prompter.java
69 lines (60 loc) · 2.41 KB
/
Prompter.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
package net.rubygrapefruit.platform.prompts;
import net.rubygrapefruit.platform.terminal.TerminalOutput;
import net.rubygrapefruit.platform.terminal.Terminals;
import java.util.List;
/**
* Displays prompts on the terminal to ask the user various kinds of questions.
*/
public class Prompter {
static final TerminalOutput.Color SELECTION_COLOR = TerminalOutput.Color.Cyan;
private final AbstractPrompter implementation;
public Prompter(Terminals terminals) {
if (!terminals.isTerminalInput() || !terminals.isTerminal(Terminals.Output.Stdout)) {
implementation = new NonInteractivePrompter();
} else {
if (terminals.getTerminal(Terminals.Output.Stdout).supportsCursorMotion() && terminals.getTerminalInput().supportsRawMode()) {
implementation = new InteractivePrompter(terminals);
} else {
implementation = new PlainPrompter(terminals);
}
}
}
/**
* Returns {@code true} if this prompter can ask the user questions.
*/
public boolean isInteractive() {
return implementation.isInteractive();
}
/**
* Asks the user to select an option from a list.
*
* @return The index of the selected option or {@code null} on end of input. Returns the default option when not interactive.
*/
public Integer select(String prompt, List<String> options, int defaultOption) {
return implementation.select(prompt, options, defaultOption);
}
/**
* Asks the user to enter some text.
*
* @return The text or {@code null} on end of input. Returns the default value when not interactive.
*/
public String enterText(String prompt, String defaultValue) {
return implementation.enterText(prompt, defaultValue);
}
/**
* Asks the user to enter a password.
*
* @return The password or {@code null} on end of input or when not interactive or when not possible to prompt for a password without echoing the characters.
*/
public String enterPassword(String prompt) {
return implementation.enterPassword(prompt);
}
/**
* Asks the user a yes/no question.
*
* @return The selected value or null on end of input. Returns the default value when not interactive.
*/
public Boolean askYesNo(String prompt, boolean defaultValue) {
return implementation.askYesNo(prompt, defaultValue);
}
}