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.

Confirmation

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

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

Simple

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

Choices

var favorites = AnsiConsole.Prompt(
    new MultiSelectionPrompt<string>()
        .PageSize(10)
        .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"
        })
        .AddChoices(new[]
        {
            "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>()
            .EnableSearch()
            .Title("Ok, but if you could only choose [green]one[/]?")
            .MoreChoicesText("[grey](Move up and down to reveal more fruits)[/]")
            .AddChoices(favorites));
}

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

Validation

return AnsiConsole.Prompt(
    new TextPrompt<int>("How [green]old[/] are you?")
        .PromptStyle("green")
        .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? _

Secrets

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

Masks

return AnsiConsole.Prompt(
    new TextPrompt<string>("Enter [green]password[/]?")
        .PromptStyle("red")
        .Secret('-'));
Enter password: ------------_

You can utilize a null character to completely hide input.

return AnsiConsole.Prompt(
    new TextPrompt<string>("Enter [green]password[/]?")
        .PromptStyle("red")
        .Secret(null));
Enter password: _

Optional

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