mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-06-19 13:28:16 +08:00
Add interactive prompts for selecting values
* Adds SelectionPrompt * Adds MultiSelectionPrompt Closes #210
This commit is contained in:

committed by
Patrik Svensson

parent
3a593857c8
commit
0e0f4b4220
@ -1,3 +1,4 @@
|
||||
using System.Collections.Generic;
|
||||
using Spectre.Console;
|
||||
|
||||
namespace Cursor
|
||||
@ -20,26 +21,87 @@ namespace Cursor
|
||||
return;
|
||||
}
|
||||
|
||||
// String
|
||||
// Ask the user for some different things
|
||||
var name = AskName();
|
||||
var fruit = AskFruit();
|
||||
var sport = AskSport();
|
||||
var age = AskAge();
|
||||
var password = AskPassword();
|
||||
var color = AskColor();
|
||||
|
||||
// Summary
|
||||
AnsiConsole.WriteLine();
|
||||
AnsiConsole.Render(new Rule("[yellow]Results[/]").RuleStyle("grey").LeftAligned());
|
||||
AnsiConsole.Render(new Table().AddColumns("[grey]Question[/]", "[grey]Answer[/]")
|
||||
.RoundedBorder()
|
||||
.BorderColor(Color.Grey)
|
||||
.AddRow("[grey]Name[/]", name)
|
||||
.AddRow("[grey]Favorite fruit[/]", fruit)
|
||||
.AddRow("[grey]Favorite sport[/]", sport)
|
||||
.AddRow("[grey]Age[/]", age.ToString())
|
||||
.AddRow("[grey]Password[/]", password)
|
||||
.AddRow("[grey]Favorite color[/]", string.IsNullOrEmpty(color) ? "Unknown" : color));
|
||||
}
|
||||
|
||||
private static string AskName()
|
||||
{
|
||||
AnsiConsole.WriteLine();
|
||||
AnsiConsole.Render(new Rule("[yellow]Strings[/]").RuleStyle("grey").LeftAligned());
|
||||
var name = AnsiConsole.Ask<string>("What's your [green]name[/]?");
|
||||
return name;
|
||||
}
|
||||
|
||||
// String with choices
|
||||
private static string AskFruit()
|
||||
{
|
||||
AnsiConsole.WriteLine();
|
||||
AnsiConsole.Render(new Rule("[yellow]Lists[/]").RuleStyle("grey").LeftAligned());
|
||||
|
||||
var favorites = AnsiConsole.Prompt(
|
||||
new MultiSelectionPrompt<string>()
|
||||
.PageSize(10)
|
||||
.Title("What are your [green]favorite fruits[/]?")
|
||||
.AddChoices(new[]
|
||||
{
|
||||
"Apple", "Apricot", "Avocado", "Banana", "Blackcurrant", "Blueberry",
|
||||
"Cherry", "Cloudberry", "Cocunut", "Date", "Dragonfruit", "Durian",
|
||||
"Egg plant", "Elderberry", "Fig", "Grape", "Guava", "Honeyberry",
|
||||
"Jackfruit", "Jambul", "Kiwano", "Kiwifruit", "Lime", "Lylo",
|
||||
"Lychee", "Melon", "Mulberry", "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[/]?")
|
||||
.AddChoices(favorites));
|
||||
}
|
||||
|
||||
AnsiConsole.MarkupLine("Your selected: [yellow]{0}[/]", fruit);
|
||||
return fruit;
|
||||
}
|
||||
|
||||
private static string AskSport()
|
||||
{
|
||||
AnsiConsole.WriteLine();
|
||||
AnsiConsole.Render(new Rule("[yellow]Choices[/]").RuleStyle("grey").LeftAligned());
|
||||
var fruit = AnsiConsole.Prompt(
|
||||
new TextPrompt<string>("What's your [green]favorite fruit[/]?")
|
||||
.InvalidChoiceMessage("[red]That's not a valid fruit[/]")
|
||||
.DefaultValue("Orange")
|
||||
.AddChoice("Apple")
|
||||
.AddChoice("Banana")
|
||||
.AddChoice("Orange"));
|
||||
|
||||
// Integer
|
||||
return AnsiConsole.Prompt(
|
||||
new TextPrompt<string>("What's your [green]favorite sport[/]?")
|
||||
.InvalidChoiceMessage("[red]That's not a valid fruit[/]")
|
||||
.DefaultValue("Lol")
|
||||
.AddChoice("Soccer")
|
||||
.AddChoice("Hockey")
|
||||
.AddChoice("Basketball"));
|
||||
}
|
||||
|
||||
private static int AskAge()
|
||||
{
|
||||
AnsiConsole.WriteLine();
|
||||
AnsiConsole.Render(new Rule("[yellow]Integers[/]").RuleStyle("grey").LeftAligned());
|
||||
var age = AnsiConsole.Prompt(
|
||||
|
||||
return AnsiConsole.Prompt(
|
||||
new TextPrompt<int>("How [green]old[/] are you?")
|
||||
.PromptStyle("green")
|
||||
.ValidationErrorMessage("[red]That's not a valid age[/]")
|
||||
@ -52,33 +114,27 @@ namespace Cursor
|
||||
_ => ValidationResult.Success(),
|
||||
};
|
||||
}));
|
||||
}
|
||||
|
||||
// Secret
|
||||
private static string AskPassword()
|
||||
{
|
||||
AnsiConsole.WriteLine();
|
||||
AnsiConsole.Render(new Rule("[yellow]Secrets[/]").RuleStyle("grey").LeftAligned());
|
||||
var password = AnsiConsole.Prompt(
|
||||
|
||||
return AnsiConsole.Prompt(
|
||||
new TextPrompt<string>("Enter [green]password[/]?")
|
||||
.PromptStyle("red")
|
||||
.Secret());
|
||||
}
|
||||
|
||||
// Optional
|
||||
private static string AskColor()
|
||||
{
|
||||
AnsiConsole.WriteLine();
|
||||
AnsiConsole.Render(new Rule("[yellow]Optional[/]").RuleStyle("grey").LeftAligned());
|
||||
var color = AnsiConsole.Prompt(
|
||||
|
||||
return AnsiConsole.Prompt(
|
||||
new TextPrompt<string>("[grey][[Optional]][/] What is your [green]favorite color[/]?")
|
||||
.AllowEmpty());
|
||||
|
||||
// Summary
|
||||
AnsiConsole.WriteLine();
|
||||
AnsiConsole.Render(new Rule("[yellow]Results[/]").RuleStyle("grey").LeftAligned());
|
||||
AnsiConsole.Render(new Table().AddColumns("[grey]Question[/]", "[grey]Answer[/]")
|
||||
.RoundedBorder()
|
||||
.BorderColor(Color.Grey)
|
||||
.AddRow("[grey]Name[/]", name)
|
||||
.AddRow("[grey]Favorite fruit[/]", fruit)
|
||||
.AddRow("[grey]Age[/]", age.ToString())
|
||||
.AddRow("[grey]Password[/]", password)
|
||||
.AddRow("[grey]Favorite color[/]", string.IsNullOrEmpty(color) ? "Unknown" : color));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user