Class AbstractUnixSysTerminal
- All Implemented Interfaces:
Closeable,Flushable,AutoCloseable,Sized,TerminalExt,Terminal
- Direct Known Subclasses:
FfmUnixSysTerminal,JniUnixSysTerminal
Subclasses only need to implement four methods for platform-specific attribute and size operations:
The call chain is reduced from 7 layers to 4:
Terminal → AbstractTerminal → AbstractUnixSysTerminal → subclass → native call
Important: the underlying system streams (FileDescriptor.in,
FileDescriptor.out/err) are wrapped in NonCloseableInputStream /
NonCloseableOutputStream. Closing the terminal will shut down the pump thread and
release resources, but will not close the shared file descriptors. This prevents
breaking System.in/System.out for the rest of the JVM.
-
Nested Class Summary
Nested classes/interfaces inherited from interface org.jline.terminal.Terminal
Terminal.MouseTracking, Terminal.Signal, Terminal.SignalHandler -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final intprotected static final intprotected static final intFields inherited from class org.jline.terminal.impl.AbstractTerminal
bools, closed, currentMouseTracking, encoding, handlers, inputEncoding, ints, name, onClose, outputEncoding, palette, status, strings, typeFields inherited from interface org.jline.terminal.Terminal
TYPE_DUMB, TYPE_DUMB_COLOR -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedAbstractUnixSysTerminal(TerminalProvider provider, SystemStream systemStream, String name, String type, Charset encoding, Charset inputEncoding, Charset outputEncoding, boolean nativeSignals, Terminal.SignalHandler signalHandler, Attributes originalAttributes) -
Method Summary
Modifier and TypeMethodDescriptionprotected voiddoClose()protected abstract Attributesprotected abstract Sizeprotected abstract voiddoSetAttributes(Attributes attr) protected abstract voidReturns the current terminal attributes.getCursorPosition(IntConsumer discarded) Query the terminal to report the cursor position.intGet the terminal's default background color.intGet the terminal's default foreground color.Returns the terminal provider that created this terminal.getSize()Retrieve the size of the visible windowReturns the system stream associated with this terminal, if any.handle(Terminal.Signal signal, Terminal.SignalHandler handler) Registers a handler for the givenTerminal.Signal.input()Retrieve the input stream for this terminal.output()Retrieve the output stream for this terminal.reader()Retrieve theReaderfor this terminal.voidsetAttributes(Attributes attr) Sets the terminal attributes to the specified values.voidRequests that the terminal adopt the specified window dimensions.toString()writer()Retrieve theWriterfor this terminal.Methods inherited from class org.jline.terminal.impl.AbstractTerminal
canPauseResume, checkClosed, checkInterrupted, close, detectTrueColorSupport, echo, echo, echoSignal, encoding, enterRawMode, flush, getBooleanCapability, getCurrentMouseTracking, getGraphemeClusterMode, getKind, getName, getNumericCapability, getPalette, getStatus, getStatus, getStringCapability, getType, hasFocusSupport, hasMouseSupport, inputEncoding, isClusterGrouped, outputEncoding, parseInfoCmp, pause, pause, paused, puts, raise, readMouseEvent, readMouseEvent, readMouseEvent, readMouseEvent, resume, setGraphemeClusterMode, setOnClose, supportsGraphemeClusterMode, trackFocus, trackMouseMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface org.jline.terminal.Terminal
getBufferSize, getColumns, getHeight, getRows, getWidth, setSize
-
Field Details
-
STDIN_FD
protected static final int STDIN_FD- See Also:
-
STDOUT_FD
protected static final int STDOUT_FD- See Also:
-
STDERR_FD
protected static final int STDERR_FD- See Also:
-
-
Constructor Details
-
AbstractUnixSysTerminal
protected AbstractUnixSysTerminal(TerminalProvider provider, SystemStream systemStream, String name, String type, Charset encoding, Charset inputEncoding, Charset outputEncoding, boolean nativeSignals, Terminal.SignalHandler signalHandler, Attributes originalAttributes) throws IOException - Throws:
IOException
-
-
Method Details
-
handle
Description copied from interface:TerminalRegisters a handler for the givenTerminal.Signal.This method allows the application to specify custom behavior when a particular signal is raised. The handler's
Terminal.SignalHandler.handle(Signal)method will be called whenever the specified signal is raised.Note that the JVM does not easily allow catching the
Terminal.Signal.QUITsignal (Ctrl+\), which typically causes a thread dump to be displayed. This signal handling is mainly effective when connecting through an SSH socket to a virtual terminal.Example usage:
Terminal terminal = TerminalBuilder.terminal(); // Handle window resize events terminal.handle(Signal.WINCH, signal -> { Size size = terminal.getSize(); terminal.writer().println("\nTerminal resized to " + size.getColumns() + "x" + size.getRows()); terminal.flush(); }); // Ignore interrupt signal terminal.handle(Signal.INT, SignalHandler.SIG_IGN);- Specified by:
handlein interfaceTerminal- Overrides:
handlein classAbstractTerminal- Parameters:
signal- the signal to register a handler forhandler- the handler to be called when the signal is raised- Returns:
- the previous signal handler that was registered for this signal
- See Also:
-
doGetAttributes
-
doSetAttributes
-
doGetSize
-
doSetSize
-
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);
- Returns:
- a copy of the terminal's current attributes
- See Also:
-
setAttributes
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); }- Parameters:
attr- the attributes to apply to the terminal- See Also:
-
getSize
-
setSize
Description copied from interface:TerminalRequests that the terminal adopt the specified window dimensions. Implementations may apply constraints or ignore the request if resizing is unsupported; callers should useTerminal.getSize()to observe the effective size after calling this method.- Parameters:
size- the desired terminal size (columns and rows)- See Also:
-
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
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
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:
-
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:
-
getProvider
Description copied from interface:TerminalExtReturns 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
TerminalProviderthat created this terminal, ornullif the terminal was created with no provider - See Also:
-
getSystemStream
Description copied from interface:TerminalExtReturns 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, ornullif this terminal is not bound to a system stream - See Also:
-
getCursorPosition
Description copied from interface:TerminalQuery 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:
getCursorPositionin interfaceTerminal- Overrides:
getCursorPositionin classAbstractTerminal- Parameters:
discarded- a consumer receiving discarded characters- Returns:
nullif cursor position reporting is not supported or a valid cursor position
-
doClose
- Overrides:
doClosein classAbstractTerminal- Throws:
IOException
-
getDefaultForegroundColor
public int getDefaultForegroundColor()Description copied from class:AbstractTerminalGet the terminal's default foreground color. This method should be overridden by concrete implementations.- Specified by:
getDefaultForegroundColorin interfaceTerminal- Overrides:
getDefaultForegroundColorin 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:AbstractTerminalGet the terminal's default background color. This method should be overridden by concrete implementations.- Specified by:
getDefaultBackgroundColorin interfaceTerminal- Overrides:
getDefaultBackgroundColorin classAbstractTerminal- Returns:
- the RGB value of the default background color, or -1 if not available
- See Also:
-
toString
- Overrides:
toStringin classAbstractTerminal
-