Class DefaultPrompter

  • All Implemented Interfaces:
    Prompter

    public class DefaultPrompter
    extends java.lang.Object
    implements Prompter
    Default implementation of the Prompter interface. This is now the native implementation that doesn't depend on console-ui.
    • Constructor Summary

      Constructors 
      Constructor Description
      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.
      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

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected java.io.File createEditorTempFile​(java.lang.String prefix, java.lang.String suffix)
      Creates the temporary file handed to the editor.
      org.jline.reader.LineReader getLineReader()
      Get the line reader associated with this Prompter, if any.
      org.jline.terminal.Terminal getTerminal()
      Get the terminal associated with this Prompter.
      PromptBuilder newBuilder()
      Get a builder for creating prompts.
      java.util.Map<java.lang.String,​? extends PromptResult<? extends Prompt>> prompt​(java.util.List<org.jline.utils.AttributedString> header, java.util.function.Function<java.util.Map<java.lang.String,​? extends PromptResult<? extends Prompt>>,​java.util.List<? extends Prompt>> promptsProvider)
      Execute prompts dynamically based on previous user responses.
      java.util.Map<java.lang.String,​? extends PromptResult<? extends Prompt>> prompt​(java.util.List<org.jline.utils.AttributedString> header, java.util.List<? extends Prompt> prompts)
      Execute a list of prompts and collect user responses.
      protected PromptResult<? extends Prompt> promptElement​(java.util.List<org.jline.utils.AttributedString> header, Prompt prompt, PromptResult<? extends Prompt> oldResult)
      Execute a single prompt element (like ConsolePrompt.promptElement).
      protected void promptInternal​(java.util.List<org.jline.utils.AttributedString> headerIn, java.util.List<? extends Prompt> promptList, java.util.Map<java.lang.String,​PromptResult<? extends Prompt>> resultMap, boolean cancellableFirstPrompt)
      Internal prompt method that mirrors ConsolePrompt.prompt() logic.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • 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 Detail

      • 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 java.util.Map<java.lang.String,​? extends PromptResult<? extends Prompt>> prompt​(java.util.List<org.jline.utils.AttributedString> header,
                                                                                                     java.util.List<? extends Prompt> prompts)
                                                                                              throws java.io.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:
        java.io.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:
        PromptBuilder.build(), PromptResult
      • prompt

        public java.util.Map<java.lang.String,​? extends PromptResult<? extends Prompt>> prompt​(java.util.List<org.jline.utils.AttributedString> header,
                                                                                                     java.util.function.Function<java.util.Map<java.lang.String,​? extends PromptResult<? extends Prompt>>,​java.util.List<? extends Prompt>> promptsProvider)
                                                                                              throws java.io.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:
        java.io.IOException - if an I/O error occurs during prompt execution
        See Also:
        PromptResult, PromptBuilder
      • 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​(java.util.List<org.jline.utils.AttributedString> headerIn,
                                      java.util.List<? extends Prompt> promptList,
                                      java.util.Map<java.lang.String,​PromptResult<? extends Prompt>> resultMap,
                                      boolean cancellableFirstPrompt)
                               throws java.io.IOException
        Internal prompt method that mirrors ConsolePrompt.prompt() logic. Handles header accumulation and backward navigation.
        Throws:
        java.io.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​(java.util.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
      • createEditorTempFile

        protected java.io.File createEditorTempFile​(java.lang.String prefix,
                                                    java.lang.String suffix)
                                             throws java.io.IOException
        Creates the temporary file handed to the editor. The file holds the prompt text, so it is created with owner-only permissions on POSIX file systems.
        Throws:
        java.io.IOException