Class DefaultPrompter

java.lang.Object
org.jline.prompt.impl.DefaultPrompter
All Implemented Interfaces:
Prompter

public class DefaultPrompter extends Object implements Prompter
Default implementation of the Prompter interface. This is now the native implementation that doesn't depend on console-ui.
  • 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

      public DefaultPrompter(org.jline.terminal.Terminal terminal, PrompterConfig config)
      Create a new DefaultPrompter with the given terminal and configuration.
      Parameters:
      terminal - the terminal to use
      config - 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 use
      terminal - the terminal to use
      config - the configuration to use
  • Method Details

    • newBuilder

      public PromptBuilder newBuilder()
      Description copied from interface: Prompter
      Get a builder for creating prompts.
      Specified by:
      newBuilder in interface Prompter
      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: Prompter
      Execute 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:
      prompt in interface Prompter
      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 execution
      org.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>>,List<? extends Prompt>> promptsProvider) throws IOException
      Description copied from interface: Prompter
      Execute 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 promptsProvider function is called repeatedly with the accumulated results until it returns null, 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:
      prompt in interface Prompter
      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, or null to 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: Prompter
      Get the terminal associated with this Prompter.
      Specified by:
      getTerminal in interface Prompter
      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 IOException
      Internal 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: Prompter
      Get the line reader associated with this Prompter, if any.
      Specified by:
      getLineReader in interface Prompter
      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.UserInterruptException
      org.jline.reader.EndOfFileException