Text prompt

Sometimes you want to get some input from the user, and for this you can use the Prompt<TResult>.

The use of prompts insides status or progress displays is not supported.


if (!AnsiConsole.Confirm("Run prompt example?"))
    AnsiConsole.MarkupLine("Ok... :(");
    return false;

return true;
Run prompt example? [y/n] (y): _


var name = AnsiConsole.Ask<string>("What's your [green]name[/]?");
return name;
What's your name? Patrik
What's your age? 37


var favorites = AnsiConsole.Prompt(
    new MultiSelectionPrompt<string>()
        .Title("What are your [green]favorite fruits[/]?")
        .MoreChoicesText("[grey](Move up and down to reveal more fruits)[/]")
        .InstructionsText("[grey](Press [blue][/] to toggle a fruit, [green][/] to accept)[/]")
        .AddChoiceGroup("Berries", new[]
            "Blackcurrant", "Blueberry", "Cloudberry",
            "Elderberry", "Honeyberry", "Mulberry"
            "Apple", "Apricot", "Avocado", "Banana",
            "Cherry", "Cocunut", "Date", "Dragonfruit", "Durian",
            "Egg plant",  "Fig", "Grape", "Guava",
            "Jackfruit", "Jambul", "Kiwano", "Kiwifruit", "Lime", "Lylo",
            "Lychee", "Melon", "Nectarine", "Orange", "Olive"

var fruit = favorites.Count == 1 ? favorites[0] : null;
if (string.IsNullOrWhiteSpace(fruit))
    fruit = AnsiConsole.Prompt(
        new SelectionPrompt<string>()
            .Title("Ok, but if you could only choose [green]one[/]?")
            .MoreChoicesText("[grey](Move up and down to reveal more fruits)[/]")

AnsiConsole.MarkupLine("Your selected: [yellow]{0}[/]", fruit);
return fruit;
What's your favorite fruit? [Apple/Banana/Orange] (Orange): _


return AnsiConsole.Prompt(
    new TextPrompt<int>("How [green]old[/] are you?")
        .ValidationErrorMessage("[red]That's not a valid age[/]")
        .Validate(age =>
            return age switch
                <= 0 => ValidationResult.Error("[red]You must at least be 1 years old[/]"),
                >= 123 => ValidationResult.Error("[red]You must be younger than the oldest person alive[/]"),
                _ => ValidationResult.Success(),
What's the secret number? 32
Too low
What's the secret number? 102
Too high
What's the secret number? _


return AnsiConsole.Prompt(
    new TextPrompt<string>("Enter [green]password[/]?")
Enter password: ************_


return AnsiConsole.Prompt(
    new TextPrompt<string>("[grey][[Optional]][/] What is your [green]favorite color[/]?")
[Optional] Favorite color? _