java.lang.Object
org.jline.prompt.impl.DefaultPrompter
- All Implemented Interfaces:
Prompter
Default implementation of the Prompter interface.
This is now the native implementation that doesn't depend on console-ui.
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionDefaultPrompter(org.jline.reader.LineReader reader, org.jline.terminal.Terminal terminal, PrompterConfig config) Create a new DefaultPrompter with the given line reader, terminal, and configuration.DefaultPrompter(org.jline.terminal.Terminal terminal) Create a new DefaultPrompter with the given terminal.DefaultPrompter(org.jline.terminal.Terminal terminal, PrompterConfig config) Create a new DefaultPrompter with the given terminal and configuration. -
Method Summary
Modifier and TypeMethodDescriptionorg.jline.reader.LineReaderGet the line reader associated with this Prompter, if any.org.jline.terminal.TerminalGet the terminal associated with this Prompter.Get a builder for creating prompts.Map<String, ? extends PromptResult<? extends Prompt>> prompt(List<org.jline.utils.AttributedString> header, Function<Map<String, ? extends PromptResult<? extends Prompt>>, List<? extends Prompt>> promptsProvider) Execute prompts dynamically based on previous user responses.Map<String, ? extends PromptResult<? extends Prompt>> Execute a list of prompts and collect user responses.protected PromptResult<? extends Prompt> promptElement(List<org.jline.utils.AttributedString> header, Prompt prompt, PromptResult<? extends Prompt> oldResult) Execute a single prompt element (like ConsolePrompt.promptElement).protected voidpromptInternal(List<org.jline.utils.AttributedString> headerIn, List<? extends Prompt> promptList, Map<String, PromptResult<? extends Prompt>> resultMap, boolean cancellableFirstPrompt) Internal prompt method that mirrors ConsolePrompt.prompt() logic.
-
Field Details
-
DEFAULT_TIMEOUT_WITH_ESC
public static final long DEFAULT_TIMEOUT_WITH_ESC- See Also:
-
-
Constructor Details
-
DefaultPrompter
public DefaultPrompter(org.jline.terminal.Terminal terminal) Create a new DefaultPrompter with the given terminal.- Parameters:
terminal- the terminal to use
-
DefaultPrompter
Create a new DefaultPrompter with the given terminal and configuration.- Parameters:
terminal- the terminal to useconfig- the configuration to use
-
DefaultPrompter
public DefaultPrompter(org.jline.reader.LineReader reader, org.jline.terminal.Terminal terminal, PrompterConfig config) Create a new DefaultPrompter with the given line reader, terminal, and configuration.- Parameters:
reader- the line reader to useterminal- the terminal to useconfig- the configuration to use
-
-
Method Details
-
newBuilder
Description copied from interface:PrompterGet a builder for creating prompts.- Specified by:
newBuilderin interfacePrompter- Returns:
- a prompt builder
-
prompt
public Map<String,? extends PromptResult<? extends Prompt>> prompt(List<org.jline.utils.AttributedString> header, List<? extends Prompt> prompts) throws IOException, org.jline.reader.UserInterruptException Description copied from interface:PrompterExecute a list of prompts and collect user responses.This method presents the given prompts to the user in sequence and collects their responses. Each prompt is identified by its name, which serves as the key in the returned result map.
Example:
List<AttributedString> header = Arrays.asList( new AttributedString("Welcome to the setup wizard") ); List<Prompt> prompts = builder.build(); Map<String, ? extends PromptResult<? extends Prompt>> results = prompter.prompt(header, prompts); // Access specific results ListResult choice = (ListResult) results.get("choice"); String selectedId = choice.getSelectedId();- Specified by:
promptin interfacePrompter- Parameters:
header- header information to display before the prompts (may be empty)prompts- the list of prompts to present to the user in sequence- Returns:
- a map containing results for each prompt, keyed by prompt name
- Throws:
IOException- if an I/O error occurs during prompt executionorg.jline.reader.UserInterruptException- if user interrupt handling is enabled and the user types the interrupt character (Ctrl+C)- See Also:
-
prompt
public Map<String,? extends PromptResult<? extends Prompt>> prompt(List<org.jline.utils.AttributedString> header, Function<Map<String, ? extends PromptResult<? extends Prompt>>, throws IOExceptionList<? extends Prompt>> promptsProvider) Description copied from interface:PrompterExecute prompts dynamically based on previous user responses.This method enables conditional prompting where subsequent prompts are generated based on the user's previous answers. The
promptsProviderfunction is called repeatedly with the accumulated results until it returnsnull, indicating no more prompts should be shown.Use Cases:
- Conditional prompts based on user choices
- Multi-step wizards with branching logic
- Progressive disclosure of options
- Validation-dependent follow-up questions
Example:
Map<String, ? extends PromptResult<? extends Prompt>> results = prompter.prompt( header, previousResults -> { if (previousResults.isEmpty()) { // First call - show initial prompts return Arrays.asList( builder.createConfirmPrompt() .name("advanced") .message("Show advanced options?") .addPrompt() ); } else if (previousResults.containsKey("advanced")) { ConfirmResult advanced = (ConfirmResult) previousResults.get("advanced"); if (advanced.isConfirmed()) { // Show advanced prompts return Arrays.asList( builder.createInputPrompt() .name("config") .message("Enter configuration:") .addPrompt() ); } } return null; // No more prompts } );- Specified by:
promptin interfacePrompter- Parameters:
header- header information to display before the first set of prompts (may be empty)promptsProvider- a function that receives previous results and returns the next list of prompts, ornullto indicate no more prompts should be shown- Returns:
- a map containing results for all executed prompts, keyed by prompt name
- Throws:
IOException- if an I/O error occurs during prompt execution- See Also:
-
getTerminal
public org.jline.terminal.Terminal getTerminal()Description copied from interface:PrompterGet the terminal associated with this Prompter.- Specified by:
getTerminalin interfacePrompter- Returns:
- the terminal
-
promptInternal
protected void promptInternal(List<org.jline.utils.AttributedString> headerIn, List<? extends Prompt> promptList, Map<String, PromptResult<? extends Prompt>> resultMap, boolean cancellableFirstPrompt) throws IOExceptionInternal prompt method that mirrors ConsolePrompt.prompt() logic. Handles header accumulation and backward navigation.- Throws:
IOException
-
getLineReader
public org.jline.reader.LineReader getLineReader()Description copied from interface:PrompterGet the line reader associated with this Prompter, if any.- Specified by:
getLineReaderin interfacePrompter- Returns:
- the line reader, or null if none is associated
-
promptElement
protected PromptResult<? extends Prompt> promptElement(List<org.jline.utils.AttributedString> header, Prompt prompt, PromptResult<? extends Prompt> oldResult) throws org.jline.reader.UserInterruptException, org.jline.reader.EndOfFileException Execute a single prompt element (like ConsolePrompt.promptElement).- Throws:
org.jline.reader.UserInterruptExceptionorg.jline.reader.EndOfFileException
-