51
51
* which allows {@link #println(Verbosity,String)} calls which act like a logger,
52
52
* only actually printing if the verbosity level of the terminal is above
53
53
* the verbosity of the message.
54
+ * @see ConsoleTerminal
55
+ * @see SystemTerminal
54
56
*/
55
57
public abstract class Terminal {
56
58
@@ -65,35 +67,57 @@ private static PrintWriter newErrorWriter() {
65
67
return new PrintWriter (System .err );
66
68
}
67
69
68
- /** Defines the available verbosity levels of messages to be printed. */
70
+ /** Defines the available verbosity levels of messages to be printed.*/
69
71
public enum Verbosity {
70
- SILENT , /* always printed */
71
- NORMAL , /* printed when no options are given to cli */
72
- VERBOSE /* printed only when cli is passed verbose option */
72
+ /** always printed */
73
+ SILENT ,
74
+ /** printed when no options are given to cli */
75
+ NORMAL ,
76
+ /** printed only when cli is passed verbose option */
77
+ VERBOSE
73
78
}
74
79
75
80
/** The current verbosity for the terminal, defaulting to {@link Verbosity#NORMAL}. */
76
81
private Verbosity verbosity = Verbosity .NORMAL ;
77
82
78
- /** The newline used when calling println. */
83
+ /** The newline separator used when calling println. */
79
84
private final String lineSeparator ;
80
85
86
+ /** Constructs a new terminal with the given line separator.
87
+ * @param lineSeparator the line separator to use when calling println
88
+ * */
81
89
protected Terminal (String lineSeparator ) {
82
90
this .lineSeparator = lineSeparator ;
83
91
}
84
92
85
- /** Sets the verbosity of the terminal. */
93
+ /** Sets the {@link Terminal#verbosity} of the terminal. (Default is {@link Verbosity#NORMAL})
94
+ * @param verbosity the {@link Verbosity} level that will be used for printing
95
+ * */
86
96
public void setVerbosity (Verbosity verbosity ) {
87
97
this .verbosity = verbosity ;
88
98
}
89
99
90
- /** Reads clear text from the terminal input. See {@link Console#readLine()}. */
100
+ /** Reads clear text from the terminal input.
101
+ * @see Console#readLine()
102
+ * @param prompt message to display to the user
103
+ * @return the text entered by the user
104
+ * */
91
105
public abstract String readText (String prompt );
92
106
93
- /** Reads password text from the terminal input. See {@link Console#readPassword()}}. */
107
+ /** Reads secret text from the terminal input with echoing disabled.
108
+ * @see Console#readPassword()
109
+ * @param prompt message to display to the user
110
+ * @return the secret as a character array
111
+ * */
94
112
public abstract char [] readSecret (String prompt );
95
113
96
- /** Read password text form terminal input up to a maximum length. */
114
+ /** Read secret text from terminal input with echoing disabled, up to a maximum length.
115
+ * @see Console#readPassword()
116
+ * @param prompt message to display to the user
117
+ * @param maxLength the maximum length of the secret
118
+ * @return the secret as a character array
119
+ * @throws IllegalStateException if the secret exceeds the maximum length
120
+ * */
97
121
public char [] readSecret (String prompt , int maxLength ) {
98
122
char [] result = readSecret (prompt );
99
123
if (result .length > maxLength ) {
@@ -103,30 +127,48 @@ public char[] readSecret(String prompt, int maxLength) {
103
127
return result ;
104
128
}
105
129
106
- /** Returns a Writer which can be used to write to the terminal directly using standard output. */
130
+ /** Returns a Writer which can be used to write to the terminal directly using standard output.
131
+ * @return a writer to {@link Terminal#DEFAULT} output
132
+ * @see Terminal.ConsoleTerminal
133
+ * @see Terminal.SystemTerminal
134
+ * */
107
135
public abstract PrintWriter getWriter ();
108
136
109
- /** Returns a Writer which can be used to write to the terminal directly using standard error. */
137
+ /** Returns a Writer which can be used to write to the terminal directly using standard error.
138
+ * @return a writer to stderr
139
+ * */
110
140
public PrintWriter getErrorWriter () {
111
141
return ERROR_WRITER ;
112
142
}
113
143
114
- /** Prints a line to the terminal at {@link Verbosity#NORMAL} verbosity level. */
144
+ /** Prints a line to the terminal at {@link Verbosity#NORMAL} verbosity level, with a {@link Terminal#lineSeparator}
145
+ * @param msg the message to print
146
+ * */
115
147
public final void println (String msg ) {
116
148
println (Verbosity .NORMAL , msg );
117
149
}
118
150
119
- /** Prints a line to the terminal at {@code verbosity} level. */
151
+ /** Prints message to the terminal's standard output at {@link Verbosity} level, with a {@link Terminal#lineSeparator}.
152
+ * @param verbosity the {@link Verbosity} level at which to print
153
+ * @param msg the message to print
154
+ * */
120
155
public final void println (Verbosity verbosity , String msg ) {
121
156
print (verbosity , msg + lineSeparator );
122
157
}
123
158
124
- /** Prints message to the terminal's standard output at {@code verbosity} level, without a newline. */
159
+ /** Prints message to the terminal's standard output at {@link Verbosity} level, without adding a {@link Terminal#lineSeparator}.
160
+ * @param verbosity the {@link Verbosity} level at which to print
161
+ * @param msg the message to print
162
+ * */
125
163
public final void print (Verbosity verbosity , String msg ) {
126
164
print (verbosity , msg , false );
127
165
}
128
166
129
- /** Prints message to the terminal at {@code verbosity} level, without a newline. */
167
+ /** Prints message to either standard or error output at {@link Verbosity} level, without adding a {@link Terminal#lineSeparator}.
168
+ * @param verbosity the {@link Verbosity} level at which to print.
169
+ * @param msg the message to print
170
+ * @param isError if true, prints to standard error instead of standard output
171
+ * */
130
172
private void print (Verbosity verbosity , String msg , boolean isError ) {
131
173
if (isPrintable (verbosity )) {
132
174
PrintWriter writer = isError ? getErrorWriter () : getWriter ();
@@ -135,29 +177,44 @@ private void print(Verbosity verbosity, String msg, boolean isError) {
135
177
}
136
178
}
137
179
138
- /** Prints a line to the terminal's standard error at {@link Verbosity#NORMAL} verbosity level, without a newline. */
180
+ /** Prints a line to the terminal's standard error at {@link Verbosity} level, without adding a {@link Terminal#lineSeparator}.
181
+ * @param verbosity the {@link Verbosity} level at which to print.
182
+ * @param msg the message to print
183
+ * */
139
184
public final void errorPrint (Verbosity verbosity , String msg ) {
140
185
print (verbosity , msg , true );
141
186
}
142
187
143
- /** Prints a line to the terminal's standard error at {@link Verbosity#NORMAL} verbosity level. */
188
+ /** Prints a line to the terminal's standard error at {@link Verbosity#NORMAL} verbosity level, with a {@link Terminal#lineSeparator}
189
+ * @param msg the message to print
190
+ * */
144
191
public final void errorPrintln (String msg ) {
145
192
errorPrintln (Verbosity .NORMAL , msg );
146
193
}
147
194
148
- /** Prints a line to the terminal's standard error at {@code verbosity} level. */
195
+ /** Prints a line to the terminal's standard error at {@link Verbosity} level, with a {@link Terminal#lineSeparator}.
196
+ * @param verbosity the {@link Verbosity} level at which to print.
197
+ * @param msg the message to print
198
+ * */
149
199
public final void errorPrintln (Verbosity verbosity , String msg ) {
150
200
errorPrint (verbosity , msg + lineSeparator );
151
201
}
152
202
153
- /** Checks if is enough {@code verbosity} level to be printed */
203
+ /** Checks if given {@link Verbosity} level is high enough to be printed at the level defined by {@link Terminal#verbosity}
204
+ * @param verbosity the {@link Verbosity} level to check
205
+ * @return true if the {@link Verbosity} level is high enough to be printed
206
+ * @see Terminal#setVerbosity(Verbosity)
207
+ * */
154
208
public final boolean isPrintable (Verbosity verbosity ) {
155
209
return this .verbosity .ordinal () >= verbosity .ordinal ();
156
210
}
157
211
158
212
/**
159
- * Prompt for a yes or no answer from the user. This method will loop until 'y' or 'n'
213
+ * Prompt for a yes or no answer from the user. This method will loop until 'y', 'n'
160
214
* (or the default empty value) is entered.
215
+ * @param prompt the prompt to display to the user
216
+ * @param defaultYes if true, the default answer is 'y', otherwise it is 'n'
217
+ * @return true if the user answered 'y', false if the user answered 'n' or the defaultYes value if the user entered nothing
161
218
*/
162
219
public final boolean promptYesNo (String prompt , boolean defaultYes ) {
163
220
String answerPrompt = defaultYes ? " [Y/n]" : " [y/N]" ;
@@ -181,6 +238,11 @@ public final boolean promptYesNo(String prompt, boolean defaultYes) {
181
238
* character is immediately preceded by a carriage return, we have
182
239
* a Windows-style newline, so we discard the carriage return as well
183
240
* as the newline.
241
+ * @param reader the reader to read from
242
+ * @param maxLength the maximum length of the line to read
243
+ * @return the line read from the reader
244
+ * @throws RuntimeException if the line read exceeds the maximum length
245
+ * @throws RuntimeException if an IOException occurs while reading
184
246
*/
185
247
public static char [] readLineToCharArray (Reader reader , int maxLength ) {
186
248
char [] buf = new char [maxLength + 2 ];
@@ -215,6 +277,7 @@ public static char[] readLineToCharArray(Reader reader, int maxLength) {
215
277
}
216
278
}
217
279
280
+ /** Flushes the terminal's standard output and standard error. */
218
281
public void flush () {
219
282
this .getWriter ().flush ();
220
283
this .getErrorWriter ().flush ();
0 commit comments