Interface IOSafeTerminal

All Superinterfaces:
AutoCloseable, Closeable, InputProvider, Terminal
All Known Subinterfaces:
IOSafeExtendedTerminal, VirtualTerminal
All Known Implementing Classes:
AWTTerminal, AWTTerminalFrame, AWTTerminalImplementation, DefaultVirtualTerminal, GraphicalTerminalImplementation, IOSafeTerminalAdapter, IOSafeTerminalAdapter.Extended, ScrollingAWTTerminal, ScrollingSwingTerminal, SwingTerminal, SwingTerminalFrame, SwingTerminalImplementation

public interface IOSafeTerminal extends Terminal
Interface extending Terminal that removes the IOException throw clause. You can for example use this instead of Terminal if you use an implementation that doesn't throw any IOExceptions or if you wrap your terminal in an IOSafeTerminalAdapter. Please note that readInput() still throws IOException when it is interrupted, in order to fit better in with what normal terminal do when they are blocked on input and you interrupt them.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Prints 0x7 to the terminal, which will make the terminal (emulator) ring a bell (or more likely beep).
    void
    Removes all the characters, colors and graphics from the screen and leaves you with a big empty space.
    void
    Closes the terminal, if applicable.
    void
    Deactivates an SGR (Selected Graphic Rendition) code which has previously been activated through enableSGR(..).
    void
    Activates an SGR (Selected Graphic Rendition) code.
    byte[]
    enquireTerminal(int timeout, TimeUnit timeoutUnit)
    Retrieves optional information from the terminal by printing the ENQ (\u005) character.
    void
    Calling this method will, where supported, give your terminal a private area to use, separate from what was there before.
    void
    If you have previously entered private mode, this method will exit this and, depending on implementation, maybe restore what the terminal looked like before private mode was entered.
    void
    Calls flush() on the underlying OutputStream object, or whatever other implementation this terminal is built around.
    Returns the position of the cursor, as reported by the terminal.
    Returns the size of the terminal, expressed as a TerminalSize object.
    Returns the next Key off the input queue or null if there is no more input events available.
    void
    putCharacter(char c)
    Prints one character to the terminal at the current cursor location.
    void
    putString(String string)
    Prints a string to the terminal at the current cursor location.
    Returns the next Key off the input queue or blocks until one is available.
    void
    Removes all currently active SGR codes and sets foreground and background colors back to default.
    void
    Changes the background color for all the following characters put to the terminal.
    void
    setCursorPosition(int x, int y)
    Moves the text cursor to a new location on the terminal.
    void
    Same as calling setCursorPosition(position.getColumn(), position.getRow())
    void
    setCursorVisible(boolean visible)
    Hides or shows the text cursor, but not all terminal (-emulators) supports this.
    void
    Changes the foreground color for all the following characters put to the terminal.

    Methods inherited from interface com.googlecode.lanterna.terminal.Terminal

    addResizeListener, newTextGraphics, removeResizeListener
  • Method Details

    • enterPrivateMode

      void enterPrivateMode()
      Description copied from interface: Terminal
      Calling this method will, where supported, give your terminal a private area to use, separate from what was there before. Some terminal emulators will preserve the terminal history and restore it when you exit private mode. Some terminals will just clear the screen and put the cursor in the top-left corner. Typically, if you terminal supports scrolling, going into private mode will disable the scrolling and leave you with a fixed screen, which can be useful if you don't want to deal with what the terminal buffer will look like if the user scrolls up.
      Specified by:
      enterPrivateMode in interface Terminal
    • exitPrivateMode

      void exitPrivateMode()
      Description copied from interface: Terminal
      If you have previously entered private mode, this method will exit this and, depending on implementation, maybe restore what the terminal looked like before private mode was entered. If the terminal doesn't support a secondary buffer for private mode, it will probably make a new line below the private mode and place the cursor there.
      Specified by:
      exitPrivateMode in interface Terminal
    • clearScreen

      void clearScreen()
      Description copied from interface: Terminal
      Removes all the characters, colors and graphics from the screen and leaves you with a big empty space. Text cursor position is undefined after this call (depends on platform and terminal) so you should always call moveCursor next. Some terminal implementations doesn't reset color and modifier state so it's also good practise to call resetColorAndSGR() after this.
      Specified by:
      clearScreen in interface Terminal
    • setCursorPosition

      void setCursorPosition(int x, int y)
      Description copied from interface: Terminal
      Moves the text cursor to a new location on the terminal. The top-left corner has coordinates 0 x 0 and the bottom- right corner has coordinates terminal_width-1 x terminal_height-1. You can retrieve the size of the terminal by calling getTerminalSize().
      Specified by:
      setCursorPosition in interface Terminal
      Parameters:
      x - The 0-indexed column to place the cursor at
      y - The 0-indexed row to place the cursor at
    • setCursorPosition

      void setCursorPosition(TerminalPosition position)
      Description copied from interface: Terminal
      Same as calling setCursorPosition(position.getColumn(), position.getRow())
      Specified by:
      setCursorPosition in interface Terminal
      Parameters:
      position - Position to place the cursor at
    • getCursorPosition

      TerminalPosition getCursorPosition()
      Description copied from interface: Terminal
      Returns the position of the cursor, as reported by the terminal. The top-left corner has coordinates 0 x 0 and the bottom-right corner has coordinates terminal_width-1 x terminal_height-1.
      Specified by:
      getCursorPosition in interface Terminal
      Returns:
      Position of the cursor
    • setCursorVisible

      void setCursorVisible(boolean visible)
      Description copied from interface: Terminal
      Hides or shows the text cursor, but not all terminal (-emulators) supports this. The text cursor is normally a text block or an underscore, sometimes blinking, which shows the user where keyboard-entered text is supposed to show up.
      Specified by:
      setCursorVisible in interface Terminal
      Parameters:
      visible - Hides the text cursor if false and shows it if true
    • putCharacter

      void putCharacter(char c)
      Description copied from interface: Terminal
      Prints one character to the terminal at the current cursor location. Please note that the cursor will then move one column to the right, so multiple calls to putCharacter will print out a text string without the need to reposition the text cursor. If you reach the end of the line while putting characters using this method, you can expect the text cursor to move to the beginning of the next line.

      You can output CJK (Chinese, Japanese, Korean) characters (as well as other regional scripts) but remember that the terminal that the user is using might not have the required font to render it. Also worth noticing is that CJK (and some others) characters tend to take up 2 columns per character, simply because they are a square in their construction as opposed to the somewhat rectangular shape we fit latin characters in. As it's very difficult to create a monospace font for CJK with a 2:1 height-width proportion, it seems like the implementers back in the days simply gave up and made each character take 2 column. It causes issues for the random terminal programmer because you can't really trust 1 character = 1 column, but I suppose it's "しょうがない". If you try to print non-printable control characters, the terminal is likely to ignore them (all Terminal implementations bundled with Lanterna will).

      Specified by:
      putCharacter in interface Terminal
      Parameters:
      c - Character to place on the terminal
    • putString

      void putString(String string)
      Description copied from interface: Terminal
      Prints a string to the terminal at the current cursor location. Please note that the cursor will then move one column to the right, so multiple calls to putString will print out a text string without the need to reposition the text cursor. If you reach the end of the line while putting characters using this method, you can expect the text cursor to move to the beginning of the next line.

      You can output CJK (Chinese, Japanese, Korean) characters (as well as other regional scripts) but remember that the terminal that the user is using might not have the required font to render it. Also worth noticing is that CJK (and some others) characters tend to take up 2 columns per character, simply because they are a square in their construction as opposed to the somewhat rectangular shape we fit latin characters in. As it's very difficult to create a monospace font for CJK with a 2:1 height-width proportion, it seems like the implementers back in the days simply gave up and made each character take 2 column. It causes issues for the random terminal programmer because you can't really trust 1 character = 1 column, but I suppose it's "しょうがない".

      If you try to print non-printable control characters, the terminal is likely to ignore them (all Terminal implementations bundled with Lanterna will).

      You can use this method to place emoji characters on the terminal, since they take up more than one char with Java's built-in UTF16 encoding.

      Specified by:
      putString in interface Terminal
      Parameters:
      string - String to place on the terminal
    • enableSGR

      void enableSGR(SGR sgr)
      Description copied from interface: Terminal
      Activates an SGR (Selected Graphic Rendition) code. This code modifies a state inside the terminal that will apply to all characters written afterwards, such as bold, italic, blinking code and so on.
      Specified by:
      enableSGR in interface Terminal
      Parameters:
      sgr - SGR code to apply
      See Also:
    • disableSGR

      void disableSGR(SGR sgr)
      Description copied from interface: Terminal
      Deactivates an SGR (Selected Graphic Rendition) code which has previously been activated through enableSGR(..).
      Specified by:
      disableSGR in interface Terminal
      Parameters:
      sgr - SGR code to apply
      See Also:
    • resetColorAndSGR

      void resetColorAndSGR()
      Description copied from interface: Terminal
      Removes all currently active SGR codes and sets foreground and background colors back to default.
      Specified by:
      resetColorAndSGR in interface Terminal
      See Also:
    • setForegroundColor

      void setForegroundColor(TextColor color)
      Description copied from interface: Terminal
      Changes the foreground color for all the following characters put to the terminal. The foreground color is what color to draw the text in, as opposed to the background color which is the color surrounding the characters.

      This overload is using the TextColor class to define a color, which is a layer of abstraction above the three different color formats supported (ANSI, indexed and RGB). The other setForegroundColor(..) overloads gives you direct access to set one of those three.

      Note to implementers of this interface, just make this method call color.applyAsForeground(this);

      Specified by:
      setForegroundColor in interface Terminal
      Parameters:
      color - Color to use for foreground
    • setBackgroundColor

      void setBackgroundColor(TextColor color)
      Description copied from interface: Terminal
      Changes the background color for all the following characters put to the terminal. The background color is the color surrounding the text being printed.

      This overload is using the TextColor class to define a color, which is a layer of abstraction above the three different color formats supported (ANSI, indexed and RGB). The other setBackgroundColor(..) overloads gives you direct access to set one of those three.

      Note to implementers of this interface, just make this method call color.applyAsBackground(this);

      Specified by:
      setBackgroundColor in interface Terminal
      Parameters:
      color - Color to use for the background
    • getTerminalSize

      TerminalSize getTerminalSize()
      Description copied from interface: Terminal
      Returns the size of the terminal, expressed as a TerminalSize object. Please bear in mind that depending on the Terminal implementation, this may or may not be accurate. See the implementing classes for more information. Most commonly, calling getTerminalSize() will involve some kind of hack to retrieve the size of the terminal, like moving the cursor to position 5000x5000 and then read back the location, unless the terminal implementation has a more smooth way of getting this data. Keep this in mind and see if you can avoid calling this method too often. There is a helper class, SimpleTerminalResizeListener, that you can use to cache the size and update it only when resize events are received (which depends on if a resize is detectable, which they are not on all platforms).
      Specified by:
      getTerminalSize in interface Terminal
      Returns:
      Size of the terminal
    • enquireTerminal

      byte[] enquireTerminal(int timeout, TimeUnit timeoutUnit)
      Description copied from interface: Terminal
      Retrieves optional information from the terminal by printing the ENQ (\u005) character. Terminals and terminal emulators may or may not respond to this command, sometimes it's configurable.
      Specified by:
      enquireTerminal in interface Terminal
      Parameters:
      timeout - How long to wait for the talk-back message, if there's nothing immediately available on the input stream, you should probably set this to a somewhat small value to prevent unnecessary blockage on the input stream but large enough to accommodate a round-trip to the user's terminal (~300 ms if you are connection across the globe).
      timeoutUnit - What unit to use when interpreting the timeout parameter
      Returns:
      Answer-back message from the terminal or empty if there was nothing
    • bell

      void bell()
      Description copied from interface: Terminal
      Prints 0x7 to the terminal, which will make the terminal (emulator) ring a bell (or more likely beep). Not all terminals implements this. Wikipedia has more details.
      Specified by:
      bell in interface Terminal
    • flush

      void flush()
      Description copied from interface: Terminal
      Calls flush() on the underlying OutputStream object, or whatever other implementation this terminal is built around. Some implementing classes of this interface (like SwingTerminal) doesn't do anything as it doesn't really apply to them.
      Specified by:
      flush in interface Terminal
    • pollInput

      KeyStroke pollInput()
      Description copied from interface: InputProvider
      Returns the next Key off the input queue or null if there is no more input events available. Note, this method call is not blocking, it returns null immediately if there is nothing on the input stream.
      Specified by:
      pollInput in interface InputProvider
      Returns:
      Key object which represents a keystroke coming in through the input stream
    • readInput

      KeyStroke readInput()
      Description copied from interface: InputProvider
      Returns the next Key off the input queue or blocks until one is available. NOTE: In previous versions of Lanterna, this method was not blocking. From lanterna 3, it is blocking and you can call pollInput() for the non-blocking version.
      Specified by:
      readInput in interface InputProvider
      Returns:
      Key object which represents a keystroke coming in through the input stream
    • close

      void close()
      Description copied from interface: Terminal
      Closes the terminal, if applicable. If the implementation doesn't support closing the terminal, this will do nothing. The Swing/AWT emulator implementations will translate this into a dispose() call on the UI resources, the telnet implementation will hang out the connection.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Specified by:
      close in interface Terminal