Package org.opensearch.cli
Class Terminal
java.lang.Object
org.opensearch.cli.Terminal
A Terminal wraps access to reading input and writing output for a cli.
The available methods are similar to those of Console, with the ability
to read either normal text or a password, and the ability to print a line
of text. Printing is also gated by the Terminal.Verbosity of the terminal,
which allows println(Verbosity,String) calls which act like a logger,
only actually printing if the verbosity level of the terminal is above
the verbosity of the message.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumDefines the available verbosity levels of messages to be printed. -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfinal voiderrorPrint(Terminal.Verbosity verbosity, String msg) Prints a line to the terminal's standard error atTerminal.Verbositylevel, without adding alineSeparator.final voiderrorPrintln(String msg) Prints a line to the terminal's standard error atTerminal.Verbosity.NORMALverbosity level, with alineSeparatorfinal voiderrorPrintln(Terminal.Verbosity verbosity, String msg) Prints a line to the terminal's standard error atTerminal.Verbositylevel, with alineSeparator.voidflush()Flushes the terminal's standard output and standard error.Returns a Writer which can be used to write to the terminal directly using standard error.abstract PrintWriterReturns a Writer which can be used to write to the terminal directly using standard output.final booleanisPrintable(Terminal.Verbosity verbosity) Checks if givenTerminal.Verbositylevel is high enough to be printed at the level defined byverbosityfinal voidprint(Terminal.Verbosity verbosity, String msg) Prints message to the terminal's standard output atTerminal.Verbositylevel, without adding alineSeparator.final voidPrints a line to the terminal atTerminal.Verbosity.NORMALverbosity level, with alineSeparatorfinal voidprintln(Terminal.Verbosity verbosity, String msg) Prints message to the terminal's standard output atTerminal.Verbositylevel, with alineSeparator.final booleanpromptYesNo(String prompt, boolean defaultYes) Prompt for a yes or no answer from the user.static char[]readLineToCharArray(Reader reader, int maxLength) Read from the reader until we find a newline.abstract char[]readSecret(String prompt) Reads secret text from the terminal input with echoing disabled.char[]readSecret(String prompt, int maxLength) Read secret text from terminal input with echoing disabled, up to a maximum length.abstract StringReads clear text from the terminal input.voidsetVerbosity(Terminal.Verbosity verbosity) Sets theverbosityof the terminal.
-
Field Details
-
DEFAULT
The default terminal implementation, which will be a console if available, or stdout/stderr if not.
-
-
Constructor Details
-
Terminal
Constructs a new terminal with the given line separator.- Parameters:
lineSeparator- the line separator to use when calling println
-
-
Method Details
-
setVerbosity
Sets theverbosityof the terminal. (Default isTerminal.Verbosity.NORMAL)- Parameters:
verbosity- theTerminal.Verbositylevel that will be used for printing
-
readText
Reads clear text from the terminal input.- Parameters:
prompt- message to display to the user- Returns:
- the text entered by the user
- See Also:
-
readSecret
Reads secret text from the terminal input with echoing disabled.- Parameters:
prompt- message to display to the user- Returns:
- the secret as a character array
- See Also:
-
readSecret
Read secret text from terminal input with echoing disabled, up to a maximum length.- Parameters:
prompt- message to display to the usermaxLength- the maximum length of the secret- Returns:
- the secret as a character array
- Throws:
IllegalStateException- if the secret exceeds the maximum length- See Also:
-
getWriter
Returns a Writer which can be used to write to the terminal directly using standard output.- Returns:
- a writer to
DEFAULToutput - See Also:
-
getErrorWriter
Returns a Writer which can be used to write to the terminal directly using standard error.- Returns:
- a writer to stderr
-
println
Prints a line to the terminal atTerminal.Verbosity.NORMALverbosity level, with alineSeparator- Parameters:
msg- the message to print
-
println
Prints message to the terminal's standard output atTerminal.Verbositylevel, with alineSeparator.- Parameters:
verbosity- theTerminal.Verbositylevel at which to printmsg- the message to print
-
print
Prints message to the terminal's standard output atTerminal.Verbositylevel, without adding alineSeparator.- Parameters:
verbosity- theTerminal.Verbositylevel at which to printmsg- the message to print
-
errorPrint
Prints a line to the terminal's standard error atTerminal.Verbositylevel, without adding alineSeparator.- Parameters:
verbosity- theTerminal.Verbositylevel at which to print.msg- the message to print
-
errorPrintln
Prints a line to the terminal's standard error atTerminal.Verbosity.NORMALverbosity level, with alineSeparator- Parameters:
msg- the message to print
-
errorPrintln
Prints a line to the terminal's standard error atTerminal.Verbositylevel, with alineSeparator.- Parameters:
verbosity- theTerminal.Verbositylevel at which to print.msg- the message to print
-
isPrintable
Checks if givenTerminal.Verbositylevel is high enough to be printed at the level defined byverbosity- Parameters:
verbosity- theTerminal.Verbositylevel to check- Returns:
- true if the
Terminal.Verbositylevel is high enough to be printed - See Also:
-
promptYesNo
Prompt for a yes or no answer from the user. This method will loop until 'y', 'n' (or the default empty value) is entered.- Parameters:
prompt- the prompt to display to the userdefaultYes- if true, the default answer is 'y', otherwise it is 'n'- Returns:
- true if the user answered 'y', false if the user answered 'n' or the defaultYes value if the user entered nothing
-
readLineToCharArray
Read from the reader until we find a newline. If that newline character is immediately preceded by a carriage return, we have a Windows-style newline, so we discard the carriage return as well as the newline.- Parameters:
reader- the reader to read frommaxLength- the maximum length of the line to read- Returns:
- the line read from the reader
- Throws:
RuntimeException- if the line read exceeds the maximum lengthRuntimeException- if an IOException occurs while reading
-
flush
public void flush()Flushes the terminal's standard output and standard error.
-