mask default value when prompt is a secret

This commit is contained in:
Thomas Freudenberg 2021-01-13 21:12:44 +01:00 committed by Patrik Svensson
parent a3e11b24e5
commit dee3c01629
3 changed files with 22 additions and 2 deletions

View File

@ -0,0 +1 @@
Favorite fruit? (******): ******

View File

@ -200,5 +200,23 @@ namespace Spectre.Console.Tests.Unit
result.Item1.ShouldBe(2);
return Verifier.Verify(console.Output);
}
[Fact]
[Expectation("SecretDefaultValue")]
public Task Should_Chose_Masked_Default_Value_If_Nothing_Is_Entered_And_Prompt_Is_Secret()
{
// Given
var console = new FakeConsole();
console.Input.PushKey(ConsoleKey.Enter);
// When
console.Prompt(
new TextPrompt<string>("Favorite fruit?")
.Secret()
.DefaultValue("Banana"));
// Then
return Verifier.Verify(console.Output);
}
}
}

View File

@ -117,7 +117,7 @@ namespace Spectre.Console
{
if (DefaultValue != null)
{
console.Write(converter(DefaultValue.Value), promptStyle);
console.Write(IsSecret ? "******" : converter(DefaultValue.Value), promptStyle);
console.WriteLine();
return DefaultValue.Value;
}
@ -186,10 +186,11 @@ namespace Spectre.Console
if (ShowDefaultValue && DefaultValue != null)
{
var converter = Converter ?? TypeConverterHelper.ConvertToString;
builder.AppendFormat(
CultureInfo.InvariantCulture,
" [green]({0})[/]",
TypeConverterHelper.ConvertToString(DefaultValue.Value));
IsSecret ? "******" : converter(DefaultValue.Value));
}
var markup = builder.ToString().Trim();