- java.lang.Object
-
- org.jline.terminal.impl.AbstractTerminal
-
- org.jline.terminal.impl.AbstractPosixTerminal
-
- org.jline.terminal.impl.PosixSysTerminal
-
- All Implemented Interfaces:
java.io.Closeable,java.io.Flushable,java.lang.AutoCloseable,Sized,TerminalExt,Terminal
public class PosixSysTerminal extends AbstractPosixTerminal
Terminal implementation for POSIX systems using system streams.The PosixSysTerminal class provides a terminal implementation for POSIX systems (Linux, macOS, etc.) that uses the system standard input and output streams. It extends the AbstractPosixTerminal class and adds functionality specific to system stream-based terminals.
This implementation is used when connecting to the actual system terminal, such as when running a console application in a terminal window. It provides access to the standard input and output streams, allowing for interaction with the user through the terminal.
Key features of this implementation include:
- Direct access to system standard input and output
- Support for terminal attributes and size changes
- Support for non-blocking I/O
- Automatic restoration of terminal state on shutdown
- See Also:
AbstractPosixTerminal,Pty
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.jline.terminal.Terminal
Terminal.KittyKeyboardMode, Terminal.Mode, Terminal.MouseTracking, Terminal.Signal, Terminal.SignalHandler
-
-
Field Summary
Fields Modifier and Type Field Description protected ShutdownHooks.Taskcloserprotected NonBlockingInputStreaminputprotected java.util.Map<Terminal.Signal,java.lang.Object>nativeHandlersprotected java.io.OutputStreamoutputprotected TerminalProviderproviderprotected NonBlockingReaderreaderprotected java.io.PrintWriterwriter-
Fields inherited from class org.jline.terminal.impl.AbstractPosixTerminal
originalAttributes, pty
-
Fields inherited from class org.jline.terminal.impl.AbstractTerminal
bools, closed, currentMouseTracking, encoding, envProvider, handlers, inputEncoding, ints, name, onClose, outputEncoding, palette, status, strings, type
-
Fields inherited from interface org.jline.terminal.Terminal
TYPE_DUMB, TYPE_DUMB_COLOR
-
-
Constructor Summary
Constructors Constructor Description PosixSysTerminal(java.lang.String name, java.lang.String type, Pty pty, java.nio.charset.Charset encoding, boolean nativeSignals, Terminal.SignalHandler signalHandler)Creates a POSIX system terminal backed by the provided PTY, using the same charset for input and output, and optionally enabling native signal handling.PosixSysTerminal(java.lang.String name, java.lang.String type, Pty pty, java.nio.charset.Charset encoding, java.nio.charset.Charset inputEncoding, java.nio.charset.Charset outputEncoding, boolean nativeSignals, Terminal.SignalHandler signalHandler)Creates a POSIX system terminal backed by the given PTY and character encodings, without a TerminalProvider.PosixSysTerminal(TerminalProvider provider, java.lang.String name, java.lang.String type, Pty pty, java.nio.charset.Charset encoding, java.nio.charset.Charset inputEncoding, java.nio.charset.Charset outputEncoding, boolean nativeSignals, Terminal.SignalHandler signalHandler)Create a POSIX system terminal backed by the provided PTY, initializing non-blocking input/output, reader/writer encodings, and optional native signal handlers.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voiddoClose()Closes the terminal and releases its resources: flushes pending output, removes the shutdown hook, unregisters any native signal handlers, performs superclass shutdown, and closes the reader.AttributesgetAttributes()Returns the current terminal attributes.Terminal.SignalHandlerhandle(Terminal.Signal signal, Terminal.SignalHandler handler)Install a new handler for the given signal and synchronize native registration when the handler changes.java.io.InputStreaminput()Retrieve the input stream for this terminal.java.io.OutputStreamoutput()Retrieve the output stream for this terminal.NonBlockingReaderreader()Retrieve theReaderfor this terminal.voidsetAttributes(Attributes attr)Sets the terminal attributes to the specified values.booleansupportsGraphemeClusterMode()Determine if grapheme cluster mode is supported for this terminal.java.io.PrintWriterwriter()Retrieve theWriterfor this terminal.-
Methods inherited from class org.jline.terminal.impl.AbstractPosixTerminal
getCursorPosition, getDefaultBackgroundColor, getDefaultForegroundColor, getProvider, getPty, getSize, getSystemStream, setSize, toString
-
Methods inherited from class org.jline.terminal.impl.AbstractTerminal
canPauseResume, checkClosed, checkInterrupted, close, detectTrueColorSupport, echo, echo, echoSignal, encoding, enterRawMode, flush, getBooleanCapability, getCurrentMouseTracking, getenv, getGraphemeClusterMode, getKind, getName, getNumericCapability, getPalette, getStatus, getStatus, getStringCapability, getType, hasFocusSupport, hasInBandResizeSupport, hasKittyKeyboardSupport, hasMouseSupport, hasPollSupport, inputEncoding, isClusterGrouped, isModeSupported, outputEncoding, parseInfoCmp, pause, pause, paused, puts, raise, readMouseEvent, readMouseEvent, readMouseEvent, readMouseEvent, resetKittyKeyboardMode, resume, setEnv, setGraphemeClusterMode, setKittyKeyboardMode, setOnClose, trackFocus, trackInBandResize, trackMouse
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.jline.terminal.Terminal
beginSynchronizedUpdate, endSynchronizedUpdate, getBufferSize, getColumns, getHeight, getRows, getWidth, setSize, synchronizedUpdate
-
-
-
-
Field Detail
-
provider
protected final TerminalProvider provider
-
input
protected final NonBlockingInputStream input
-
output
protected final java.io.OutputStream output
-
reader
protected final NonBlockingReader reader
-
writer
protected final java.io.PrintWriter writer
-
nativeHandlers
protected final java.util.Map<Terminal.Signal,java.lang.Object> nativeHandlers
-
closer
protected final ShutdownHooks.Task closer
-
-
Constructor Detail
-
PosixSysTerminal
public PosixSysTerminal(java.lang.String name, java.lang.String type, Pty pty, java.nio.charset.Charset encoding, boolean nativeSignals, Terminal.SignalHandler signalHandler) throws java.io.IOExceptionCreates a POSIX system terminal backed by the provided PTY, using the same charset for input and output, and optionally enabling native signal handling.- Parameters:
name- the terminal nametype- the terminal type (TERM)pty- the pseudo-terminal providing slave input/output streams for the terminalencoding- the charset used for both input and output encodingnativeSignals- if true, native OS signal handlers will be registeredsignalHandler- the initial handler to install for native signals- Throws:
java.io.IOException- if an I/O error occurs while initializing the terminal
-
PosixSysTerminal
public PosixSysTerminal(java.lang.String name, java.lang.String type, Pty pty, java.nio.charset.Charset encoding, java.nio.charset.Charset inputEncoding, java.nio.charset.Charset outputEncoding, boolean nativeSignals, Terminal.SignalHandler signalHandler) throws java.io.IOExceptionCreates a POSIX system terminal backed by the given PTY and character encodings, without a TerminalProvider.- Parameters:
name- the terminal nametype- the terminal type (TERM)pty- the pseudoterminal providing slave input/output streamsencoding- the primary charset for the terminalinputEncoding- the charset used for input decodingoutputEncoding- the charset used for output encodingnativeSignals- whether native signal handlers should be registeredsignalHandler- the initial handler to install for native signals- Throws:
java.io.IOException- if an I/O error occurs while initializing the terminal
-
PosixSysTerminal
public PosixSysTerminal(TerminalProvider provider, java.lang.String name, java.lang.String type, Pty pty, java.nio.charset.Charset encoding, java.nio.charset.Charset inputEncoding, java.nio.charset.Charset outputEncoding, boolean nativeSignals, Terminal.SignalHandler signalHandler) throws java.io.IOException
Create a POSIX system terminal backed by the provided PTY, initializing non-blocking input/output, reader/writer encodings, and optional native signal handlers.- Parameters:
provider- optional TerminalProvider used for native signal registration; may be nullname- the terminal nametype- the terminal type (TERM)pty- the PTY whose slave streams back this terminalencoding- the primary charset for the terminalinputEncoding- the charset used for the terminal readeroutputEncoding- the charset used for the terminal writernativeSignals- if true, register native OS signal handlers for all signalssignalHandler- the initial handler to install for native signals; if `SignalHandler.SIG_DFL`, default native handlers will be registered- Throws:
java.io.IOException- if the PTY streams or terminal I/O cannot be initialized
-
-
Method Detail
-
handle
public Terminal.SignalHandler handle(Terminal.Signal signal, Terminal.SignalHandler handler)
Install a new handler for the given signal and synchronize native registration when the handler changes. If the new handler differs from the previous one, registers a native default handler when the new handler is `SignalHandler.SIG_DFL`; otherwise registers a native handler that will raise the signal.- Specified by:
handlein interfaceTerminal- Overrides:
handlein classAbstractTerminal- Parameters:
signal- the signal to updatehandler- the new handler to install for the signal- Returns:
- the previous handler for the signal
- See Also:
Terminal.Signal,Terminal.SignalHandler,Terminal.raise(Signal)
-
getAttributes
public Attributes getAttributes()
Description copied from interface:TerminalReturns the current terminal attributes.Terminal attributes control various aspects of terminal behavior, including:
- Input processing - How input characters are processed (e.g., character mapping, parity checking)
- Output processing - How output characters are processed (e.g., newline translation)
- Control settings - Hardware settings like baud rate and character size
- Local settings - Terminal behavior settings like echo, canonical mode, and signal generation
- Control characters - Special characters like EOF, interrupt, and erase
The returned
Attributesobject is a copy of the terminal's current attributes and can be safely modified without affecting the terminal until it is applied usingTerminal.setAttributes(Attributes). This allows for making multiple changes to the attributes before applying them all at once.Example usage:
Terminal terminal = TerminalBuilder.terminal(); // Get current attributes Attributes attrs = terminal.getAttributes(); // Modify attributes attrs.setLocalFlag(LocalFlag.ECHO, false); // Disable echo attrs.setInputFlag(InputFlag.ICRNL, false); // Disable CR to NL mapping attrs.setControlChar(ControlChar.VMIN, 1); // Set minimum input to 1 character attrs.setControlChar(ControlChar.VTIME, 0); // Set timeout to 0 deciseconds // Apply modified attributes terminal.setAttributes(attrs);
- Specified by:
getAttributesin interfaceTerminal- Overrides:
getAttributesin classAbstractPosixTerminal- Returns:
- a copy of the terminal's current attributes
- See Also:
Terminal.setAttributes(Attributes),Attributes,Terminal.enterRawMode()
-
setAttributes
public void setAttributes(Attributes attr)
Description copied from interface:TerminalSets the terminal attributes to the specified values.This method applies the specified attributes to the terminal, changing its behavior according to the settings in the
Attributesobject. The terminal makes a copy of the provided attributes, so further modifications to theattrobject will not affect the terminal until this method is called again.Terminal attributes control various aspects of terminal behavior, including input and output processing, control settings, local settings, and special control characters. Changing these attributes allows for fine-grained control over how the terminal processes input and output.
Common attribute modifications include:
- Disabling echo for password input
- Enabling/disabling canonical mode for line-by-line or character-by-character input
- Disabling signal generation for custom handling of Ctrl+C and other control sequences
- Changing control characters like the interrupt character or end-of-file character
For convenience, the
Terminal.enterRawMode()method provides a pre-configured set of attributes suitable for full-screen interactive applications.Example usage:
Terminal terminal = TerminalBuilder.terminal(); // Save original attributes for later restoration Attributes originalAttrs = terminal.getAttributes(); try { // Create and configure new attributes Attributes attrs = new Attributes(originalAttrs); attrs.setLocalFlag(LocalFlag.ECHO, false); // Disable echo for password input attrs.setLocalFlag(LocalFlag.ICANON, false); // Disable canonical mode // Apply the new attributes terminal.setAttributes(attrs); // Use terminal with modified attributes... } finally { // Restore original attributes terminal.setAttributes(originalAttrs); }- Specified by:
setAttributesin interfaceTerminal- Overrides:
setAttributesin classAbstractPosixTerminal- Parameters:
attr- the attributes to apply to the terminal- See Also:
Terminal.getAttributes(),Attributes,Terminal.enterRawMode()
-
supportsGraphemeClusterMode
public boolean supportsGraphemeClusterMode()
Determine if grapheme cluster mode is supported for this terminal.- Specified by:
supportsGraphemeClusterModein interfaceTerminal- Overrides:
supportsGraphemeClusterModein classAbstractTerminal- Returns:
- `true` if grapheme cluster mode is supported, `false` otherwise; on Windows this always returns `false` to avoid writing a DECRQM probe to raw stdout/stderr that may not be a real PTY and could contaminate process output.
- See Also:
Terminal.setGraphemeClusterMode(boolean, boolean),Terminal.getGraphemeClusterMode()
-
reader
public NonBlockingReader reader()
Description copied from interface:TerminalRetrieve theReaderfor 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
-
writer
public java.io.PrintWriter writer()
Description copied from interface:TerminalRetrieve theWriterfor 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
-
input
public java.io.InputStream input()
Description copied from interface:TerminalRetrieve 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 theTerminal.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:
Terminal.reader()
-
output
public java.io.OutputStream output()
Description copied from interface:TerminalRetrieve 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 theTerminal.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:
Terminal.writer()
-
doClose
protected void doClose() throws java.io.IOExceptionCloses the terminal and releases its resources: flushes pending output, removes the shutdown hook, unregisters any native signal handlers, performs superclass shutdown, and closes the reader.- Overrides:
doClosein classAbstractPosixTerminal- Throws:
java.io.IOException- if an I/O error occurs while flushing or closing the terminal streams
-
-