Module 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:
Prompter,PrompterFactory,PromptBuilder,PromptResult,Prompt
-
Interface Summary Interface Description BaseBuilder<T extends BaseBuilder<T>> Base interface for all prompt builders providing common configuration methods.CheckboxBuilder Builder for checkbox prompts.CheckboxItem Interface for checkbox items in the Prompter.CheckboxPrompt Interface for checkbox prompts.CheckboxResult Result of a checkbox choice.CheckboxSeparatorBuilder Builder interface for checkbox separators.ChoiceBuilder Builder for choice prompts.ChoiceItem Interface for choice items in the Prompter.ChoicePrompt Interface for choice prompts.ChoiceResult Result of an expandable choice prompt.ConfirmBuilder Builder for confirmation prompts.ConfirmItem Interface for confirmation items in the Prompter.ConfirmPrompt Interface for confirmation prompts.ConfirmResult Result of a confirmation prompt.EditorBuilder Builder interface for editor prompts.EditorPrompt Interface for editor prompts.EditorResult Result of an editor prompt.InputBuilder Builder for creating text input prompts with advanced features.InputPrompt Interface for input prompts.InputResult Result of an input prompt.KeyPressBuilder Builder for key press prompts.KeyPressPrompt Interface for key press prompts.KeyPressResult Result of a key press prompt.ListBuilder Builder for list prompts.ListItem Interface for list items in the Prompt.ListPrompt Interface for list prompts.ListResult Result of a list choice.ListSeparatorBuilder Builder interface for list separators.NoResult A result that represents no result.NumberBuilder Builder interface for number prompts.NumberPrompt Interface for number prompts.PasswordBuilder Builder interface for password prompts.PasswordPrompt Interface for password prompts.Prompt Interface for all promptable elements in the Prompter.PromptBuilder Interface for building prompts in the Prompter.Prompter Main interface for the JLine Prompt API.PrompterConfig Configuration interface for customizing the visual appearance and behavior of prompts.PromptItem Interface for all UI items in the Prompter.PromptResult<T extends Prompt> Parameterized interface representing the result of a user prompt interaction.SearchBuilder<T> Builder interface for search prompts.SearchPrompt<T> Interface for search prompts.SearchResult<T> Result of a search prompt.SeparatorItem Interface for separator items in lists and checkboxes.TextBuilder Builder for text displays.TextPrompt Interface for text prompts.ToggleBuilder Builder for toggle prompts.TogglePrompt Interface for toggle prompts.ToggleResult Result of a toggle prompt. -
Class Summary Class Description DynamicPromptHelper Utility class to help with dynamic prompting scenarios.PromptCommands Prompt command implementations for JLine applications.PromptCommands.Context Context for command execution, providing I/O streams and current directory.PrompterFactory Factory for creating Prompter instances. -
Enum Summary Enum Description ConfirmResult.ConfirmationValue Possible confirmation values.