Interface Terminal

All Superinterfaces:
AutoCloseable, Closeable, Flushable
All Known Subinterfaces:
TerminalExt
All Known Implementing Classes:
AbstractPosixTerminal, AbstractTerminal, AbstractWindowsTerminal, DumbTerminal, ExternalTerminal, LineDisciplineTerminal, NativeWinSysTerminal, PosixPtyTerminal, PosixSysTerminal

public interface Terminal extends Closeable, Flushable
A terminal representing a virtual terminal on the computer.

The Terminal interface is the central abstraction in JLine, providing access to the terminal's capabilities, input/output streams, and control functions. It abstracts the differences between various terminal types and operating systems, allowing applications to work consistently across environments.

Terminal Capabilities

Terminals provide access to their capabilities through the getStringCapability(Capability), getBooleanCapability(Capability), and getNumericCapability(Capability) methods. These capabilities represent the terminal's features and are defined in the terminfo database.

Input and Output

Terminal input can be read using the reader() method, which returns a non-blocking reader. Output can be written using the writer() method, which returns a print writer. For raw access to the underlying streams, use input() and output().

Terminal Attributes

Terminal attributes control the behavior of the terminal, such as echo mode, canonical mode, etc. These can be accessed and modified using getAttributes() and setAttributes(Attributes).

Signal Handling

Terminals can handle various signals, such as CTRL+C (INT), CTRL+\ (QUIT), etc. Signal handlers can be registered using handle(Signal, SignalHandler).

Signal handling allows terminal applications to respond appropriately to these events, such as gracefully terminating when the user presses Ctrl+C, or adjusting the display when the terminal window is resized.

Example usage:

 Terminal terminal = TerminalBuilder.terminal();

 // Handle interrupt signal (Ctrl+C)
 terminal.handle(Signal.INT, signal -> {
     terminal.writer().println("\nInterrupted! Press Enter to exit.");
     terminal.flush();
 });
 

Mouse Support

Some terminals support mouse tracking, which can be enabled using trackMouse(MouseTracking). Mouse events can then be read using readMouseEvent().

Lifecycle

Terminals should be closed by calling the Closeable.close() method when they are no longer needed in order to restore their original state. Failure to close a terminal may leave the terminal in an inconsistent state.

Creating Terminals

Terminals are typically created using the TerminalBuilder class, which provides a fluent API for configuring and creating terminal instances.

See Also: