Lesson 5 - Item selection

This commit is contained in:
2025-08-08 08:49:09 +02:00
parent f3451f5d41
commit 0a819704f3

View File

@ -20,30 +20,51 @@
////AnsiConsole.Write(new Markup("Danger Text from Style\n", danger));
////AnsiConsole.WriteLine(" and more...");
#endregion
// Lesson 4 - Text prompts
//int age = AnsiConsole.Ask<int>("What is your [green]age[/]?");
int age = AnsiConsole.Prompt(
new TextPrompt<int>("What is your age")
.Validate((x) => x switch
{
< 0 => ValidationResult.Error("You were not born yet!"),
> 120 => ValidationResult.Error("You are too old!"),
_ => ValidationResult.Success()
})
);
//bool isHappy = AnsiConsole.Ask<bool>("Are you [green]happy[/]?");
//////int age = AnsiConsole.Ask<int>("What is your [green]age[/]?");
////int age = AnsiConsole.Prompt(
//// new TextPrompt<int>("What is your age")
//// .Validate((x) => x switch
//// {
//// < 0 => ValidationResult.Error("You were not born yet!"),
//// > 120 => ValidationResult.Error("You are too old!"),
//// _ => ValidationResult.Success()
//// })
////);
//////bool isHappy = AnsiConsole.Ask<bool>("Are you [green]happy[/]?");
string happyText = AnsiConsole.Prompt(
new TextPrompt<string>("Are you [green]happy[/]?")
.AddChoice("Yes")
.AddChoice("No")
.DefaultValue("Yes")
////string happyText = AnsiConsole.Prompt(
//// new TextPrompt<string>("Are you [green]happy[/]?")
//// .AddChoice("Yes")
//// .AddChoice("No")
//// .DefaultValue("Yes")
////);
////AnsiConsole.MarkupLine($"Happy: {happyText}\nAge: {age}");
#endregion
// Lesson 5 - Item Selection
List<string> names = [
"Tim Corey",
"Sue Storm",
"Bilbo Baggins",
"John Doe",
"Princess Donaut",
"Steve Rogers"
];
string favoriteName = AnsiConsole.Prompt(
new SelectionPrompt<string>()
.Title("Which is your favorite placeholder name?")
.PageSize(4)
.MoreChoicesText("[grey](Move up and down to reveal more choices)[/]")
.AddChoices(names)
);
AnsiConsole.MarkupLine($"Happy: {happyText}\nAge: {age}");
AnsiConsole.MarkupLine($"Your favorite name is: [bold red]{favoriteName}[/].");
Console.ReadLine();
AnsiConsole.Clear();