Add default value parameter to Confirm extension

Closes #482
This commit is contained in:
Antonio Valentini 2021-07-12 08:47:36 +02:00 committed by GitHub
parent 5f97f2300c
commit d532e1011f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 2 deletions

View File

@ -41,10 +41,15 @@ namespace Spectre.Console
/// </summary>
/// <param name="console">The console.</param>
/// <param name="prompt">The prompt markup text.</param>
/// <param name="defaultValue">Specifies the default answer.</param>
/// <returns><c>true</c> if the user selected "yes", otherwise <c>false</c>.</returns>
public static bool Confirm(this IAnsiConsole console, string prompt)
public static bool Confirm(this IAnsiConsole console, string prompt, bool defaultValue = true)
{
return new ConfirmationPrompt(prompt).Show(console);
return new ConfirmationPrompt(prompt)
{
DefaultValue = defaultValue,
}
.Show(console);
}
}
}

View File

@ -0,0 +1,29 @@
using System;
using Shouldly;
using Spectre.Console.Testing;
using Xunit;
namespace Spectre.Console.Tests.Unit
{
public partial class AnsiConsoleTests
{
public sealed class Prompt
{
[Theory]
[InlineData(true, true)]
[InlineData(false, false)]
public void Should_Return_Default_Value_If_Nothing_Is_Entered(bool expected, bool defaultValue)
{
// Given
var console = new TestConsole().EmitAnsiSequences();
console.Input.PushKey(ConsoleKey.Enter);
// When
var result = console.Confirm("Want some prompt?", defaultValue);
// Then
result.ShouldBe(expected);
}
}
}
}