mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-04-16 08:52:50 +08:00
(#460) added an overload to AnsiConsole.Ask
that takes an additional default value.
This commit is contained in:
parent
c9b178ac96
commit
b92827ce3d
@ -25,6 +25,7 @@ namespace Spectre.Console.Examples
|
||||
var age = AskAge();
|
||||
var password = AskPassword();
|
||||
var color = AskColor();
|
||||
var origin = AskOrigin();
|
||||
|
||||
// Summary
|
||||
AnsiConsole.WriteLine();
|
||||
@ -37,7 +38,8 @@ namespace Spectre.Console.Examples
|
||||
.AddRow("[grey]Favorite sport[/]", sport)
|
||||
.AddRow("[grey]Age[/]", age.ToString())
|
||||
.AddRow("[grey]Password[/]", password)
|
||||
.AddRow("[grey]Favorite color[/]", string.IsNullOrEmpty(color) ? "Unknown" : color));
|
||||
.AddRow("[grey]Favorite color[/]", string.IsNullOrEmpty(color) ? "Unknown" : color)
|
||||
.AddRow("[grey]Origin[/]", origin));
|
||||
}
|
||||
|
||||
private static string AskName()
|
||||
@ -141,5 +143,14 @@ namespace Spectre.Console.Examples
|
||||
new TextPrompt<string>("[grey][[Optional]][/] What is your [green]favorite color[/]?")
|
||||
.AllowEmpty());
|
||||
}
|
||||
|
||||
private static string AskOrigin()
|
||||
{
|
||||
AnsiConsole.WriteLine();
|
||||
AnsiConsole.Render(new Rule("[yellow]Default answer[/]").RuleStyle("grey").LeftAligned());
|
||||
var name = AnsiConsole.Ask("Where are you [green]from[/]?", "Earth");
|
||||
return name;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -34,6 +34,20 @@ namespace Spectre.Console
|
||||
return new TextPrompt<T>(prompt).Show(Console);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Displays a prompt to the user with a given default.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The prompt result type.</typeparam>
|
||||
/// <param name="prompt">The prompt markup text.</param>
|
||||
/// <param name="defaultValue">The default value.</param>
|
||||
/// <returns>The prompt input result.</returns>
|
||||
public static T Ask<T>(string prompt, T defaultValue)
|
||||
{
|
||||
return new TextPrompt<T>(prompt)
|
||||
.DefaultValue(defaultValue)
|
||||
.Show(Console);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Displays a prompt with two choices, yes or no.
|
||||
/// </summary>
|
||||
|
Loading…
x
Reference in New Issue
Block a user