1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.jline;
20
21 import java.util.Iterator;
22 import java.util.List;
23
24 import org.apache.maven.api.annotations.Experimental;
25 import org.apache.maven.api.di.Named;
26 import org.apache.maven.api.di.Singleton;
27 import org.apache.maven.api.services.Prompter;
28 import org.apache.maven.api.services.PrompterException;
29
30 @Experimental
31 @Named
32 @Singleton
33 public class DefaultPrompter implements Prompter {
34
35 @Override
36 public String prompt(String message, List<String> possibleValues, String defaultReply) throws PrompterException {
37 return doPrompt(message, possibleValues, defaultReply, false);
38 }
39
40 @Override
41 public String promptForPassword(String message) throws PrompterException {
42 return doPrompt(message, null, null, true);
43 }
44
45 @Override
46 public void showMessage(String message) throws PrompterException {
47 doDisplay(message);
48 }
49
50 private String doPrompt(String message, boolean password) {
51 try {
52 if (message != null) {
53 if (!message.endsWith("\n")) {
54 if (message.endsWith(":")) {
55 message += " ";
56 } else if (!message.endsWith(": ")) {
57 message += ": ";
58 }
59 }
60 int lastNl = message.lastIndexOf('\n');
61 String begin = message.substring(0, lastNl + 1);
62 message = message.substring(lastNl + 1);
63 MessageUtils.terminal.writer().print(begin);
64 MessageUtils.terminal.flush();
65 }
66 return MessageUtils.reader.readLine(message, password ? '*' : null);
67 } catch (Exception e) {
68 throw new PrompterException("Unable to prompt user", e);
69 }
70 }
71
72 private void doDisplay(String message) {
73 try {
74 MessageUtils.terminal.writer().print(message);
75 MessageUtils.terminal.flush();
76 } catch (Exception e) {
77 throw new PrompterException("Unable to display message", e);
78 }
79 }
80
81 private String doPrompt(String message, List<?> possibleValues, String defaultReply, boolean password) {
82 String formattedMessage = formatMessage(message, possibleValues, defaultReply);
83 String line;
84 do {
85 line = doPrompt(formattedMessage, password);
86 if (line == null && defaultReply == null) {
87 throw new PrompterException("EOF");
88 }
89 if (line == null || line.isEmpty()) {
90 line = defaultReply;
91 }
92 if (line != null && (possibleValues != null && !possibleValues.contains(line))) {
93 doDisplay("Invalid selection.\n");
94 }
95 } while (line == null || (possibleValues != null && !possibleValues.contains(line)));
96 return line;
97 }
98
99 private String formatMessage(String message, List<?> possibleValues, String defaultReply) {
100 if (message == null) {
101 return null;
102 }
103 StringBuilder formatted = new StringBuilder(message.length() * 2);
104 formatted.append(message);
105 if (possibleValues != null && !possibleValues.isEmpty()) {
106 formatted.append(" (");
107 for (Iterator<?> it = possibleValues.iterator(); it.hasNext(); ) {
108 String possibleValue = String.valueOf(it.next());
109 formatted.append(possibleValue);
110 if (it.hasNext()) {
111 formatted.append('/');
112 }
113 }
114 formatted.append(')');
115 }
116 if (defaultReply != null) {
117 formatted.append(' ').append(defaultReply).append(": ");
118 }
119 return formatted.toString();
120 }
121 }