Class PosixPtyTerminal

All Implemented Interfaces:
Closeable, Flushable, AutoCloseable, Sized, TerminalExt, Terminal

public class PosixPtyTerminal extends AbstractPosixTerminal
Terminal implementation for POSIX systems using a pseudoterminal (PTY).

The PosixPtyTerminal class provides a terminal implementation for POSIX systems (Linux, macOS, etc.) that uses a pseudoterminal (PTY) for terminal operations. It extends the AbstractPosixTerminal class and adds functionality specific to PTY-based terminals.

This implementation is used when a full terminal emulation is needed, such as when creating a terminal for an external process or when connecting to a remote terminal. It provides access to the master and slave sides of the PTY, allowing for bidirectional communication with the terminal.

Key features of this implementation include:

  • Full terminal emulation using a pseudoterminal
  • Support for terminal attributes and size changes
  • Access to both master and slave sides of the PTY
  • Support for non-blocking I/O
See Also:
  • Constructor Details

  • Method Details

    • input

      public InputStream input()
      Description copied from interface: Terminal
      Retrieve the input stream for this terminal. In some rare cases, there may be a need to access the terminal input stream directly. In the usual cases, use the Terminal.reader() instead.

      The returned stream is owned by this terminal and will be closed when the terminal is closed. Callers should not close it directly.

      Returns:
      The input stream
      See Also:
    • reader

      public NonBlockingReader reader()
      Description copied from interface: Terminal
      Retrieve the Reader for this terminal. This is the standard way to read input from this terminal. The reader is non blocking.

      The returned reader is owned by this terminal and will be closed when the terminal is closed. Callers should not close it directly.

      Returns:
      The non blocking reader
    • output

      public OutputStream output()
      Description copied from interface: Terminal
      Retrieve the output stream for this terminal. In some rare cases, there may be a need to access the terminal output stream directly. In the usual cases, use the Terminal.writer() instead.

      The returned stream is owned by this terminal and will be closed when the terminal is closed. Callers should not close it directly.

      Returns:
      The output stream
      See Also:
    • writer

      public PrintWriter writer()
      Description copied from interface: Terminal
      Retrieve the Writer for this terminal. This is the standard way to write to this terminal.

      The returned writer is owned by this terminal and will be closed when the terminal is closed. Callers should not close it directly.

      Returns:
      The writer
    • doClose

      protected void doClose() throws IOException
      Overrides:
      doClose in class AbstractPosixTerminal
      Throws:
      IOException
    • canPauseResume

      public boolean canPauseResume()
      Description copied from interface: Terminal
      Whether this terminal supports Terminal.pause() and Terminal.resume() calls.
      Specified by:
      canPauseResume in interface Terminal
      Overrides:
      canPauseResume in class AbstractTerminal
      Returns:
      whether this terminal supports Terminal.pause() and Terminal.resume() calls.
      See Also:
    • pause

      public void pause()
      Description copied from interface: Terminal
      Temporarily stops reading the input stream.

      This method pauses the terminal's input processing, which can be useful when transferring control to a subprocess or when the terminal needs to be in a specific state for certain operations. While paused, the terminal will not process input or handle signals that would normally be triggered by special characters in the input stream.

      This method returns immediately without waiting for the terminal to actually pause. To wait until the terminal has fully paused, use Terminal.pause(boolean) with a value of true.

      Example usage:

       Terminal terminal = TerminalBuilder.terminal();
      
       // Pause terminal input processing before running a subprocess
       terminal.pause();
      
       // Run subprocess that takes control of the terminal
       Process process = new ProcessBuilder("vim").inheritIO().start();
       process.waitFor();
      
       // Resume terminal input processing
       terminal.resume();
       
      Specified by:
      pause in interface Terminal
      Overrides:
      pause in class AbstractTerminal
      See Also:
    • pause

      public void pause(boolean wait) throws InterruptedException
      Description copied from interface: Terminal
      Stop reading the input stream and optionally wait for the underlying threads to finish.
      Specified by:
      pause in interface Terminal
      Overrides:
      pause in class AbstractTerminal
      Parameters:
      wait - true to wait until the terminal is actually paused
      Throws:
      InterruptedException - if the call has been interrupted
    • resume

      public void resume()
      Description copied from interface: Terminal
      Resumes reading the input stream after it has been paused.

      This method restarts the terminal's input processing after it has been temporarily stopped using Terminal.pause() or Terminal.pause(boolean). Once resumed, the terminal will continue to process input and handle signals triggered by special characters in the input stream.

      Calling this method when the terminal is not paused has no effect.

      Example usage:

       Terminal terminal = TerminalBuilder.terminal();
      
       // Pause terminal input processing
       terminal.pause();
      
       // Perform operations while terminal input is paused...
      
       // Resume terminal input processing
       terminal.resume();
       
      Specified by:
      resume in interface Terminal
      Overrides:
      resume in class AbstractTerminal
      See Also:
    • paused

      public boolean paused()
      Description copied from interface: Terminal
      Check whether the terminal is currently reading the input stream or not. In order to process signal as quickly as possible, the terminal need to read the input stream and buffer it internally so that it can detect specific characters in the input stream (Ctrl+C, Ctrl+D, etc...) and raise the appropriate signals. However, there are some cases where this processing should be disabled, for example when handing the terminal control to a subprocess.
      Specified by:
      paused in interface Terminal
      Overrides:
      paused in class AbstractTerminal
      Returns:
      whether the terminal is currently reading the input stream or not
      See Also: