Class AbstractPosixTerminal
- All Implemented Interfaces:
Closeable
,Flushable
,AutoCloseable
,TerminalExt
,Terminal
- Direct Known Subclasses:
PosixPtyTerminal
,PosixSysTerminal
The AbstractPosixTerminal class provides a foundation for terminal implementations on POSIX-compliant systems such as Linux, macOS, and other Unix-like operating systems. It builds on the AbstractTerminal class and adds POSIX-specific functionality, particularly related to pseudoterminal (PTY) handling.
This class manages the interaction with the underlying PTY, handling terminal attributes, size changes, and other POSIX-specific terminal operations. It provides implementations for many of the abstract methods defined in AbstractTerminal, leaving only a few methods to be implemented by concrete subclasses.
Key features provided by this class include:
- PTY management and interaction
- Terminal attribute preservation and restoration
- Size handling and window change signals
- Cursor position detection
This class is designed to be extended by concrete implementations that target specific POSIX platforms or environments.
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from interface org.jline.terminal.Terminal
Terminal.MouseTracking, Terminal.Signal, Terminal.SignalHandler
-
Field Summary
FieldsFields inherited from class org.jline.terminal.impl.AbstractTerminal
bools, currentMouseTracking, encoding, handlers, ints, name, onClose, palette, status, stderrEncoding, stdinEncoding, stdoutEncoding, strings, type
Fields inherited from interface org.jline.terminal.Terminal
TYPE_DUMB, TYPE_DUMB_COLOR
-
Constructor Summary
ConstructorsConstructorDescriptionAbstractPosixTerminal
(String name, String type, Pty pty) AbstractPosixTerminal
(String name, String type, Pty pty, Charset encoding, Charset stdinEncoding, Charset stdoutEncoding, Charset stderrEncoding, Terminal.SignalHandler signalHandler) AbstractPosixTerminal
(String name, String type, Pty pty, Charset encoding, Terminal.SignalHandler signalHandler) -
Method Summary
Modifier and TypeMethodDescriptionprotected void
doClose()
Returns the current terminal attributes.getCursorPosition
(IntConsumer discarded) Query the terminal to report the cursor position.int
Get the terminal's default background color.int
Get the terminal's default foreground color.Returns the terminal provider that created this terminal.getPty()
getSize()
Retrieve the size of the visible windowReturns the system stream associated with this terminal, if any.void
setAttributes
(Attributes attr) Sets the terminal attributes to the specified values.void
Sets the size of the terminal.toString()
Methods inherited from class org.jline.terminal.impl.AbstractTerminal
canPauseResume, checkInterrupted, close, echo, echo, echoSignal, encoding, enterRawMode, flush, getBooleanCapability, getCurrentMouseTracking, getKind, getName, getNumericCapability, getPalette, getStatus, getStatus, getStringCapability, getType, handle, hasFocusSupport, hasMouseSupport, parseInfoCmp, pause, pause, paused, puts, raise, readMouseEvent, readMouseEvent, readMouseEvent, readMouseEvent, resume, setOnClose, stderrEncoding, stdinEncoding, stdoutEncoding, trackFocus, trackMouse
-
Field Details
-
pty
-
originalAttributes
-
-
Constructor Details
-
AbstractPosixTerminal
- Throws:
IOException
-
AbstractPosixTerminal
public AbstractPosixTerminal(String name, String type, Pty pty, Charset encoding, Terminal.SignalHandler signalHandler) throws IOException - Throws:
IOException
-
AbstractPosixTerminal
public AbstractPosixTerminal(String name, String type, Pty pty, Charset encoding, Charset stdinEncoding, Charset stdoutEncoding, Charset stderrEncoding, Terminal.SignalHandler signalHandler) throws IOException - Throws:
IOException
-
-
Method Details
-
getPty
-
getAttributes
Description copied from interface:Terminal
Returns 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
Attributes
object 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);
- Returns:
- a copy of the terminal's current attributes
- See Also:
-
setAttributes
Description copied from interface:Terminal
Sets 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
Attributes
object. The terminal makes a copy of the provided attributes, so further modifications to theattr
object 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); }
- Parameters:
attr
- the attributes to apply to the terminal- See Also:
-
getSize
Description copied from interface:Terminal
Retrieve the size of the visible window- Returns:
- the visible terminal size
- See Also:
-
setSize
Description copied from interface:Terminal
Sets the size of the terminal.This method attempts to resize the terminal to the specified dimensions. Note that not all terminals support resizing, and the actual size after this operation may differ from the requested size depending on terminal capabilities and constraints.
For virtual terminals or terminal emulators, this may update the internal size representation. For physical terminals, this may send appropriate escape sequences to adjust the viewable area.
- Parameters:
size
- the new terminal size (columns and rows)- See Also:
-
doClose
- Overrides:
doClose
in classAbstractTerminal
- Throws:
IOException
-
getCursorPosition
Description copied from interface:Terminal
Query the terminal to report the cursor position. As the response is read from the input stream, some characters may be read before the cursor position is actually read. Those characters can be given back usingorg.jline.keymap.BindingReader#runMacro(String)
- Specified by:
getCursorPosition
in interfaceTerminal
- Overrides:
getCursorPosition
in classAbstractTerminal
- Parameters:
discarded
- a consumer receiving discarded characters- Returns:
null
if cursor position reporting is not supported or a valid cursor position
-
getProvider
Description copied from interface:TerminalExt
Returns the terminal provider that created this terminal.The terminal provider is responsible for creating and managing terminal instances on a specific platform. This method allows access to the provider that created this terminal, which can be useful for accessing provider-specific functionality or for creating additional terminals with the same provider.
- Returns:
- the
TerminalProvider
that created this terminal, ornull
if the terminal was created with no provider - See Also:
-
getSystemStream
Description copied from interface:TerminalExt
Returns the system stream associated with this terminal, if any.This method indicates whether the terminal is bound to a standard system stream (standard input, standard output, or standard error). Terminals that are connected to system streams typically represent the actual terminal window or console that the application is running in.
- Returns:
- the underlying system stream, which may be
SystemStream.Input
,SystemStream.Output
,SystemStream.Error
, ornull
if this terminal is not bound to a system stream - See Also:
-
toString
- Overrides:
toString
in classAbstractTerminal
-
getDefaultForegroundColor
public int getDefaultForegroundColor()Description copied from class:AbstractTerminal
Get the terminal's default foreground color. This method should be overridden by concrete implementations.- Specified by:
getDefaultForegroundColor
in interfaceTerminal
- Overrides:
getDefaultForegroundColor
in classAbstractTerminal
- Returns:
- the RGB value of the default foreground color, or -1 if not available
- See Also:
-
getDefaultBackgroundColor
public int getDefaultBackgroundColor()Description copied from class:AbstractTerminal
Get the terminal's default background color. This method should be overridden by concrete implementations.- Specified by:
getDefaultBackgroundColor
in interfaceTerminal
- Overrides:
getDefaultBackgroundColor
in classAbstractTerminal
- Returns:
- the RGB value of the default background color, or -1 if not available
- See Also:
-