Class Terminal

java.lang.Object
org.opensearch.cli.Terminal

public abstract class Terminal extends Object
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:
  • Terminal.ConsoleTerminal
  • Terminal.SystemTerminal
  • Field Details

    • DEFAULT

      public static final Terminal DEFAULT
      The default terminal implementation, which will be a console if available, or stdout/stderr if not.
  • Constructor Details

    • Terminal

      protected Terminal(String lineSeparator)
      Constructs a new terminal with the given line separator.
      Parameters:
      lineSeparator - the line separator to use when calling println
  • Method Details

    • setVerbosity

      public void setVerbosity(Terminal.Verbosity verbosity)
      Sets the verbosity of the terminal. (Default is Terminal.Verbosity.NORMAL)
      Parameters:
      verbosity - the Terminal.Verbosity level that will be used for printing
    • readText

      public abstract String readText(String prompt)
      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

      public abstract char[] readSecret(String prompt)
      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

      public char[] readSecret(String prompt, int maxLength)
      Read secret text from terminal input with echoing disabled, up to a maximum length.
      Parameters:
      prompt - message to display to the user
      maxLength - 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

      public abstract PrintWriter getWriter()
      Returns a Writer which can be used to write to the terminal directly using standard output.
      Returns:
      a writer to DEFAULT output
      See Also:
      • Terminal.ConsoleTerminal
      • Terminal.SystemTerminal
    • getErrorWriter

      public PrintWriter getErrorWriter()
      Returns a Writer which can be used to write to the terminal directly using standard error.
      Returns:
      a writer to stderr
    • println

      public final void println(String msg)
      Prints a line to the terminal at Terminal.Verbosity.NORMAL verbosity level, with a lineSeparator
      Parameters:
      msg - the message to print
    • println

      public final void println(Terminal.Verbosity verbosity, String msg)
      Prints message to the terminal's standard output at Terminal.Verbosity level, with a lineSeparator.
      Parameters:
      verbosity - the Terminal.Verbosity level at which to print
      msg - the message to print
    • print

      public final void print(Terminal.Verbosity verbosity, String msg)
      Prints message to the terminal's standard output at Terminal.Verbosity level, without adding a lineSeparator.
      Parameters:
      verbosity - the Terminal.Verbosity level at which to print
      msg - the message to print
    • errorPrint

      public final void errorPrint(Terminal.Verbosity verbosity, String msg)
      Prints a line to the terminal's standard error at Terminal.Verbosity level, without adding a lineSeparator.
      Parameters:
      verbosity - the Terminal.Verbosity level at which to print.
      msg - the message to print
    • errorPrintln

      public final void errorPrintln(String msg)
      Prints a line to the terminal's standard error at Terminal.Verbosity.NORMAL verbosity level, with a lineSeparator
      Parameters:
      msg - the message to print
    • errorPrintln

      public final void errorPrintln(Terminal.Verbosity verbosity, String msg)
      Prints a line to the terminal's standard error at Terminal.Verbosity level, with a lineSeparator.
      Parameters:
      verbosity - the Terminal.Verbosity level at which to print.
      msg - the message to print
    • isPrintable

      public final boolean isPrintable(Terminal.Verbosity verbosity)
      Checks if given Terminal.Verbosity level is high enough to be printed at the level defined by verbosity
      Parameters:
      verbosity - the Terminal.Verbosity level to check
      Returns:
      true if the Terminal.Verbosity level is high enough to be printed
      See Also:
    • promptYesNo

      public final boolean promptYesNo(String prompt, boolean defaultYes)
      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 user
      defaultYes - 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

      public static char[] readLineToCharArray(Reader reader, int maxLength)
      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 from
      maxLength - the maximum length of the line to read
      Returns:
      the line read from the reader
      Throws:
      RuntimeException - if the line read exceeds the maximum length
      RuntimeException - if an IOException occurs while reading
    • flush

      public void flush()
      Flushes the terminal's standard output and standard error.