java.lang.Object
org.elasticsearch.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,CharSequence)
calls which act like a logger,
only actually printing if the verbosity level of the terminal is above
the verbosity of the message.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enum
Defines the available verbosity levels of messages to be printed. -
Field Summary
Fields -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
Terminal
(Reader reader, PrintWriter outWriter, PrintWriter errWriter) Constructs a terminal instance.protected
Constructs a terminal instance from a delegate instance. -
Method Summary
Modifier and TypeMethodDescriptionfinal OutputStream
asLineOutputStream
(Charset charset) Returns a line based OutputStream wrapping this Terminal's println.final void
errorPrint
(Terminal.Verbosity verbosity, String msg) Prints a line to the terminal's standard error atTerminal.Verbosity.NORMAL
verbosity level, without a newline.final void
errorPrintln
(String msg) Prints a line to the terminal's standard error atTerminal.Verbosity.NORMAL
verbosity level.void
errorPrintln
(Throwable throwable) Prints a stacktrace to the terminal's standard error atTerminal.Verbosity.SILENT
verbosity level.final void
errorPrintln
(Terminal.Verbosity verbosity, String msg) Prints a line to the terminal's standard error atverbosity
level.final void
errorPrintln
(Terminal.Verbosity verbosity, String msg, boolean flush) Prints a line to the terminal's standard error atverbosity
level, with an optional flushvoid
errorPrintln
(Terminal.Verbosity verbosity, Throwable throwable) Prints a stacktrace to the terminal's standard error atverbosity
level.final void
flush()
Flush the outputs of this terminal.Returns an InputStream which can be used to read from the terminal directly using standard input.Returns an OutputStream which can be used to write to the terminal directly using standard output.final Reader
Returns a Reader which can be used to read directly from the terminal using standard input.Return the current verbosity level of this terminal.boolean
Indicates whether this terminal is for a headless system i.e.final boolean
isPrintable
(Terminal.Verbosity verbosity) Checks if is enoughverbosity
level to be printedprotected void
print
(Terminal.Verbosity verbosity, PrintWriter writer, CharSequence msg, boolean newline, boolean flush) Prints message to the terminal atverbosity
level.final void
print
(Terminal.Verbosity verbosity, String msg) Prints message to the terminal's standard output atverbosity
level, without a newline.final void
println
(CharSequence msg) Prints a line to the terminal atTerminal.Verbosity.NORMAL
verbosity level.final void
println
(Terminal.Verbosity verbosity, CharSequence msg) Prints a line to the terminal atverbosity
level.final boolean
promptYesNo
(String prompt, boolean defaultYes) Prompt for a yes or no answer from the user.static char[]
readLineToCharArray
(Reader reader) Read from the reader until we find a newline.char[]
readSecret
(String prompt) Reads password text from the terminal input.Reads clear text from the terminal input.void
setVerbosity
(Terminal.Verbosity verbosity) Sets the verbosity of 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 terminal instance.- Parameters:
reader
- A character-based reader over the input of this terminaloutWriter
- A character-based writer for the output of this terminalerrWriter
- A character-based writer for the error stream of this terminal
-
Terminal
Constructs a terminal instance from a delegate instance.
-
-
Method Details
-
setVerbosity
Sets the verbosity of the terminal.Defaults to
Terminal.Verbosity.NORMAL
. -
getVerbosity
Return the current verbosity level of this terminal. -
readText
Reads clear text from the terminal input. SeeConsole.readLine()
. -
readSecret
Reads password text from the terminal input. SeeConsole.readPassword()
}. -
getReader
Returns a Reader which can be used to read directly from the terminal using standard input. -
asLineOutputStream
Returns a line based OutputStream wrapping this Terminal's println. Note, this OutputStream is not thread-safe! -
getInputStream
Returns an InputStream which can be used to read from the terminal directly using standard input.May return
null
if this Terminal is not capable of binary input. This corresponds with the underlying stream of bytes read byreader
. -
getOutputStream
Returns an OutputStream which can be used to write to the terminal directly using standard output.May return
null
if this Terminal is not capable of binary output. This corresponds with the underlying stream of bytes written to byprintln(CharSequence)
. -
println
Prints a line to the terminal atTerminal.Verbosity.NORMAL
verbosity level. -
println
Prints a line to the terminal atverbosity
level. -
print
Prints message to the terminal's standard output atverbosity
level, without a newline. -
print
protected void print(Terminal.Verbosity verbosity, PrintWriter writer, CharSequence msg, boolean newline, boolean flush) Prints message to the terminal atverbosity
level. Subclasses may override if the writers are not implemented. -
errorPrint
Prints a line to the terminal's standard error atTerminal.Verbosity.NORMAL
verbosity level, without a newline. -
errorPrintln
Prints a line to the terminal's standard error atTerminal.Verbosity.NORMAL
verbosity level. -
errorPrintln
Prints a line to the terminal's standard error atverbosity
level. -
errorPrintln
Prints a line to the terminal's standard error atverbosity
level, with an optional flush -
errorPrintln
Prints a stacktrace to the terminal's standard error atverbosity
level. -
errorPrintln
Prints a stacktrace to the terminal's standard error atTerminal.Verbosity.SILENT
verbosity level. -
isPrintable
Checks if is enoughverbosity
level to be printed -
promptYesNo
Prompt for a yes or no answer from the user. This method will loop until 'y' or 'n' (or the default empty value) is entered. -
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. -
flush
public final void flush()Flush the outputs of this terminal. -
isHeadless
public boolean isHeadless()Indicates whether this terminal is for a headless system i.e. is not interactive. If an instances answersfalse
, interactive operations can be attempted, but it is not guaranteed that they will succeed.- Returns:
- if this terminal is headless.
-