Class ExecTerminalProvider

  • All Implemented Interfaces:
    TerminalProvider

    public class ExecTerminalProvider
    extends java.lang.Object
    implements TerminalProvider
    A terminal provider implementation that uses external commands to interact with the terminal.

    The ExecTerminalProvider class provides a TerminalProvider 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 provider 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.

    The provider name is "exec", which can be specified in the org.jline.terminal.provider system property to force the use of this provider.

    See Also:
    TerminalProvider, ExecPty
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      Pty current​(SystemStream systemStream)
      Creates a Pty for the current terminal.
      boolean isPosixSystemStream​(SystemStream stream)  
      boolean isSystemStream​(SystemStream stream)
      Creates a new terminal with custom input and output streams.
      boolean isWindowsSystemStream​(SystemStream stream)  
      java.lang.String name()
      Returns the name of this terminal provider.
      protected static java.lang.ProcessBuilder.Redirect newDescriptor​(java.io.FileDescriptor fd)  
      Terminal newTerminal​(java.lang.String name, java.lang.String type, java.io.InputStream in, java.io.OutputStream out, java.nio.charset.Charset encoding, java.nio.charset.Charset inputEncoding, java.nio.charset.Charset outputEncoding, Terminal.SignalHandler signalHandler, boolean paused, Attributes attributes, Size size)
      Creates a new terminal with custom input and output streams.
      Terminal posixSysTerminal​(java.lang.String name, java.lang.String type, boolean ansiPassThrough, java.nio.charset.Charset encoding, boolean nativeSignals, Terminal.SignalHandler signalHandler, boolean paused, SystemStream systemStream)
      Creates a terminal connected to a system stream on POSIX systems.
      Terminal posixSysTerminal​(java.lang.String name, java.lang.String type, boolean ansiPassThrough, java.nio.charset.Charset encoding, java.nio.charset.Charset stdinEncoding, java.nio.charset.Charset stdoutEncoding, java.nio.charset.Charset stderrEncoding, boolean nativeSignals, Terminal.SignalHandler signalHandler, boolean paused, SystemStream systemStream)
      Create a POSIX system terminal backed by the specified system stream.
      java.lang.String systemStreamName​(SystemStream stream)
      Returns the name of the specified system stream on this platform.
      int systemStreamWidth​(SystemStream stream)
      Returns the width (number of columns) of the specified system stream.
      Terminal sysTerminal​(java.lang.String name, java.lang.String type, boolean ansiPassThrough, java.nio.charset.Charset encoding, java.nio.charset.Charset inputEncoding, java.nio.charset.Charset outputEncoding, boolean nativeSignals, Terminal.SignalHandler signalHandler, boolean paused)
      Creates a terminal connected to a system stream.
      Terminal sysTerminal​(java.lang.String name, java.lang.String type, boolean ansiPassThrough, java.nio.charset.Charset encoding, java.nio.charset.Charset inputEncoding, java.nio.charset.Charset outputEncoding, boolean nativeSignals, Terminal.SignalHandler signalHandler, boolean paused, SystemStream systemStream)
      Creates a terminal connected to a system stream.
      java.lang.String toString()  
      Terminal winSysTerminal​(java.lang.String name, java.lang.String type, boolean ansiPassThrough, java.nio.charset.Charset encoding, boolean nativeSignals, Terminal.SignalHandler signalHandler, boolean paused, SystemStream systemStream)
      Creates a terminal connected to a system stream.
      Terminal winSysTerminal​(java.lang.String name, java.lang.String type, boolean ansiPassThrough, java.nio.charset.Charset encoding, java.nio.charset.Charset stdinEncoding, java.nio.charset.Charset stdoutEncoding, java.nio.charset.Charset stderrEncoding, boolean nativeSignals, Terminal.SignalHandler signalHandler, boolean paused, SystemStream systemStream)
      Create a POSIX-style system terminal for Windows environments that provide a POSIX layer (Cygwin/MSYS); returns null on other Windows variants.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • ExecTerminalProvider

        public ExecTerminalProvider()
        Default constructor.
    • Method Detail

      • name

        public java.lang.String name()
        Returns the name of this terminal provider.

        This method returns the name of this terminal provider, which is "exec". This name can be specified in the org.jline.terminal.provider system property to force the use of this provider.

        Specified by:
        name in interface TerminalProvider
        Returns:
        the name of this terminal provider ("exec")
      • current

        public Pty current​(SystemStream systemStream)
                    throws java.io.IOException
        Creates a Pty 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:
        systemStream - the system stream to associate with the Pty
        Returns:
        a new ExecPty instance for the current terminal
        Throws:
        java.io.IOException - if the current terminal is not a TTY or if an error occurs while executing the 'tty' command
      • sysTerminal

        public Terminal sysTerminal​(java.lang.String name,
                                    java.lang.String type,
                                    boolean ansiPassThrough,
                                    java.nio.charset.Charset encoding,
                                    java.nio.charset.Charset inputEncoding,
                                    java.nio.charset.Charset outputEncoding,
                                    boolean nativeSignals,
                                    Terminal.SignalHandler signalHandler,
                                    boolean paused)
                             throws java.io.IOException
        Creates a terminal connected to a system stream.

        This method creates a terminal that is connected to one of the standard system streams (standard input, standard output, or standard error). It uses the ExecPty implementation to interact with the terminal using external commands.

        Specified by:
        sysTerminal in interface TerminalProvider
        Parameters:
        name - the name of the terminal
        type - the terminal type (e.g., "xterm", "dumb")
        ansiPassThrough - whether to pass through ANSI escape sequences
        encoding - the character encoding to use
        inputEncoding - the character encoding to use for input
        outputEncoding - the character encoding to use for output
        nativeSignals - whether to use native signal handling
        signalHandler - the signal handler to use
        paused - whether the terminal should start in a paused state
        systemStream - the system stream to connect to
        Returns:
        a new terminal connected to the specified system stream
        Throws:
        java.io.IOException - if an I/O error occurs
      • sysTerminal

        public Terminal sysTerminal​(java.lang.String name,
                                    java.lang.String type,
                                    boolean ansiPassThrough,
                                    java.nio.charset.Charset encoding,
                                    java.nio.charset.Charset inputEncoding,
                                    java.nio.charset.Charset outputEncoding,
                                    boolean nativeSignals,
                                    Terminal.SignalHandler signalHandler,
                                    boolean paused,
                                    SystemStream systemStream)
                             throws java.io.IOException
        Description copied from interface: TerminalProvider
        Creates a terminal connected to a system stream.

        This method creates a terminal that is connected to one of the standard system streams (standard input, standard output, or standard error). Such terminals typically represent the actual terminal window or console that the application is running in.

        Specified by:
        sysTerminal in interface TerminalProvider
        Parameters:
        name - the name of the terminal
        type - the terminal type (e.g., "xterm", "dumb")
        ansiPassThrough - whether to pass through ANSI escape sequences (only used on Windows)
        encoding - the general character encoding to use
        inputEncoding - the character encoding to use for input
        outputEncoding - the character encoding to use for output
        nativeSignals - whether to use native signal handling
        signalHandler - the signal handler to use
        paused - whether the terminal should start in a paused state (only used on Windows)
        systemStream - the system stream to connect to
        Returns:
        a new terminal connected to the specified system stream
        Throws:
        java.io.IOException - if an I/O error occurs
      • winSysTerminal

        public Terminal winSysTerminal​(java.lang.String name,
                                       java.lang.String type,
                                       boolean ansiPassThrough,
                                       java.nio.charset.Charset encoding,
                                       boolean nativeSignals,
                                       Terminal.SignalHandler signalHandler,
                                       boolean paused,
                                       SystemStream systemStream)
                                throws java.io.IOException
        Creates a terminal connected to a system stream.

        This method creates a terminal that is connected to one of the standard system streams (standard input, standard output, or standard error). It uses the ExecPty implementation to interact with the terminal using external commands.

        Parameters:
        name - the name of the terminal
        type - the terminal type (e.g., "xterm", "dumb")
        ansiPassThrough - whether to pass through ANSI escape sequences
        encoding - the character encoding to use
        nativeSignals - whether to use native signal handling
        signalHandler - the signal handler to use
        paused - whether the terminal should start in a paused state
        systemStream - the system stream to connect to
        name - the name of the terminal
        type - the terminal type (e.g., "xterm", "dumb")
        ansiPassThrough - whether to pass through ANSI escape sequences
        encoding - the character encoding to use
        nativeSignals - whether to use native signal handling
        signalHandler - the signal handler to use
        paused - whether the terminal should start in a paused state
        systemStream - the system stream to connect to
        Returns:
        a new terminal connected to the specified system stream
        Throws:
        java.io.IOException - if an I/O error occurs /** Creates a terminal connected to a system stream on Windows.

        This method creates a terminal that is connected to one of the standard system streams on Windows. It uses the ExecPty implementation to interact with the terminal using external commands.

        Note that on Windows, the exec provider has limited functionality and may not work as well as the native providers (JNI, JNA, etc.).

        java.io.IOException - if an I/O error occurs
      • winSysTerminal

        public Terminal winSysTerminal​(java.lang.String name,
                                       java.lang.String type,
                                       boolean ansiPassThrough,
                                       java.nio.charset.Charset encoding,
                                       java.nio.charset.Charset stdinEncoding,
                                       java.nio.charset.Charset stdoutEncoding,
                                       java.nio.charset.Charset stderrEncoding,
                                       boolean nativeSignals,
                                       Terminal.SignalHandler signalHandler,
                                       boolean paused,
                                       SystemStream systemStream)
                                throws java.io.IOException
        Create a POSIX-style system terminal for Windows environments that provide a POSIX layer (Cygwin/MSYS); returns null on other Windows variants.
        Parameters:
        name - the terminal name
        type - the terminal type (TERM)
        ansiPassThrough - whether ANSI sequences should be passed through (unused for POSIX path)
        encoding - the general charset for the terminal
        stdinEncoding - charset to use for stdin
        stdoutEncoding - charset to use for stdout
        stderrEncoding - charset to use for stderr
        nativeSignals - whether native signal handling is enabled
        signalHandler - handler for terminal signals
        paused - whether the terminal should be created in a paused state
        systemStream - the system stream to associate with the terminal; determines which output encoding (stdout vs stderr) is selected
        Returns:
        a configured PosixSysTerminal when running under Cygwin or MSYS, null otherwise
        Throws:
        java.io.IOException
      • posixSysTerminal

        public Terminal posixSysTerminal​(java.lang.String name,
                                         java.lang.String type,
                                         boolean ansiPassThrough,
                                         java.nio.charset.Charset encoding,
                                         boolean nativeSignals,
                                         Terminal.SignalHandler signalHandler,
                                         boolean paused,
                                         SystemStream systemStream)
                                  throws java.io.IOException
        Creates a terminal connected to a system stream on POSIX systems.

        This method creates a terminal that is connected to one of the standard system streams on POSIX systems (Linux, macOS, etc.). It uses the ExecPty implementation to interact with the terminal using external commands.

        Parameters:
        name - the name of the terminal
        type - the terminal type (e.g., "xterm", "dumb")
        ansiPassThrough - whether to pass through ANSI escape sequences
        encoding - the character encoding to use
        nativeSignals - whether to use native signal handling
        signalHandler - the signal handler to use
        paused - whether the terminal should start in a paused state
        systemStream - the system stream to connect to
        Returns:
        a new terminal connected to the specified system stream
        Throws:
        java.io.IOException - if an I/O error occurs
      • posixSysTerminal

        public Terminal posixSysTerminal​(java.lang.String name,
                                         java.lang.String type,
                                         boolean ansiPassThrough,
                                         java.nio.charset.Charset encoding,
                                         java.nio.charset.Charset stdinEncoding,
                                         java.nio.charset.Charset stdoutEncoding,
                                         java.nio.charset.Charset stderrEncoding,
                                         boolean nativeSignals,
                                         Terminal.SignalHandler signalHandler,
                                         boolean paused,
                                         SystemStream systemStream)
                                  throws java.io.IOException
        Create a POSIX system terminal backed by the specified system stream.
        Parameters:
        name - the terminal name
        type - the terminal type
        ansiPassThrough - whether ANSI sequences should be passed through (platform-dependent)
        encoding - the terminal's primary character encoding
        stdinEncoding - the encoding to use for standard input
        stdoutEncoding - the encoding to use for standard output (used when systemStream is not Error)
        stderrEncoding - the encoding to use for standard error (used when systemStream is Error)
        nativeSignals - whether native signal handling is enabled
        signalHandler - handler for terminal signals
        paused - whether the terminal should be created in a paused state
        systemStream - the system stream to back the terminal; selects the output encoding (uses stderrEncoding when SystemStream.Error, otherwise stdoutEncoding)
        Returns:
        a POSIX-based Terminal instance connected to the chosen system stream
        Throws:
        java.io.IOException - if obtaining the underlying PTY or creating the terminal fails
      • newTerminal

        public Terminal newTerminal​(java.lang.String name,
                                    java.lang.String type,
                                    java.io.InputStream in,
                                    java.io.OutputStream out,
                                    java.nio.charset.Charset encoding,
                                    java.nio.charset.Charset inputEncoding,
                                    java.nio.charset.Charset outputEncoding,
                                    Terminal.SignalHandler signalHandler,
                                    boolean paused,
                                    Attributes attributes,
                                    Size size)
                             throws java.io.IOException
        Creates a new terminal with custom input and output streams.

        This method creates a terminal that is connected to the specified input and output streams. It creates an ExternalTerminal that emulates the line discipline functionality typically provided by the operating system's terminal driver.

        Specified by:
        newTerminal in interface TerminalProvider
        Parameters:
        name - the name of the terminal
        type - the terminal type (e.g., "xterm", "dumb")
        in - the input stream to read from
        out - the output stream to write to
        encoding - the character encoding to use
        inputEncoding - the character encoding to use for input
        outputEncoding - the character encoding to use for output
        signalHandler - the signal handler to use
        paused - whether the terminal should start in a paused state
        attributes - the initial terminal attributes
        size - the initial terminal size
        Returns:
        a new terminal connected to the specified streams
        Throws:
        java.io.IOException - if an I/O error occurs
      • isSystemStream

        public boolean isSystemStream​(SystemStream stream)
        Creates a new terminal with custom input and output streams.

        This method creates a terminal that is connected to the specified input and output streams. It creates an ExternalTerminal that emulates the line discipline functionality typically provided by the operating system's terminal driver.

        Specified by:
        isSystemStream in interface TerminalProvider
        Parameters:
        name - the name of the terminal
        type - the terminal type (e.g., "xterm", "dumb")
        in - the input stream to read from
        out - the output stream to write to
        encoding - the character encoding to use
        signalHandler - the signal handler to use
        paused - whether the terminal should start in a paused state
        attributes - the initial terminal attributes
        size - the initial terminal size
        stream - the system stream to check
        Returns:
        a new terminal connected to the specified streams
        Throws:
        java.io.IOException - if an I/O error occurs /** Checks if the specified system stream is available on this platform.

        This method determines whether the specified system stream (standard input, standard output, or standard error) is available for use on the current platform. It checks both POSIX and Windows system streams.

      • isWindowsSystemStream

        public boolean isWindowsSystemStream​(SystemStream stream)
      • isPosixSystemStream

        public boolean isPosixSystemStream​(SystemStream stream)
      • systemStreamName

        public java.lang.String systemStreamName​(SystemStream stream)
        Returns the name of the specified system stream on this platform.

        This method returns a platform-specific name or identifier for the specified system stream. The name may be used for display purposes or for accessing the stream through platform-specific APIs.

        Specified by:
        systemStreamName in interface TerminalProvider
        Parameters:
        stream - the system stream
        Returns:
        the name of the system stream on this platform
      • systemStreamWidth

        public int systemStreamWidth​(SystemStream stream)
        Returns the width (number of columns) of the specified system stream.

        This method determines the width of the terminal associated with the specified system stream. The width is measured in character cells and represents the number of columns available for display.

        This implementation uses the 'tput cols' command to determine the terminal width. If the command fails or returns an invalid value, a default width of 80 columns is returned.

        Specified by:
        systemStreamWidth in interface TerminalProvider
        Parameters:
        stream - the system stream
        Returns:
        the width of the system stream in character columns
      • newDescriptor

        protected static java.lang.ProcessBuilder.Redirect newDescriptor​(java.io.FileDescriptor fd)
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object