Class Selection<E>

  • All Implemented Interfaces:
    Closeable, AutoCloseable, LinePrinter

    public class Selection<E>
    extends Object
    implements LinePrinter, Closeable
    Tabular selection with simple navigation. It displays a list of items in a table that can be paginated and navigated between using the arrow keys, numbers, pg-up, pg-down etc.
    
     {message}: [c=command, o=overview]
      1 {line 1}
      2 {line 2}
      3 {line 3}
     Your choice (1..N or c,o):
     

    When doing actions, it is possible to add extra output to the user, this will be printed below the `Your choice` line with a LinePrinter connected back to the Selection instance.

    By default, there are no actions on the items, even exit is not provided. Actions are specified using a list of "commands", that each have an action, where each action has a Reaction. Note that any runtime exception thrown while handling an action exits the selection.

    • Constructor Detail

      • Selection

        protected Selection​(Terminal terminal,
                            String prompt,
                            EntrySource<E> entries,
                            List<EntryCommand<E>> commands,
                            EntryPrinter<E> printer,
                            Clock clock,
                            int pageSize,
                            int pageMargin,
                            int lineWidth,
                            boolean pageHint,
                            int initialIndex)
        Create a selection instance.
        Parameters:
        terminal - The terminal to print to.
        prompt - The prompt to introduce the selection with.
        entries - The list of entries.
        commands - The list of commands.
        printer - The entry printer.
        clock - The system clock.
        pageSize - The max number of entries per page.
        pageMargin - The number of entries above page size needed to trigger paging.
        lineWidth - The number of columns to print on.
        pageHint - True if pagination is always on.
        initialIndex - Initial index of first selection.
    • Method Detail

      • newBuilder

        public static <E> SelectionBuilder<E> newBuilder​(List<E> source)
        Create a new selection builder selecting from a list. If the list is modified in the background (i.e. by actions), future fetches will be able to pick that up, given the SelectionReaction.UPDATE_KEEP_ITEM or SelectionReaction.UPDATE_KEEP_POSITION reactions are used.
        Type Parameters:
        E - The item type.
        Parameters:
        source - The list source of items.
        Returns:
        The selection builder.
      • println

        public void println​(String message)
        Description copied from interface: LinePrinter
        Print a new line to the terminal.
        Specified by:
        println in interface LinePrinter
        Parameters:
        message - The message to write.
      • runSelection

        public E runSelection()
        Run interactive selection, including action handling, and return the selected item. See SelectionReaction.
        Returns:
        The selected result or null if exited without selection.
        Throws:
        UncheckedIOException - If end of input or user interrupt.
      • select

        public void select​(int index)
        Move selection to index. If selection is not on the current page, will change page to the one with the selected index.
        Parameters:
        index - The index to move the selection to.
        Throws:
        IndexOutOfBoundsException - If index outside bounds.
      • select

        public void select​(E entry)
        Move selection to specified entry. If selection is not on the current page, will change page to the one with the selected entry.
        Parameters:
        entry - Entry to move selection to.
        Throws:
        NoSuchElementException - If no such entry found.
      • refreshKeepPosition

        public void refreshKeepPosition()
        refresh the items in the view, and stay on the same position (index wise). If the number of available items has decreased to be lower than for the current index, will select the last entry.
      • refreshKeepEntry

        public void refreshKeepEntry()
        Refresh the items in the view, and stay on the same entry (so will look for the currently selected entry in the entry source, and use that index as the new selected / current position.). If that entry is not found, will keep the current position.
      • confirm

        public boolean confirm​(String confirmation)
        Make a single confirmation with no default. Printed below the selection table.
        Parameters:
        confirmation - The confirmation text.
        Returns:
        True if confirmed.
      • confirm

        public boolean confirm​(String confirmation,
                               boolean defaultValue)
        Make a single confirmation with specified default. Printed below the selection table.
        Parameters:
        confirmation - The confirmation text.
        defaultValue - The default value.
        Returns:
        True if confirmed.