- All Superinterfaces:
BaseBuilder<InputBuilder>
- All Known Implementing Classes:
DefaultInputBuilder
InputBuilder creates prompts that allow users to enter free-form text input.
It supports various advanced features including input masking (for passwords),
auto-completion, validation, and custom line readers.
Features
- Text Input - Free-form text entry with full editing capabilities
- Input Masking - Hide sensitive input like passwords
- Auto-completion - Provide completion suggestions as user types
- Validation - Validate input before accepting
- Default Values - Pre-populate input with default text
- Custom Line Readers - Use specialized line readers for advanced scenarios
Basic Usage
builder.createInputPrompt()
.name("username")
.message("Enter your username:")
.defaultValue("admin")
.addPrompt();
Password Input
builder.createInputPrompt()
.name("password")
.message("Enter your password:")
.mask('*') // Hide input with asterisks
.addPrompt();
Input with Validation
builder.createInputPrompt()
.name("email")
.message("Enter your email address:")
.validator(input -> input.contains("@") && input.contains("."))
.addPrompt();
Input with Completion
Completer fileCompleter = new FileNameCompleter();
builder.createInputPrompt()
.name("filepath")
.message("Enter file path:")
.completer(fileCompleter)
.addPrompt();
- Since:
- 3.30.0
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptioncompleter(org.jline.reader.Completer completer) Set the completer to provide auto-completion suggestions.defaultValue(String defaultValue) Set the default value that will be pre-filled in the input field.lineReader(org.jline.reader.LineReader lineReader) Set a custom line reader for advanced input handling.Set the character used to mask input for sensitive data.Set a validator function to validate user input before accepting it.Methods inherited from interface org.jline.prompt.BaseBuilder
addPrompt, filter, message, name, transformer
-
Method Details
-
defaultValue
Set the default value that will be pre-filled in the input field.The default value appears in the input field when the prompt is displayed, allowing users to accept it by pressing Enter or modify it as needed. This is useful for providing sensible defaults or previously entered values.
Example:
builder.createInputPrompt() .name("port") .message("Enter port number:") .defaultValue("8080") // User can accept or change this .addPrompt();- Parameters:
defaultValue- the default text to pre-fill (may be null for no default)- Returns:
- this builder instance for method chaining
-
mask
Set the character used to mask input for sensitive data.When a mask character is set, the actual input characters are hidden and replaced with the mask character in the display. This is essential for password input and other sensitive data entry.
Common Mask Characters:
'*'- Traditional asterisk masking'?'- Bullet point (modern style)'?'- Filled circle' '- Space (completely hidden)
Example:
builder.createInputPrompt() .name("password") .message("Enter password:") .mask('*') // Input appears as: **** .addPrompt();- Parameters:
mask- the character to display instead of actual input, ornullto disable masking- Returns:
- this builder instance for method chaining
-
completer
Set the completer to provide auto-completion suggestions.The completer is invoked as the user types to provide completion suggestions. Users can typically press Tab to trigger completion or navigate through suggestions. JLine provides several built-in completers for common use cases.
Built-in Completers:
FileNameCompleter- File and directory completionStringsCompleter- Completion from a predefined listArgumentCompleter- Multi-argument completionAggregateCompleter- Combines multiple completers
Example:
Completer completer = new StringsCompleter("option1", "option2", "option3"); builder.createInputPrompt() .name("choice") .message("Enter option:") .completer(completer) .addPrompt();- Parameters:
completer- the completer to use for auto-completion (may be null for no completion)- Returns:
- this builder instance for method chaining
- See Also:
-
lineReader
Set a custom line reader for advanced input handling.By default, the prompter uses its own line reader configuration. This method allows you to provide a custom line reader with specific settings, key bindings, or behaviors that differ from the default configuration.
Use Cases:
- Custom key bindings for specific input scenarios
- Specialized editing modes (vi vs emacs)
- Custom history management
- Integration with existing line reader configurations
Example:
LineReader customReader = LineReaderBuilder.builder() .terminal(terminal) .option(LineReader.Option.DISABLE_EVENT_EXPANSION, true) .build(); builder.createInputPrompt() .name("command") .message("Enter command:") .lineReader(customReader) .addPrompt();- Parameters:
lineReader- the custom line reader to use (may be null to use default)- Returns:
- this builder instance for method chaining
- See Also:
-
validator
Set a validator function to validate user input before accepting it.The validator function is called with the user's input and should return
trueif the input is valid,falseotherwise. If validation fails, the user is prompted to enter the input again.Validation Examples:
// Email validation builder.createInputPrompt() .name("email") .message("Enter email:") .validator(input -> input.matches("^[^@]+@[^@]+\\.[^@]+$")) .addPrompt(); // Number validation builder.createInputPrompt() .name("port") .message("Enter port (1-65535):") .validator(input -> { try { int port = Integer.parseInt(input); return port >= 1 && port <= 65535; } catch (NumberFormatException e) { return false; } }) .addPrompt(); // Non-empty validation builder.createInputPrompt() .name("name") .message("Enter your name:") .validator(input -> !input.trim().isEmpty()) .addPrompt();- Parameters:
validator- function that returnstruefor valid input,falseotherwise- Returns:
- this builder instance for method chaining
-