Class ExecPty

java.lang.Object
org.jline.terminal.impl.AbstractPty
org.jline.terminal.impl.exec.ExecPty
All Implemented Interfaces:
Closeable, AutoCloseable, Pty

public class ExecPty extends AbstractPty implements Pty
A pseudoterminal implementation that uses external commands to interact with the terminal.

The ExecPty class provides a Pty implementation that uses external commands (such as stty, tput, etc.) to interact with the terminal. This approach allows JLine to work in environments where native libraries are not available or cannot be used, by relying on standard command-line utilities that are typically available on Unix-like systems.

This implementation executes external commands to perform operations such as:

  • Getting and setting terminal attributes
  • Getting and setting terminal size
  • Determining the current terminal device

The ExecPty is typically used as a fallback when more direct methods of terminal interaction (such as JNI or JNA) are not available. While it provides good compatibility, it may have higher overhead due to the need to spawn external processes for many operations.

See Also:
  • Constructor Details

    • ExecPty

      protected ExecPty(TerminalProvider provider, SystemStream systemStream, String name)
      Creates a new ExecPty instance.

      This constructor creates a new ExecPty instance with the specified provider, system stream, and terminal device name. It is protected because instances should typically be created using the current(TerminalProvider, SystemStream) method.

      Parameters:
      provider - the terminal provider that will own this Pty
      systemStream - the system stream associated with this Pty
      name - the name of the terminal device (e.g., "/dev/tty")
  • Method Details

    • current

      public static Pty current(TerminalProvider provider, SystemStream systemStream) throws IOException
      Creates an ExecPty instance for the current terminal.

      This method creates an ExecPty instance for the current terminal by executing the 'tty' command to determine the terminal device name. It is used to obtain a Pty object that can interact with the current terminal using external commands.

      Parameters:
      provider - the terminal provider that will own this Pty
      systemStream - the system stream (must be Output or Error) associated with this Pty
      Returns:
      a new ExecPty instance for the current terminal
      Throws:
      IOException - if the current terminal is not a TTY or if an error occurs while executing the 'tty' command
      IllegalArgumentException - if systemStream is not Output or Error
    • close

      public void close() throws IOException
      Closes this Pty.

      This implementation does nothing, as there are no resources to release. The terminal device is not actually opened by this class, so it does not need to be closed.

      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException - if an I/O error occurs (never thrown by this implementation)
    • getName

      public String getName()
      Returns the name of the terminal device.

      This method returns the name of the terminal device associated with this Pty, which was determined when the Pty was created. This is typically a device path such as "/dev/tty" or "/dev/pts/0".

      Returns:
      the name of the terminal device
    • getMasterInput

      public InputStream getMasterInput()
      Description copied from interface: Pty
      Returns the input stream for the master side of the PTY.

      This stream receives data that has been written to the slave's output stream. Terminal emulators typically read from this stream to get the output from processes running in the terminal.

      Specified by:
      getMasterInput in interface Pty
      Returns:
      the master's input stream
    • getMasterOutput

      public OutputStream getMasterOutput()
      Description copied from interface: Pty
      Returns the output stream for the master side of the PTY.

      Data written to this stream will be available for reading from the slave's input stream. Terminal emulators typically write to this stream to send input to processes running in the terminal.

      Specified by:
      getMasterOutput in interface Pty
      Returns:
      the master's output stream
    • doGetSlaveInput

      protected InputStream doGetSlaveInput() throws IOException
      Specified by:
      doGetSlaveInput in class AbstractPty
      Throws:
      IOException
    • getSlaveOutput

      public OutputStream getSlaveOutput() throws IOException
      Description copied from interface: Pty
      Returns the output stream for the slave side of the PTY.

      Data written to this stream will be available for reading from the master's input stream. Processes running in the terminal write to this stream to produce their output.

      Specified by:
      getSlaveOutput in interface Pty
      Returns:
      the slave's output stream
      Throws:
      IOException - if an I/O error occurs
    • getAttr

      public Attributes getAttr() throws IOException
      Description copied from interface: Pty
      Returns the current terminal attributes for this PTY.

      Terminal attributes control various aspects of terminal behavior, such as echo settings, line discipline, and control characters.

      Specified by:
      getAttr in interface Pty
      Returns:
      the current terminal attributes
      Throws:
      IOException - if an I/O error occurs
      See Also:
    • doSetAttr

      protected void doSetAttr(Attributes attr) throws IOException
      Specified by:
      doSetAttr in class AbstractPty
      Throws:
      IOException
    • getFlagsToSet

      protected List<String> getFlagsToSet(Attributes attr, Attributes current)
    • getSize

      public Size getSize() throws IOException
      Description copied from interface: Pty
      Returns the current size (dimensions) of this PTY.

      The size includes the number of rows and columns in the terminal window.

      Specified by:
      getSize in interface Pty
      Returns:
      the current terminal size
      Throws:
      IOException - if an I/O error occurs
      See Also:
    • doGetConfig

      protected String doGetConfig() throws IOException
      Throws:
      IOException
    • doGetAttr

      public static Attributes doGetAttr(String cfg) throws IOException
      Throws:
      IOException
    • setSize

      public void setSize(Size size) throws IOException
      Description copied from interface: Pty
      Sets the size (dimensions) of this PTY.

      This method changes the number of rows and columns in the terminal window. When the size changes, a SIGWINCH signal is typically sent to processes running in the terminal.

      Specified by:
      setSize in interface Pty
      Parameters:
      size - the new terminal size to set
      Throws:
      IOException - if an I/O error occurs
      See Also:
    • toString

      public String toString()
      Overrides:
      toString in class Object