API Overview
JLine provides a comprehensive API for building interactive command-line applications. This section provides an overview of the key interfaces and classes in JLine.
Core Components
JLine's API is organized around several core components:
Terminal
The Terminal interface represents the terminal device. It provides methods for:
- Reading input
- Writing output
- Querying terminal capabilities
- Handling signals
- Managing terminal attributes
Key classes:
Terminal: The main interfaceTerminalBuilder: Factory for creating terminalsAttributes: Terminal attributes (raw mode, echo, etc.)Size: Terminal dimensions
LineReader
The LineReader interface provides line editing capabilities. It handles:
- Reading lines with editing
- History management
- Completion
- Syntax highlighting
- Key binding
Key classes:
LineReader: The main interfaceLineReaderBuilder: Factory for creating line readersParsedLine: Parsed command lineCandidate: Completion candidate
Completer
The Completer interface provides tab completion. Key classes:
Completer: The main interfaceStringsCompleter: Completes from a list of stringsFileNameCompleter: Completes file namesArgumentCompleter: Completes command argumentsTreeCompleter: Hierarchical completion
History
The History interface manages command history. Key classes:
History: The main interfaceDefaultHistory: Standard history implementationMemoryHistory: In-memory historyFileHistory: File-based history
Parser
The Parser interface parses command lines. Key classes:
Parser: The main interfaceDefaultParser: Standard parser implementationParsedLine: Parsed command line
Highlighter
The Highlighter interface provides syntax highlighting. Key classes:
Highlighter: The main interfaceDefaultHighlighter: Standard highlighter implementation
Utility Classes
JLine provides several utility classes:
AttributedString
AttributedString represents styled text with ANSI colors and attributes:
AttributedString: Immutable styled stringAttributedStringBuilder: Builder for creating attributed stringsAttributedStyle: Style attributes (color, bold, etc.)
Display
Display manages the terminal display:
Display: Terminal display managerInfoCmp: Terminal capability databaseCurses: Terminal control sequences
Package Structure
JLine is organized into several packages:
org.jline.terminal: Terminal handlingorg.jline.reader: Line reading and editingorg.jline.reader.impl: Implementation classesorg.jline.reader.impl.completer: Completion implementationsorg.jline.reader.impl.history: History implementationsorg.jline.utils: Utility classesorg.jline.builtins: Built-in commands and widgets
API Stability
JLine follows semantic versioning:
- Major version changes may include breaking API changes
- Minor version changes add functionality in a backward-compatible manner
- Patch version changes include backward-compatible bug fixes
Classes and methods marked with @Deprecated may be removed in future major versions.
Thread Safety
Most JLine classes are not thread-safe. In particular:
Terminalmethods should be called from a single threadLineReadermethods should be called from a single threadHistorycan be accessed concurrently if properly synchronized
Error Handling
JLine uses exceptions to indicate errors:
IOError: I/O-related errorsUserInterruptException: User pressed Ctrl+CEndOfFileException: End of input (Ctrl+D)SyntaxError: Syntax error in input
Further Reading
For detailed API documentation, refer to the JavaDoc for each class. The JLine GitHub repository also includes examples demonstrating various aspects of the API.