diff --git a/global.json b/global.json index 341e047..9f67e6e 100644 --- a/global.json +++ b/global.json @@ -1,7 +1,7 @@ { "projects": [ "src", "tests" ], "sdk": { - "version": "5.0.202", + "version": "5.0.301", "rollForward": "latestPatch" } } \ No newline at end of file diff --git a/src/Spectre.Console/Widgets/Prompt/TextPrompt.cs b/src/Spectre.Console/Widgets/Prompt/TextPrompt.cs index b570781..bf793ca 100644 --- a/src/Spectre.Console/Widgets/Prompt/TextPrompt.cs +++ b/src/Spectre.Console/Widgets/Prompt/TextPrompt.cs @@ -179,8 +179,10 @@ namespace Spectre.Console var builder = new StringBuilder(); builder.Append(_prompt.TrimEnd()); + var appendSuffix = false; if (ShowChoices && Choices.Count > 0) { + appendSuffix = true; var converter = Converter ?? TypeConverterHelper.ConvertToString; var choices = string.Join("/", Choices.Select(choice => converter(choice))); builder.AppendFormat(CultureInfo.InvariantCulture, " [blue][[{0}]][/]", choices); @@ -188,6 +190,7 @@ namespace Spectre.Console if (ShowDefaultValue && DefaultValue != null) { + appendSuffix = true; var converter = Converter ?? TypeConverterHelper.ConvertToString; builder.AppendFormat( CultureInfo.InvariantCulture, @@ -196,8 +199,7 @@ namespace Spectre.Console } var markup = builder.ToString().Trim(); - if (!markup.EndsWith("?", StringComparison.OrdinalIgnoreCase) && - !markup.EndsWith(":", StringComparison.OrdinalIgnoreCase)) + if (appendSuffix) { markup += ":"; } diff --git a/test/Spectre.Console.Tests/Expectations/Widgets/Prompt/Text/NoSuffix.Output.verified.txt b/test/Spectre.Console.Tests/Expectations/Widgets/Prompt/Text/NoSuffix.Output.verified.txt new file mode 100644 index 0000000..6be34fd --- /dev/null +++ b/test/Spectre.Console.Tests/Expectations/Widgets/Prompt/Text/NoSuffix.Output.verified.txt @@ -0,0 +1 @@ +Enter command$ Orange diff --git a/test/Spectre.Console.Tests/Unit/TextPromptTests.cs b/test/Spectre.Console.Tests/Unit/TextPromptTests.cs index 1f28fad..f560b87 100644 --- a/test/Spectre.Console.Tests/Unit/TextPromptTests.cs +++ b/test/Spectre.Console.Tests/Unit/TextPromptTests.cs @@ -12,6 +12,20 @@ namespace Spectre.Console.Tests.Unit [ExpectationPath("Widgets/Prompt/Text")] public sealed class TextPromptTests { + [Fact] + public void Should_Return_Entered_Text() + { + // Given + var console = new TestConsole(); + console.Input.PushTextWithEnter("Hello World"); + + // When + var result = console.Prompt(new TextPrompt("Enter text:")); + + // Then + result.ShouldBe("Hello World"); + } + [Fact] [Expectation("ConversionError")] public Task Should_Return_Validation_Error_If_Value_Cannot_Be_Converted() @@ -218,5 +232,22 @@ namespace Spectre.Console.Tests.Unit // Then return Verifier.Verify(console.Output); } + + [Fact] + [Expectation("NoSuffix")] + [GitHubIssue(413)] + public Task Should_Not_Append_Questionmark_Or_Colon_If_No_Choices_Are_Set() + { + // Given + var console = new TestConsole(); + console.Input.PushTextWithEnter("Orange"); + + // When + console.Prompt( + new TextPrompt("Enter command$")); + + // Then + return Verifier.Verify(console.Output); + } } } diff --git a/test/Spectre.Console.Tests/Utilities/GitHubIssueAttribute.cs b/test/Spectre.Console.Tests/Utilities/GitHubIssueAttribute.cs new file mode 100644 index 0000000..659c9c7 --- /dev/null +++ b/test/Spectre.Console.Tests/Utilities/GitHubIssueAttribute.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Spectre.Console.Tests +{ + public sealed class GitHubIssueAttribute : Attribute + { + public int IssueId { get; } + + public GitHubIssueAttribute(int issueId) + { + IssueId = issueId; + } + } +}