Skip to content

Commit 9aeb9e7

Browse files
merklegrootpatriksvensson
authored andcommitted
Fix #1638: Add missing text prompt suffix
1 parent e056e38 commit 9aeb9e7

4 files changed

Lines changed: 40 additions & 5 deletions

File tree

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
Enter a value ************
1+
Enter a value: ************
22

33

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
no default, with suffix: input
2+

src/Spectre.Console.Tests/Unit/Prompts/TextPromptTests.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,22 @@ public Task Should_Not_Append_Questionmark_Or_Colon_If_No_Choices_Are_Set()
319319
return Verifier.Verify(console.Output);
320320
}
321321

322+
[Fact]
323+
[Expectation("Issue_1638")]
324+
public Task Should_Append_Colon_When_No_Default_Value_Is_Set()
325+
{
326+
// Given
327+
var console = new TestConsole();
328+
console.Input.PushTextWithEnter("input");
329+
330+
// When
331+
console.Prompt(
332+
new TextPrompt<string>("no default, with suffix"));
333+
334+
// Then
335+
return Verifier.Verify(console.Output);
336+
}
337+
322338
[Fact]
323339
[Expectation("DefaultValueStyleNotSet")]
324340
public Task Uses_default_style_for_default_value_if_no_style_is_set()

src/Spectre.Console/Prompts/TextPrompt.cs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -219,10 +219,10 @@ private void WritePrompt(IAnsiConsole console)
219219
var builder = new StringBuilder();
220220
builder.Append(_prompt.TrimEnd());
221221

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

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

246246
var markup = builder.ToString().Trim();
247-
if (appendSuffix)
247+
if (ShouldAppendColon(markup, hasPromptDetails))
248248
{
249249
markup += ":";
250250
}
251251

252252
console.Markup(markup + " ");
253253
}
254254

255+
/// <summary>
256+
/// A colon should be appended when prompt details are rendered, or when a plain prompt does not already end with punctuation.
257+
/// </summary>
258+
/// <param name="markup">The prompt markup.</param>
259+
/// <param name="hasPromptDetails">Whether the prompt includes choices or a default value.</param>
260+
/// <returns>Whether a colon should be appended.</returns>
261+
private static bool ShouldAppendColon(string markup, bool hasPromptDetails)
262+
{
263+
if (hasPromptDetails)
264+
{
265+
return true;
266+
}
267+
268+
var prompt = Markup.Remove(markup).TrimEnd();
269+
return prompt.Length > 0 && char.IsLetterOrDigit(prompt[^1]);
270+
}
271+
255272
/// <summary>
256273
/// Clears the prompt line when enabled.
257274
/// </summary>

0 commit comments

Comments
 (0)