|
| 1 | +--- |
| 2 | +sidebar_position: 2 |
| 3 | +--- |
| 4 | + |
| 5 | +# JLine Architecture |
| 6 | + |
| 7 | +This page provides a high-level overview of JLine's architecture and how its components interact with each other. |
| 8 | + |
| 9 | +## Component Overview |
| 10 | + |
| 11 | +JLine is organized into several core components that work together to provide a complete terminal handling and line editing solution: |
| 12 | + |
| 13 | +``` |
| 14 | +┌─────────────────────────────────────────────────────────────────┐ |
| 15 | +│ JLine Architecture │ |
| 16 | +└─────────────────────────────────────────────────────────────────┘ |
| 17 | + │ |
| 18 | + ▼ |
| 19 | +┌─────────────────────────────────────────────────────────────────┐ |
| 20 | +│ Terminal │ |
| 21 | +│ │ |
| 22 | +│ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │ |
| 23 | +│ │ Jansi │ │ JNA │ │ FFM │ │ Exec │ │ |
| 24 | +│ │ Provider │ │ Provider │ │ Provider │ │ Provider│ │ |
| 25 | +│ └─────────────┘ └─────────────┘ └─────────────┘ └─────────┘ │ |
| 26 | +└─────────────────────────────────────────────────────────────────┘ |
| 27 | + │ |
| 28 | + ▼ |
| 29 | +┌─────────────────────────────────────────────────────────────────┐ |
| 30 | +│ LineReader │ |
| 31 | +│ │ |
| 32 | +│ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ ┌─────────┐ │ |
| 33 | +│ │ Parser │ │ Completer │ │ History │ │ Widgets │ │ |
| 34 | +│ └─────────────┘ └─────────────┘ └─────────────┘ └─────────┘ │ |
| 35 | +└─────────────────────────────────────────────────────────────────┘ |
| 36 | + │ |
| 37 | + ▼ |
| 38 | +┌─────────────────────────────────────────────────────────────────┐ |
| 39 | +│ Higher-Level APIs │ |
| 40 | +│ │ |
| 41 | +│ ┌─────────────┐ ┌─────────────┐ ┌────────────┐ ┌──────────┐ │ |
| 42 | +│ │ Style │ │ Builtins │ │ Console │ │Console UI│ │ |
| 43 | +│ └─────────────┘ └─────────────┘ └────────────┘ └──────────┘ │ |
| 44 | +└─────────────────────────────────────────────────────────────────┘ |
| 45 | +``` |
| 46 | + |
| 47 | +## Core Components |
| 48 | + |
| 49 | +### Terminal |
| 50 | + |
| 51 | +The `Terminal` component is the foundation of JLine. It provides: |
| 52 | + |
| 53 | +- Access to the underlying terminal device |
| 54 | +- Raw mode for character-by-character input |
| 55 | +- ANSI escape sequence handling |
| 56 | +- Terminal size information |
| 57 | +- Signal handling (e.g., window resize, Ctrl+C) |
| 58 | + |
| 59 | +The Terminal layer uses different providers (Jansi, JNA, FFM, etc.) to interact with the native terminal capabilities on different platforms. |
| 60 | + |
| 61 | +### LineReader |
| 62 | + |
| 63 | +The `LineReader` builds on top of the Terminal to provide: |
| 64 | + |
| 65 | +- Line editing capabilities |
| 66 | +- History management |
| 67 | +- Tab completion |
| 68 | +- Key binding |
| 69 | +- Widget system for custom functionality |
| 70 | +- Multi-line editing |
| 71 | +- Syntax highlighting |
| 72 | + |
| 73 | +### Higher-Level APIs |
| 74 | + |
| 75 | +JLine includes several higher-level modules that provide additional functionality: |
| 76 | + |
| 77 | +- **Style**: Styling API for terminal output |
| 78 | +- **Builtins**: Ready-to-use commands and utilities |
| 79 | +- **Console**: Framework for building interactive console applications |
| 80 | +- **Console UI**: UI components like progress bars, tables, and forms |
| 81 | + |
| 82 | +## Data Flow |
| 83 | + |
| 84 | +Here's how data flows through the JLine system: |
| 85 | + |
| 86 | +1. The `Terminal` captures raw input from the user |
| 87 | +2. Input is processed through key bindings and widgets |
| 88 | +3. The `LineReader` applies editing operations based on the input |
| 89 | +4. When the user presses Enter, the `LineReader` returns the completed line |
| 90 | +5. The application processes the line and may use higher-level APIs for output |
| 91 | + |
| 92 | +## Module Dependencies |
| 93 | + |
| 94 | +The modules have the following dependency relationships: |
| 95 | + |
| 96 | +``` |
| 97 | +jline-terminal |
| 98 | + ↑ |
| 99 | +jline-reader |
| 100 | + ↑ |
| 101 | +jline-style |
| 102 | + ↑ |
| 103 | +jline-builtins |
| 104 | + ↑ |
| 105 | +jline-console |
| 106 | + ↑ |
| 107 | +jline-console-ui |
| 108 | +``` |
| 109 | + |
| 110 | +## Key Interfaces |
| 111 | + |
| 112 | +JLine defines several key interfaces that you'll work with: |
| 113 | + |
| 114 | +- `Terminal`: Represents the terminal device |
| 115 | +- `LineReader`: Reads lines of input with editing capabilities |
| 116 | +- `Completer`: Provides tab completion suggestions |
| 117 | +- `History`: Manages command history |
| 118 | +- `Parser`: Parses input lines into tokens |
| 119 | +- `Widget`: Implements custom functionality for the line reader |
| 120 | + |
| 121 | +## Customization Points |
| 122 | + |
| 123 | +JLine is highly customizable through several extension points: |
| 124 | + |
| 125 | +- **Terminal Providers**: Choose or implement different terminal backends |
| 126 | +- **Completers**: Create custom completion logic |
| 127 | +- **Widgets**: Add new editing functions |
| 128 | +- **Key Bindings**: Map keys to specific actions |
| 129 | +- **Highlighters**: Implement syntax highlighting |
| 130 | +- **History**: Customize history storage and retrieval |
| 131 | + |
| 132 | +## Common Usage Patterns |
| 133 | + |
| 134 | +### Basic Terminal and LineReader |
| 135 | + |
| 136 | +```java |
| 137 | +// Create a terminal |
| 138 | +Terminal terminal = TerminalBuilder.builder() |
| 139 | + .system(true) |
| 140 | + .build(); |
| 141 | + |
| 142 | +// Create a line reader |
| 143 | +LineReader reader = LineReaderBuilder.builder() |
| 144 | + .terminal(terminal) |
| 145 | + .build(); |
| 146 | + |
| 147 | +// Read input |
| 148 | +String line = reader.readLine("prompt> "); |
| 149 | +``` |
| 150 | + |
| 151 | +### Adding Tab Completion |
| 152 | + |
| 153 | +```java |
| 154 | +// Create a completer |
| 155 | +Completer completer = new StringsCompleter("command1", "command2", "help", "quit"); |
| 156 | + |
| 157 | +// Create a line reader with completion |
| 158 | +LineReader reader = LineReaderBuilder.builder() |
| 159 | + .terminal(terminal) |
| 160 | + .completer(completer) |
| 161 | + .build(); |
| 162 | +``` |
| 163 | + |
| 164 | +### Using History |
| 165 | + |
| 166 | +```java |
| 167 | +// Create a history file |
| 168 | +Path historyFile = Paths.get(System.getProperty("user.home"), ".myapp_history"); |
| 169 | + |
| 170 | +// Create a line reader with history |
| 171 | +LineReader reader = LineReaderBuilder.builder() |
| 172 | + .terminal(terminal) |
| 173 | + .variable(LineReader.HISTORY_FILE, historyFile) |
| 174 | + .build(); |
| 175 | +``` |
| 176 | + |
| 177 | +## Conclusion |
| 178 | + |
| 179 | +JLine's architecture provides a flexible and powerful foundation for building command-line applications. By understanding how the components interact, you can leverage JLine's capabilities to create sophisticated terminal interfaces. |
| 180 | + |
| 181 | +For more detailed information about each component, refer to the specific documentation pages. |
0 commit comments