Class InputLine


  • public class InputLine
    extends java.lang.Object
    Class that handled reading a line from terminal input with character and line validators, and optional tab completion.

    When writing input into an app it is problematic if the app crashes or exits every time you makes invalid input. This can be solved with the InputLine.CharValidator and InputLine.LineValidator interfaces. The CharValidator validates that any single char input is valid, and will block if not. The LineValidator is triggered when the user finishes the input and checks if the line is valid as a whole, and blocks completion if not.

    In addition a InputLine.TabCompletion interface may be provided that can complete input based on the current content before the cursor. If the complete() method returns a string, it will replace what was before.

    • Constructor Detail

      • InputLine

        public InputLine​(Terminal terminal,
                         java.lang.String message)
        Constructor for simple line-input.
        Parameters:
        terminal - Terminal to use.
        message - Message to print.
      • InputLine

        public InputLine​(Terminal terminal,
                         java.lang.String message,
                         InputLine.CharValidator charValidator,
                         InputLine.LineValidator lineValidator,
                         InputLine.TabCompletion tabCompletion)
        Constructor for complete line-input.
        Parameters:
        terminal - Terminal to use.
        message - Message to print.
        charValidator - The character validcator or null.
        lineValidator - The line validator or null.
        tabCompletion - The tab expander or null.
      • InputLine

        public InputLine​(Terminal terminal,
                         java.lang.String message,
                         InputLine.CharValidator charValidator,
                         InputLine.LineValidator lineValidator,
                         InputLine.TabCompletion tabCompletion,
                         java.util.regex.Pattern delimiterPattern)
        Constructor for complete line-input.
        Parameters:
        terminal - Terminal to use.
        message - Message to print.
        charValidator - The character validcator or null.
        lineValidator - The line validator or null.
        tabCompletion - The tab expander or null.
        delimiterPattern - Pattern matching a character that delimit 'words' that are skipped or deleted with [ctrl-left], [ctrl-right], [alt-w] and [alt-d].
    • Method Detail

      • readLine

        public java.lang.String readLine()
        Read line from terminal.
        Returns:
        The resulting line.
      • readLine

        public java.lang.String readLine​(java.lang.String initial)
        Read line from terminal.
        Parameters:
        initial - The initial (default) value.
        Returns:
        The resulting line.