Class LineDisciplineTerminal
- java.lang.Object
-
- org.jline.terminal.impl.AbstractTerminal
-
- org.jline.terminal.impl.LineDisciplineTerminal
-
- All Implemented Interfaces:
java.io.Closeable
,java.io.Flushable
,java.lang.AutoCloseable
,TerminalExt
,Terminal
- Direct Known Subclasses:
ExternalTerminal
public class LineDisciplineTerminal extends AbstractTerminal
Abstract terminal with support for line discipline. TheTerminal
interface represents the slave side of a PTY, but implementations derived from this class will handle both the slave and master side of things. In order to correctly handle line discipline, the terminal needs to read the input in advance in order to raise the signals as fast as possible. For example, when the user hits Ctrl+C, we can't wait until the application consumes all the read events. The same applies to echoing, when enabled, as the echoing has to happen as soon as the user hit the keyboard, and not only when the application running in the terminal processes the input.The LineDisciplineTerminal class provides a terminal implementation that emulates the line discipline functionality typically provided by the operating system's terminal driver. Line discipline refers to the processing of input and output characters according to various modes and settings, such as canonical mode, echo, and special character handling.
This terminal implementation is particularly useful in environments where:
- The underlying system does not provide native terminal capabilities
- The application needs precise control over terminal behavior
- The terminal is being used in a non-standard environment (e.g., embedded systems)
Key features of this implementation include:
- Emulation of canonical and non-canonical input modes
- Support for character echoing
- Special character handling (e.g., interrupt, erase, kill)
- Input and output processing according to terminal attributes
This terminal implementation works with any input and output streams, making it highly flexible and adaptable to various environments.
- See Also:
Attributes
,AbstractTerminal
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.jline.terminal.Terminal
Terminal.MouseTracking, Terminal.Signal, Terminal.SignalHandler
-
-
Field Summary
Fields Modifier and Type Field Description protected Attributes
attributes
Console dataprotected java.io.OutputStream
masterOutput
protected Size
size
protected boolean
skipNextLf
protected NonBlockingPumpInputStream
slaveInput
protected java.io.OutputStream
slaveInputPipe
protected java.io.OutputStream
slaveOutput
protected NonBlockingReader
slaveReader
protected java.io.PrintWriter
slaveWriter
-
Fields 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
Constructors Constructor Description LineDisciplineTerminal(java.lang.String name, java.lang.String type, java.io.OutputStream masterOutput, java.nio.charset.Charset encoding)
LineDisciplineTerminal(java.lang.String name, java.lang.String type, java.io.OutputStream masterOutput, java.nio.charset.Charset encoding, java.nio.charset.Charset stdinEncoding, java.nio.charset.Charset stdoutEncoding, java.nio.charset.Charset stderrEncoding, Terminal.SignalHandler signalHandler)
LineDisciplineTerminal(java.lang.String name, java.lang.String type, java.io.OutputStream masterOutput, java.nio.charset.Charset encoding, Terminal.SignalHandler signalHandler)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
doClose()
protected boolean
doProcessInputByte(int c)
Attributes
getAttributes()
Returns the current terminal attributes.TerminalProvider
getProvider()
Returns the terminal provider that created this terminal.Size
getSize()
Retrieve the size of the visible windowSystemStream
getSystemStream()
Returns the system stream associated with this terminal, if any.java.io.InputStream
input()
Retrieve the input stream for this terminal.java.io.OutputStream
output()
Retrieve the output stream for this terminal.void
processInputByte(int c)
Master input processing.void
processInputBytes(byte[] input)
void
processInputBytes(byte[] input, int offset, int length)
protected void
processIOException(java.io.IOException ioException)
protected void
processOutputByte(int c)
Master output processing.void
raise(Terminal.Signal signal)
Raises the specified signal, triggering any registered handlers.NonBlockingReader
reader()
Retrieve theReader
for this terminal.void
setAttributes(Attributes attr)
Sets the terminal attributes to the specified values.void
setSize(Size sz)
Sets the size of the terminal.java.io.PrintWriter
writer()
Retrieve theWriter
for this terminal.-
Methods inherited from class org.jline.terminal.impl.AbstractTerminal
canPauseResume, checkInterrupted, close, echo, echo, echoSignal, encoding, enterRawMode, flush, getBooleanCapability, getCurrentMouseTracking, getCursorPosition, getDefaultBackgroundColor, getDefaultForegroundColor, getKind, getName, getNumericCapability, getPalette, getStatus, getStatus, getStringCapability, getType, handle, hasFocusSupport, hasMouseSupport, parseInfoCmp, pause, pause, paused, puts, readMouseEvent, readMouseEvent, readMouseEvent, readMouseEvent, resume, setOnClose, stderrEncoding, stdinEncoding, stdoutEncoding, toString, trackFocus, 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
getBufferSize, getHeight, getWidth
-
-
-
-
Field Detail
-
masterOutput
protected final java.io.OutputStream masterOutput
-
slaveInputPipe
protected final java.io.OutputStream slaveInputPipe
-
slaveInput
protected final NonBlockingPumpInputStream slaveInput
-
slaveReader
protected final NonBlockingReader slaveReader
-
slaveWriter
protected final java.io.PrintWriter slaveWriter
-
slaveOutput
protected final java.io.OutputStream slaveOutput
-
attributes
protected final Attributes attributes
Console data
-
size
protected final Size size
-
skipNextLf
protected boolean skipNextLf
-
-
Constructor Detail
-
LineDisciplineTerminal
public LineDisciplineTerminal(java.lang.String name, java.lang.String type, java.io.OutputStream masterOutput, java.nio.charset.Charset encoding) throws java.io.IOException
- Throws:
java.io.IOException
-
LineDisciplineTerminal
public LineDisciplineTerminal(java.lang.String name, java.lang.String type, java.io.OutputStream masterOutput, java.nio.charset.Charset encoding, Terminal.SignalHandler signalHandler) throws java.io.IOException
- Throws:
java.io.IOException
-
LineDisciplineTerminal
public LineDisciplineTerminal(java.lang.String name, java.lang.String type, java.io.OutputStream masterOutput, java.nio.charset.Charset encoding, java.nio.charset.Charset stdinEncoding, java.nio.charset.Charset stdoutEncoding, java.nio.charset.Charset stderrEncoding, Terminal.SignalHandler signalHandler) throws java.io.IOException
- Throws:
java.io.IOException
-
-
Method Detail
-
reader
public NonBlockingReader reader()
Description copied from interface:Terminal
Retrieve theReader
for this terminal. This is the standard way to read input from this terminal. The reader is non blocking.- Returns:
- The non blocking reader
-
writer
public java.io.PrintWriter writer()
Description copied from interface:Terminal
Retrieve theWriter
for this terminal. This is the standard way to write to this terminal.- Returns:
- The writer
-
input
public java.io.InputStream input()
Description copied from interface:Terminal
Retrieve 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.- Returns:
- The input stream
- See Also:
Terminal.reader()
-
output
public java.io.OutputStream output()
Description copied from interface:Terminal
Retrieve 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.- Returns:
- The output stream
- See Also:
Terminal.writer()
-
getAttributes
public Attributes 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:
Terminal.setAttributes(Attributes)
,Attributes
,Terminal.enterRawMode()
-
setAttributes
public void setAttributes(Attributes attr)
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:
Terminal.getAttributes()
,Attributes
,Terminal.enterRawMode()
-
getSize
public Size getSize()
Description copied from interface:Terminal
Retrieve the size of the visible window- Returns:
- the visible terminal size
- See Also:
Terminal.getBufferSize()
-
setSize
public void setSize(Size sz)
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:
sz
- the new terminal size (columns and rows)- See Also:
Terminal.getSize()
-
raise
public void raise(Terminal.Signal signal)
Description copied from interface:Terminal
Raises the specified signal, triggering any registered handlers.This method manually triggers a signal, causing any registered handler for that signal to be called. This is typically not a method that application code would call directly, but is used internally by terminal implementations.
When accessing a terminal through an SSH or Telnet connection, signals may be conveyed by the protocol and need to be raised when they reach the terminal code. Terminal implementations automatically raise signals when the input stream receives characters mapped to special control characters:
Attributes.ControlChar.VINTR
(typically Ctrl+C) - RaisesTerminal.Signal.INT
Attributes.ControlChar.VQUIT
(typically Ctrl+\) - RaisesTerminal.Signal.QUIT
Attributes.ControlChar.VSUSP
(typically Ctrl+Z) - RaisesTerminal.Signal.TSTP
In some cases, application code might want to programmatically raise signals to trigger specific behaviors, such as simulating a window resize event by raising
Terminal.Signal.WINCH
.- Specified by:
raise
in interfaceTerminal
- Overrides:
raise
in classAbstractTerminal
- Parameters:
signal
- the signal to raise- See Also:
Terminal.Signal
,Terminal.handle(Signal, SignalHandler)
,Attributes.ControlChar
-
processInputByte
public void processInputByte(int c) throws java.io.IOException
Master input processing. All data coming to the terminal should be provided using this method.- Parameters:
c
- the input byte- Throws:
java.io.IOException
- if anything wrong happens
-
processInputBytes
public void processInputBytes(byte[] input) throws java.io.IOException
- Throws:
java.io.IOException
-
processInputBytes
public void processInputBytes(byte[] input, int offset, int length) throws java.io.IOException
- Throws:
java.io.IOException
-
doProcessInputByte
protected boolean doProcessInputByte(int c) throws java.io.IOException
- Throws:
java.io.IOException
-
processOutputByte
protected void processOutputByte(int c) throws java.io.IOException
Master output processing. All data going to the master should be provided by this method.- Parameters:
c
- the output byte- Throws:
java.io.IOException
- if anything wrong happens
-
processIOException
protected void processIOException(java.io.IOException ioException)
-
doClose
protected void doClose() throws java.io.IOException
- Overrides:
doClose
in classAbstractTerminal
- Throws:
java.io.IOException
-
getProvider
public TerminalProvider 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:
TerminalProvider
-
getSystemStream
public SystemStream 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:
SystemStream
-
-