mirror of
				https://github.com/nsnail/spectre.console.git
				synced 2025-11-01 01:25:27 +08:00 
			
		
		
		
	mask default value when prompt is a secret
This commit is contained in:
		 Thomas Freudenberg
					Thomas Freudenberg
				
			
				
					committed by
					
						 Patrik Svensson
						Patrik Svensson
					
				
			
			
				
	
			
			
			 Patrik Svensson
						Patrik Svensson
					
				
			
						parent
						
							a3e11b24e5
						
					
				
				
					commit
					dee3c01629
				
			| @@ -0,0 +1 @@ | ||||
| Favorite fruit? (******): ****** | ||||
| @@ -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); | ||||
|         } | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -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(); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user