Module org.jline.prompt
Package org.jline.prompt
package org.jline.prompt
JLine Prompt API - Interactive Console Prompts for Java Applications.
Overview
The JLine Prompt API provides a modern, type-safe interface for creating interactive console prompts. Inspired by Inquirer.js, it offers a fluent builder pattern for constructing various types of user input prompts with full support for keyboard navigation, validation, and customization.
Supported Prompt Types
The API supports the following prompt types:
- Input Prompts - Text input with optional masking, completion, and validation
- List Prompts - Single selection from a list of options with keyboard navigation
- Checkbox Prompts - Multiple selection from a list with checkboxes
- Choice Prompts - Single selection with keyboard shortcuts and help text
- Confirmation Prompts - Yes/No confirmation dialogs
- Text Displays - Static text display without user input
Key Features
- Type Safety - Parameterized interfaces ensure compile-time type checking
- Fluent API - Builder pattern for intuitive prompt construction
- Cross-Platform - Automatic platform-specific UI configuration
- Extensible - Interface-based design allows custom implementations
- Immutable - Thread-safe prompt and result objects
Basic Usage
// Create a Prompter instance
Terminal terminal = TerminalBuilder.builder().build();
Prompter prompter = PrompterFactory.create(terminal);
// Build prompts using the fluent API
PromptBuilder builder = prompter.newBuilder();
builder.createListPrompt()
.name("color")
.message("What is your favorite color?")
.newItem("red").text("Red").add()
.newItem("green").text("Green").add()
.newItem("blue").text("Blue").add()
.addPrompt();
// Execute prompts and get results
Map<String, ? extends PromptResult<? extends Prompt>> results =
prompter.prompt(Collections.emptyList(), builder.build());
// Access typed results
ListResult colorChoice = (ListResult) results.get("color");
System.out.println("Selected: " + colorChoice.getSelectedId());
Advanced Usage
Dynamic Prompts
// Create prompts based on previous answers
Map<String, ? extends PromptResult<? extends Prompt>> results = prompter.prompt(
Collections.emptyList(),
previousResults -> {
if (previousResults.containsKey("advanced")) {
ConfirmResult advanced = (ConfirmResult) previousResults.get("advanced");
if (advanced.isConfirmed()) {
return Arrays.asList(
builder.createInputPrompt()
.name("details")
.message("Enter additional details:")
.addPrompt()
);
}
}
return null; // No more prompts
}
);
Custom Configuration
// Create custom configuration
Prompter.Config config = new DefaultPrompter.DefaultConfig(
"?", // indicator
"? ", // unchecked box
"? ", // checked box
"? " // unavailable item
);
Prompter prompter = PrompterFactory.create(terminal, config);
Architecture
The API is built around several key interfaces:
Prompter- Main entry point for creating and executing promptsPromptBuilder- Factory for creating different types of prompt buildersPrompt- Base interface for all prompt typesPromptResult- Parameterized interface for type-safe resultsPromptItem- Interface for individual items within prompts
Migration from jline-console-ui
This module replaces the deprecated jline-console-ui module. Key improvements include:
- Clean interface-based design with better separation of concerns
- Parameterized types for compile-time type safety
- Record-style accessor methods without 'get' prefixes
- Immutable prompt and result objects
- Comprehensive documentation and examples
- Since:
- 3.30.0
- See Also:
-
ClassDescriptionBaseBuilder<T extends BaseBuilder<T>>Base interface for all prompt builders providing common configuration methods.Builder for checkbox prompts.Interface for checkbox items in the Prompter.Interface for checkbox prompts.Result of a checkbox choice.Builder interface for checkbox separators.Builder for choice prompts.Interface for choice items in the Prompter.Interface for choice prompts.Result of an expandable choice prompt.Builder for confirmation prompts.Interface for confirmation items in the Prompter.Interface for confirmation prompts.Result of a confirmation prompt.Possible confirmation values.Utility class to help with dynamic prompting scenarios.Builder interface for editor prompts.Interface for editor prompts.Result of an editor prompt.Builder for creating text input prompts with advanced features.Interface for input prompts.Result of an input prompt.Builder for key press prompts.Interface for key press prompts.Result of a key press prompt.Builder for list prompts.Interface for list items in the Prompt.Interface for list prompts.Result of a list choice.Builder interface for list separators.A result that represents no result.Builder interface for number prompts.Interface for number prompts.Builder interface for password prompts.Interface for password prompts.Interface for all promptable elements in the Prompter.Interface for building prompts in the Prompter.Prompt command implementations for JLine applications.Context for command execution, providing I/O streams and current directory.Main interface for the JLine Prompt API.Configuration interface for customizing the visual appearance and behavior of prompts.Factory for creating Prompter instances.Interface for all UI items in the Prompter.PromptResult<T extends Prompt>Parameterized interface representing the result of a user prompt interaction.Builder interface for search prompts.SearchPrompt<T>Interface for search prompts.SearchResult<T>Result of a search prompt.Interface for separator items in lists and checkboxes.Builder for text displays.Interface for text prompts.Builder for toggle prompts.Interface for toggle prompts.Result of a toggle prompt.