mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-08-04 12:57:59 +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
12
docs/input/prompts/index.cshtml
Normal file
12
docs/input/prompts/index.cshtml
Normal file
@ -0,0 +1,12 @@
|
||||
Title: Prompts
|
||||
Order: 5
|
||||
---
|
||||
|
||||
<h1>Sections</h1>
|
||||
|
||||
<ul>
|
||||
@foreach (IDocument child in OutputPages.GetChildrenOf(Document))
|
||||
{
|
||||
<li>@Html.DocumentLink(child)</li>
|
||||
}
|
||||
</ul>
|
31
docs/input/prompts/multiselection.md
Normal file
31
docs/input/prompts/multiselection.md
Normal file
@ -0,0 +1,31 @@
|
||||
Title: Multi Selection
|
||||
Order: 3
|
||||
---
|
||||
|
||||
The `MultiSelectionPrompt` can be used when you want the user to select
|
||||
one or many items from a provided list.
|
||||
|
||||
<img src="../assets/images/multiselection.gif" style="width: 100%;" />
|
||||
|
||||
# Usage
|
||||
|
||||
```csharp
|
||||
// Ask for the user's favorite fruits
|
||||
var fruits = AnsiConsole.Prompt(
|
||||
new MultiSelectionPrompt<string>()
|
||||
.Title("What are your [green]favorite fruits[/]?")
|
||||
.NotRequired() // Not required to have a favorite fruit
|
||||
.PageSize(10)
|
||||
.AddChoice("Apple")
|
||||
.AddChoices(new[] {
|
||||
"Apricot", "Avocado",
|
||||
"Banana", "Blackcurrant", "Blueberry",
|
||||
"Cherry", "Cloudberry", "Cocunut",
|
||||
}));
|
||||
|
||||
// Write the selected fruits to the terminal
|
||||
foreach (string fruit in fruits)
|
||||
{
|
||||
AnsiConsole.WriteLine(fruit);
|
||||
}
|
||||
```
|
27
docs/input/prompts/selection.md
Normal file
27
docs/input/prompts/selection.md
Normal file
@ -0,0 +1,27 @@
|
||||
Title: Selection
|
||||
Order: 1
|
||||
---
|
||||
|
||||
The `SelectionPrompt` can be used when you want the user to select
|
||||
a single item from a provided list.
|
||||
|
||||
<img src="../assets/images/selection.gif" style="width: 100%;" />
|
||||
|
||||
# Usage
|
||||
|
||||
```csharp
|
||||
// Ask for the user's favorite fruit
|
||||
var fruit = AnsiConsole.Prompt(
|
||||
new SelectionPrompt<string>()
|
||||
.Title("What's your [green]favorite fruit[/]?")
|
||||
.PageSize(10)
|
||||
.AddChoice("Apple")
|
||||
.AddChoices(new[] {
|
||||
"Apricot", "Avocado",
|
||||
"Banana", "Blackcurrant", "Blueberry",
|
||||
"Cherry", "Cloudberry", "Cocunut",
|
||||
}));
|
||||
|
||||
// Echo the fruit back to the terminal
|
||||
AnsiConsole.WriteLine($"I agree. {fruit} is tasty!");
|
||||
```
|
100
docs/input/prompts/text.md
Normal file
100
docs/input/prompts/text.md
Normal file
@ -0,0 +1,100 @@
|
||||
Title: Text
|
||||
Order: 0
|
||||
RedirectFrom: prompt
|
||||
---
|
||||
|
||||
Sometimes you want to get some input from the user, and for this
|
||||
you can use the `Prompt<TResult>`.
|
||||
|
||||
# Confirmation
|
||||
|
||||
```csharp
|
||||
if (!AnsiConsole.Confirm("Run example?"))
|
||||
{
|
||||
return;
|
||||
}
|
||||
```
|
||||
|
||||
```text
|
||||
Run example? [y/n] (y): _
|
||||
```
|
||||
|
||||
# Simple
|
||||
|
||||
```csharp
|
||||
// Ask for the user's name
|
||||
string name = AnsiConsole.Ask<string>("What's your [green]name[/]?");
|
||||
|
||||
// Ask for the user's age
|
||||
int age = AnsiConsole.Ask<int>("What's your [green]age[/]?");
|
||||
```
|
||||
|
||||
```text
|
||||
What's your name? Patrik
|
||||
What's your age? 37
|
||||
```
|
||||
|
||||
# Choices
|
||||
|
||||
```csharp
|
||||
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"));
|
||||
```
|
||||
|
||||
```text
|
||||
What's your favorite fruit? [Apple/Banana/Orange] (Orange): _
|
||||
```
|
||||
|
||||
# Validation
|
||||
|
||||
```csharp
|
||||
var age = AnsiConsole.Prompt(
|
||||
new TextPrompt<int>("What's the secret number?")
|
||||
.Validate(age =>
|
||||
{
|
||||
return age switch
|
||||
{
|
||||
< 99 => ValidationResult.Error("[red]Too low[/]"),
|
||||
> 99 => ValidationResult.Error("[red]Too high[/]"),
|
||||
_ => ValidationResult.Success(),
|
||||
};
|
||||
}));
|
||||
```
|
||||
|
||||
```text
|
||||
What's the secret number? 32
|
||||
Too low
|
||||
What's the secret number? 102
|
||||
Too high
|
||||
What's the secret number? _
|
||||
```
|
||||
|
||||
# Secrets
|
||||
|
||||
```csharp
|
||||
var password = AnsiConsole.Prompt(
|
||||
new TextPrompt<string>("Enter [green]password[/]")
|
||||
.PromptStyle("red")
|
||||
.Secret());
|
||||
```
|
||||
|
||||
```text
|
||||
Enter password: ************_
|
||||
```
|
||||
|
||||
# Optional
|
||||
|
||||
```csharp
|
||||
var color = AnsiConsole.Prompt(
|
||||
new TextPrompt<string>("[grey][[Optional]][/] [green]Favorite color[/]?")
|
||||
.AllowEmpty());
|
||||
```
|
||||
|
||||
```text
|
||||
[Optional] Favorite color? _
|
||||
```
|
Reference in New Issue
Block a user