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 prompts
  • PromptBuilder - Factory for creating different types of prompt builders
  • Prompt - Base interface for all prompt types
  • PromptResult - Parameterized interface for type-safe results
  • PromptItem - 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: