Class CliPrompter

java.lang.Object
nz.org.riskscape.wizard.CliPrompter

public class CliPrompter extends Object

High-level routines for interacting with the user to guide them through the wizard-answering process

  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static enum 
     
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    Account for the '1: ' bit that gets added to the first line of every CLI choice.
    static final int
    80 chars is a bit boxy, but using the full terminal is too wide on linux
    static final int
    Try to be a little consistent with indenting menu items, where possible.
    static final List<CliChoice<Boolean>>
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    CliPrompter(nz.org.riskscape.cli.Terminal terminal, List<Asker> askers, Asker defaultAsker, nz.org.riskscape.engine.i18n.Messages messages, nz.org.riskscape.engine.i18n.TranslationContext context)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    nz.org.riskscape.problem.ResultOrProblems<Object>
    ask(nz.org.riskscape.wizard.bld.IncrementalBuildState buildState, nz.org.riskscape.wizard.Question chosen)
    Asks the user a Question and gets a single unbound response (which may or may not be valid).
    boolean
    askIf(String question)
    Asks the user a yes/no question
    askWithRepeat(nz.org.riskscape.wizard.bld.IncrementalBuildState buildState, nz.org.riskscape.wizard.Question chosen)
    Repeatedly asks the given question until we get a valid response back
    <T> CliChoice<T>
    choose(String prompt, List<CliChoice<T>> choices)
     
    <T> T
    choose(String prompt, List<T> choiceObjects, Function<T,String> labelFunc)
     
    <T> T
    choose(String prompt, List<T> choiceObjects, Function<T,String> labelFunc, Function<CliChoice<T>,CliChoice<T>> modifier)
    Get the user to choose one of the given items
    <T> CliChoice<T>
    choose(nz.org.riskscape.picocli.CommandLine.Help.Ansi.Text prompt, List<CliChoice<T>> choices)
    Ask the user to choose from the given list of choices, returning the thing that was chosen
    <T> Optional<T>
    chooseOptional(String prompt, String cancelLabel, List<T> choiceObjects, Function<T,String> labelFunc, Function<CliChoice<T>,CliChoice<T>> modifier)
    Get the user to choose one of the given items, also providing a cancel option
    void
    displayProblems(String header, List<nz.org.riskscape.problem.Problem> problemList)
     
    void
    displayProblems(String header, nz.org.riskscape.problem.Problem... problems)
     
    findAsker(nz.org.riskscape.wizard.bld.IncrementalBuildState buildState, nz.org.riskscape.wizard.Question chosen)
    We customize how we solicit answers from the user based on the type of answer that the Question expects (i.e.
     
    nz.org.riskscape.engine.i18n.Messages
     
    nz.org.riskscape.cli.AnsiPrintStream
     
    nz.org.riskscape.picocli.CommandLine.Help.Ansi.Text
    getPrompt(nz.org.riskscape.wizard.Question question)
    Returns formatted Text containing a question.
    nz.org.riskscape.cli.Terminal
     
    void
    printBreadcrumb(nz.org.riskscape.wizard.survey2.QuestionTree tree, nz.org.riskscape.wizard.Question chosen)
    Prints out a breadcrumb line to the terminal to display the "parentage" for the given chosen question.
    void
    printError(String message)
     
    nz.org.riskscape.cli.AnsiPrintStream
    println(Object message)
    Shortcut to getOut().println(message)
    nz.org.riskscape.cli.AnsiPrintStream
    Print a title out to the terminal
     
     
    readlineWithTitle(nz.org.riskscape.picocli.CommandLine.Help.Ansi.Text title)
     
    void
    showSuccessMessage(String successMessage)
     
    nz.org.riskscape.picocli.CommandLine.Help.Ansi.Text
    title(String prompt)
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • DEFAULT_DISPLAY_WIDTH

      public static final int DEFAULT_DISPLAY_WIDTH

      80 chars is a bit boxy, but using the full terminal is too wide on linux

      See Also:
    • DEFAULT_INDENT_WIDTH

      public static final int DEFAULT_INDENT_WIDTH

      Try to be a little consistent with indenting menu items, where possible. But not too much indent or it looks weird.

      See Also:
    • CHOICE_INDENT_WIDTH

      public static final int CHOICE_INDENT_WIDTH

      Account for the '1: ' bit that gets added to the first line of every CLI choice. (It assumes single-digit choices, so not perfect alignment)

      See Also:
    • YES_NO_CHOICES

      public static final List<CliChoice<Boolean>> YES_NO_CHOICES
  • Constructor Details

    • CliPrompter

      public CliPrompter(nz.org.riskscape.cli.Terminal terminal, List<Asker> askers, Asker defaultAsker, nz.org.riskscape.engine.i18n.Messages messages, nz.org.riskscape.engine.i18n.TranslationContext context)
  • Method Details

    • println

      public nz.org.riskscape.cli.AnsiPrintStream println(Object message)

      Shortcut to getOut().println(message)

    • askIf

      public boolean askIf(String question)

      Asks the user a yes/no question

    • choose

      public <T> T choose(String prompt, List<T> choiceObjects, Function<T,String> labelFunc, Function<CliChoice<T>,CliChoice<T>> modifier)

      Get the user to choose one of the given items

      Parameters:
      prompt - an unformatted title to show the user
      choiceObjects - the list of java objects to choice from
      labelFunc - a function for generating a label from each item
      modifier - a function that can arbitrarily change a choice, e.g add a subtitle or a mnemonic
      Returns:
      the chosen thing
    • choose

      public <T> T choose(String prompt, List<T> choiceObjects, Function<T,String> labelFunc)
    • chooseOptional

      public <T> Optional<T> chooseOptional(String prompt, String cancelLabel, List<T> choiceObjects, Function<T,String> labelFunc, Function<CliChoice<T>,CliChoice<T>> modifier)

      Get the user to choose one of the given items, also providing a cancel option

      Parameters:
      prompt - an unformatted title to show the user
      cancelLabel - for a cancel option - the first letter becomes its shortcut
      choiceObjects - the list of java objects to choice from
      labelFunc - a function for generating a label from each item
      modifier - a function that can arbitrarily change a choice, e.g add a subtitle or a mnemonic
      Returns:
      the chosen thing, or Optional.empty() if the cancel choice was given
    • ask

      public nz.org.riskscape.problem.ResultOrProblems<Object> ask(nz.org.riskscape.wizard.bld.IncrementalBuildState buildState, nz.org.riskscape.wizard.Question chosen)

      Asks the user a Question and gets a single unbound response (which may or may not be valid).

    • findAsker

      public Asker findAsker(nz.org.riskscape.wizard.bld.IncrementalBuildState buildState, nz.org.riskscape.wizard.Question chosen)

      We customize how we solicit answers from the user based on the type of answer that the Question expects (i.e. the parameterType). E.g. when we expect an aggregation expression, we can use AskAsAsker to ensure a valid expression is always provided. This helps to simplify things for the user and ensures we get valid responses.

    • choose

      public <T> CliChoice<T> choose(String prompt, List<CliChoice<T>> choices)
    • getPrompt

      public nz.org.riskscape.picocli.CommandLine.Help.Ansi.Text getPrompt(nz.org.riskscape.wizard.Question question)

      Returns formatted Text containing a question.

    • choose

      public <T> CliChoice<T> choose(nz.org.riskscape.picocli.CommandLine.Help.Ansi.Text prompt, List<CliChoice<T>> choices)

      Ask the user to choose from the given list of choices, returning the thing that was chosen

    • readlineWithTitle

      public String readlineWithTitle(String title)
    • readlineWithTitle

      public String readlineWithTitle(nz.org.riskscape.picocli.CommandLine.Help.Ansi.Text title)
    • readline

      public String readline()
    • title

      public nz.org.riskscape.picocli.CommandLine.Help.Ansi.Text title(String prompt)
    • printlnTitle

      public nz.org.riskscape.cli.AnsiPrintStream printlnTitle(String prompt)

      Print a title out to the terminal

      Parameters:
      prompt - unformatted, unmarked text to display
    • askWithRepeat

      public Object askWithRepeat(nz.org.riskscape.wizard.bld.IncrementalBuildState buildState, nz.org.riskscape.wizard.Question chosen)

      Repeatedly asks the given question until we get a valid response back

    • displayProblems

      public void displayProblems(String header, nz.org.riskscape.problem.Problem... problems)
    • displayProblems

      public void displayProblems(String header, List<nz.org.riskscape.problem.Problem> problemList)
    • showSuccessMessage

      public void showSuccessMessage(String successMessage)
    • printError

      public void printError(String message)
    • getLocale

      public Locale getLocale()
    • printBreadcrumb

      public void printBreadcrumb(nz.org.riskscape.wizard.survey2.QuestionTree tree, nz.org.riskscape.wizard.Question chosen)

      Prints out a breadcrumb line to the terminal to display the "parentage" for the given chosen question.

    • getTerminal

      public nz.org.riskscape.cli.Terminal getTerminal()
    • getMessages

      public nz.org.riskscape.engine.i18n.Messages getMessages()
    • getOut

      public nz.org.riskscape.cli.AnsiPrintStream getOut()