diff --git a/examples/Console/Prompt/Program.cs b/examples/Console/Prompt/Program.cs index 00a81eb..67421db 100644 --- a/examples/Console/Prompt/Program.cs +++ b/examples/Console/Prompt/Program.cs @@ -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("[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; + } + } } diff --git a/src/Spectre.Console/AnsiConsole.Prompt.cs b/src/Spectre.Console/AnsiConsole.Prompt.cs index c1bb92f..a0d7a6b 100644 --- a/src/Spectre.Console/AnsiConsole.Prompt.cs +++ b/src/Spectre.Console/AnsiConsole.Prompt.cs @@ -34,6 +34,20 @@ namespace Spectre.Console return new TextPrompt(prompt).Show(Console); } + /// + /// Displays a prompt to the user with a given default. + /// + /// The prompt result type. + /// The prompt markup text. + /// The default value. + /// The prompt input result. + public static T Ask(string prompt, T defaultValue) + { + return new TextPrompt(prompt) + .DefaultValue(defaultValue) + .Show(Console); + } + /// /// Displays a prompt with two choices, yes or no. ///