Skip to content
FutureBlitz

    Install

    Import std.term.prompts

    std.term.prompts

    Asking yes or no, for one of some options, or for a line of text.

    std.term's prompts: Confirm asks yes or no, Choose for one of some options, and Ask for a line of text, on the console or on a Terminal given. Where both what's written and the keys are a terminal's, keys are read as they're typed: Confirm takes y or n at once, Choose moves through its options with the arrows, and typing filters them, by fuzzy matching, and Ask edits its line as a shell does. Otherwise, as from a pipe or a file, each reads a line, which it writes after its question, and asks again for one it can't take. Each gives Null at the end of the input, and for Escape.

    std/term/prompts.fb

    Functions

    Confirm function

    Public Function Confirm(question: String, default: Bool? = Null): Bool?

    Asks question on the console, and gives True for yes or False for no, or default for ENTER alone, when it has one.

    std/term/prompts.fb, line 31

    Confirm function

    Public Function Confirm(terminal: Terminal, question: String, default: Bool? = Null): Bool?

    Asks question on terminal, and gives True for yes or False for no, or default for ENTER alone, when it has one: a key, y or n in either case, where it's interactive, and a line otherwise, "y", "yes", "n", or "no", in any case.

    Examples

    Local terminal = FakeTerminal(interactive: False)
    terminal.Feed("yes\n")
    Expect(Confirm(terminal, "Delete it?") == True)
    terminal.Feed("Ada\n")
    Expect(Ask(terminal, "Name?") == "Ada")

    std/term/prompts.fb, line 48

    Ask function

    Public Function Ask(question: String, default: String = ""): String?

    Asks question on the console, and gives what's typed, or default for nothing.

    std/term/prompts.fb, line 94

    Ask function

    Public Function Ask(question: String, validate: Func(String): String?, default: String = ""): String?

    Asks question on the console, and gives what's typed, or default for nothing, once validate gives Null for it; while it doesn't, what it gives says what's wrong, and the question goes on.

    Examples

    Local terminal = FakeTerminal(interactive: False)
    terminal.Feed("\n42\n")
    // What validate gives says what's wrong with an answer, or Null for one that's right.
    Local number = Function(text: String): String? => Int.Parse(text) == Null ? "a number" : Null
    Local age = Ask(terminal, "Age?", number)
    Expect(age == "42")
    Expect(terminal.Output.Contains("a number"))

    std/term/prompts.fb, line 114

    Ask function

    Public Function Ask(terminal: Terminal, question: String, default: String = ""): String?

    Asks question on terminal, and gives what's typed, or default for nothing.

    std/term/prompts.fb, line 120

    Ask function

    Public Function Ask(terminal: Terminal, question: String, validate: Func(String): String?, default: String = ""): String?

    Asks question on terminal, and gives what's typed, or default for nothing, once validate gives Null for it; while it doesn't, what it gives says what's wrong, and the question goes on.

    std/term/prompts.fb, line 127

    Choose function

    @PanicsAtCaller Public Function Choose(question: String, options: String[], default: Int = 0): Int?

    Asks question on the console, and gives one of options, by its index, from default's at first, or Null for Escape. Panics without an option.

    Examples

    Local terminal = FakeTerminal(interactive: False)
    terminal.Feed("2\n")
    Expect(Choose(terminal, "Size?", ["small", "medium", "large"]) == 1)

    std/term/prompts.fb, line 142

    Choose function

    @PanicsAtCaller Public Function Choose(terminal: Terminal, question: String, options: String[], default: Int = 0): Int?

    Asks question on terminal, and gives one of options, by its index: where it's interactive, the one selected with the arrows, from default's, at ENTER, among those that match what's typed, by fuzzy matching, the best first; otherwise, a number from 1, or default's for none. Panics without an option.

    std/term/prompts.fb, line 152

    Report an issue with this page or with FutureBlitz on GitHub.