Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Enter a value ************
Enter a value: ************


Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
no default, with suffix: input

16 changes: 16 additions & 0 deletions src/Spectre.Console.Tests/Unit/Prompts/TextPromptTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,22 @@ public Task Should_Not_Append_Questionmark_Or_Colon_If_No_Choices_Are_Set()
return Verifier.Verify(console.Output);
}

[Fact]
[Expectation("Issue_1638")]
public Task Should_Append_Colon_When_No_Default_Value_Is_Set()
{
// Given
var console = new TestConsole();
console.Input.PushTextWithEnter("input");

// When
console.Prompt(
new TextPrompt<string>("no default, with suffix"));

// Then
return Verifier.Verify(console.Output);
}

[Fact]
[Expectation("DefaultValueStyleNotSet")]
public Task Uses_default_style_for_default_value_if_no_style_is_set()
Expand Down
25 changes: 21 additions & 4 deletions src/Spectre.Console/Prompts/TextPrompt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,10 @@ private void WritePrompt(IAnsiConsole console)
var builder = new StringBuilder();
builder.Append(_prompt.TrimEnd());

var appendSuffix = false;
var hasPromptDetails = false;
if (ShowChoices && Choices.Count > 0)
{
appendSuffix = true;
hasPromptDetails = true;
var converter = Converter ?? TypeConverterHelper.ConvertToString;
var choices = string.Join("/", Choices.Select(choice => converter(choice)));
var choicesStyle = ChoicesStyle?.ToMarkup() ?? "blue";
Expand All @@ -231,7 +231,7 @@ private void WritePrompt(IAnsiConsole console)

if (ShowDefaultValue && DefaultValue != null)
{
appendSuffix = true;
hasPromptDetails = true;
var converter = Converter ?? TypeConverterHelper.ConvertToString;
var defaultValueStyle = DefaultValueStyle?.ToMarkup() ?? "green";
var defaultValue = converter(DefaultValue.Value);
Expand All @@ -244,14 +244,31 @@ private void WritePrompt(IAnsiConsole console)
}

var markup = builder.ToString().Trim();
if (appendSuffix)
if (ShouldAppendColon(markup, hasPromptDetails))
{
markup += ":";
}

console.Markup(markup + " ");
}

/// <summary>
/// A colon should be appended when prompt details are rendered, or when a plain prompt does not already end with punctuation.
/// </summary>
/// <param name="markup">The prompt markup.</param>
/// <param name="hasPromptDetails">Whether the prompt includes choices or a default value.</param>
/// <returns>Whether a colon should be appended.</returns>
private static bool ShouldAppendColon(string markup, bool hasPromptDetails)
{
if (hasPromptDetails)
{
return true;
}

var prompt = Markup.Remove(markup).TrimEnd();
return prompt.Length > 0 && char.IsLetterOrDigit(prompt[^1]);
}

/// <summary>
/// Clears the prompt line when enabled.
/// </summary>
Expand Down