Class LineReaderBuilder


  • public final class LineReaderBuilder
    extends java.lang.Object
    A builder for creating and configuring LineReader instances.

    This builder provides a fluent API for constructing LineReader objects with various configuration options. It simplifies the process of creating a properly configured LineReader by providing methods for setting all the necessary components and options.

    Example usage:

     LineReader reader = LineReaderBuilder.builder()
         .terminal(terminal)
         .completer(completer)
         .parser(parser)
         .highlighter(highlighter)
         .variable(LineReader.HISTORY_FILE, historyFile)
         .option(LineReader.Option.AUTO_LIST, true)
         .build();
     

    If no terminal is provided, the builder will attempt to create a default terminal using TerminalBuilder.terminal().

    See Also:
    LineReader, Terminal
    • Method Detail

      • builder

        public static LineReaderBuilder builder()
        Creates a new LineReaderBuilder instance.
        Returns:
        a new LineReaderBuilder
      • terminal

        public LineReaderBuilder terminal​(org.jline.terminal.Terminal terminal)
        Sets the terminal to be used by the LineReader.

        If not specified, a default terminal will be created when building the LineReader.

        Parameters:
        terminal - the terminal to use
        Returns:
        this builder
      • variables

        public LineReaderBuilder variables​(java.util.Map<java.lang.String,​java.lang.Object> variables)
      • variable

        public LineReaderBuilder variable​(java.lang.String name,
                                          java.lang.Object value)
      • completer

        public LineReaderBuilder completer​(Completer completer)
        Sets the completer to be used for tab completion.

        The completer provides completion candidates when the user presses the tab key.

        Parameters:
        completer - the completer to use
        Returns:
        this builder
        See Also:
        Completer
      • highlighter

        public LineReaderBuilder highlighter​(Highlighter highlighter)
        Sets the highlighter to be used for syntax highlighting.

        The highlighter applies styling to the input text as the user types.

        Parameters:
        highlighter - the highlighter to use
        Returns:
        this builder
        See Also:
        Highlighter
      • parser

        public LineReaderBuilder parser​(Parser parser)
        Sets the parser to be used for parsing command lines.

        The parser breaks the input line into tokens according to specific syntax rules. It is used during tab completion and when accepting a line of input.

        This method will log a warning if the provided parser does not support the CompletingParsedLine interface, as this may cause issues with completion of escaped or quoted words.

        Parameters:
        parser - the parser to use
        Returns:
        this builder
        See Also:
        Parser, CompletingParsedLine
      • build

        public LineReader build()
        Builds and returns a LineReader instance with the configured options.

        This method creates a new LineReader with all the components and options that have been set on this builder. If no terminal has been provided, a default terminal will be created.

        The resulting LineReader will have the following components set:

        • Terminal - either the provided terminal or a default one
        • Application name - either the provided name or the terminal name
        • History - either the provided history or a new DefaultHistory
        • Completer, Highlighter, Parser, Expander - if provided
        • Options - any options that were set using option()
        Returns:
        a new LineReader instance
        Throws:
        java.io.IOError - if there is an error creating the default terminal