Class UnixLikeTerminal

All Implemented Interfaces:
Scrollable, InputProvider, ExtendedTerminal, Terminal, Closeable, AutoCloseable
Direct Known Subclasses:
UnixLikeTTYTerminal

public abstract class UnixLikeTerminal extends ANSITerminal
Base class for all terminals that generally behave like Unix terminals. This class defined a number of abstract methods that needs to be implemented which are all used to setup the terminal environment (turning off echo, canonical mode, etc) and also a control variable for how to react to CTRL+c keystroke.
  • Field Details

    • terminalCtrlCBehaviour

      private final UnixLikeTerminal.CtrlCBehaviour terminalCtrlCBehaviour
    • catchSpecialCharacters

      private final boolean catchSpecialCharacters
    • shutdownHook

      private final Thread shutdownHook
    • acquired

      private boolean acquired
  • Constructor Details

  • Method Details

    • acquire

      protected void acquire() throws IOException
      Effectively taking over the terminal and enabling it for Lanterna to use, by turning off echo and canonical mode, adding resize listeners and optionally trap unix signals. This should be called automatically by the constructor of any end-user class extending from UnixLikeTerminal
      Throws:
      IOException - If there was an I/O error
    • close

      public void close() throws IOException
      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
      Overrides:
      close in class ANSITerminal
      Throws:
      IOException - If there was an underlying I/O error
    • pollInput

      public KeyStroke pollInput() throws IOException
      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
      Overrides:
      pollInput in class ANSITerminal
      Returns:
      Key object which represents a keystroke coming in through the input stream
      Throws:
      IOException - Propagated error if the underlying stream gave errors
    • readInput

      public KeyStroke readInput() throws IOException
      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
      Overrides:
      readInput in class ANSITerminal
      Returns:
      Key object which represents a keystroke coming in through the input stream
      Throws:
      IOException - Propagated error if the underlying stream gave errors
    • getTerminalCtrlCBehaviour

      protected UnixLikeTerminal.CtrlCBehaviour getTerminalCtrlCBehaviour()
    • registerTerminalResizeListener

      protected abstract void registerTerminalResizeListener(Runnable onResize) throws IOException
      Throws:
      IOException
    • saveTerminalSettings

      protected abstract void saveTerminalSettings() throws IOException
      Stores the current terminal device settings (the ones that are modified through this interface) so that they can be restored later using restoreTerminalSettings()
      Throws:
      IOException - If there was an I/O error when altering the terminal environment
    • restoreTerminalSettings

      protected abstract void restoreTerminalSettings() throws IOException
      Restores the terminal settings from last time saveTerminalSettings() was called
      Throws:
      IOException - If there was an I/O error when altering the terminal environment
    • restoreTerminalSettingsAndKeyStrokeSignals

      private void restoreTerminalSettingsAndKeyStrokeSignals() throws IOException
      Throws:
      IOException
    • keyEchoEnabled

      protected abstract void keyEchoEnabled(boolean enabled) throws IOException
      Enables or disable key echo mode, which means when the user press a key, the terminal will immediately print that key to the terminal. Normally for Lanterna, this should be turned off so the software can take the key as an input event, put it on the input queue and then depending on the code decide what to do with it.
      Parameters:
      enabled - true if key echo should be enabled, false otherwise
      Throws:
      IOException - If there was an I/O error when altering the terminal environment
    • canonicalMode

      protected abstract void canonicalMode(boolean enabled) throws IOException
      In canonical mode, data are accumulated in a line editing buffer, and do not become "available for reading" until line editing has been terminated by the user sending a line delimiter character. This is usually the default mode for a terminal. Lanterna wants to read each character as they are typed, without waiting for the final newline, so it will attempt to turn canonical mode off on initialization.
      Parameters:
      enabled - true if canonical input mode should be enabled, false otherwise
      Throws:
      IOException - If there was an I/O error when altering the terminal environment
    • keyStrokeSignalsEnabled

      protected abstract void keyStrokeSignalsEnabled(boolean enabled) throws IOException
      This method causes certain keystrokes (at the moment only ctrl+c) to be passed in to the program as a regular KeyStroke instead of as a signal to the JVM process. For example, ctrl+c will normally send an interrupt that causes the JVM to shut down, but this method will make it pass in ctrl+c as a regular KeyStroke instead. You can of course still make ctrl+c kill the application through your own input handling if you like.

      Please note that this method is called automatically by lanterna to disable signals unless you define a system property "com.googlecode.lanterna.terminal.UnixTerminal.catchSpecialCharacters" and set it to the string "false".

      Parameters:
      enabled - Pass in true if you want keystrokes to generate system signals (like process interrupt), false if you want lanterna to catch and interpret these keystrokes are regular keystrokes
      Throws:
      IOException - If there was an I/O error when attempting to disable special characters
      See Also:
    • isCtrlC

      private void isCtrlC(KeyStroke key) throws IOException
      Throws:
      IOException
    • exitPrivateModeAndRestoreState

      private void exitPrivateModeAndRestoreState()