Skip to content
Open
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
20 changes: 19 additions & 1 deletion src/Spectre.Console/Extensions/AnsiConsoleExtensions.Input.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,23 @@ namespace Spectre.Console;
/// </summary>
public static partial class AnsiConsoleExtensions
{
internal static async Task<string> ReadLine(this IAnsiConsole console, Style? style, bool secret, char? mask, IEnumerable<string>? items = null, CancellationToken cancellationToken = default)
internal static async Task<string> ReadLine(this IAnsiConsole console, Style? style, bool secret, char? mask, IEnumerable<string>? items = null, string? placeholder = null, Style? placeholderStyle = null, CancellationToken cancellationToken = default)
{
if (console is null)
{
throw new ArgumentNullException(nameof(console));
}

style ??= Style.Plain;
placeholderStyle ??= new Style(foreground: Color.Grey);
var text = string.Empty;
var showPlaceholder = !string.IsNullOrEmpty(placeholder);

var autocomplete = new List<string>(items ?? Enumerable.Empty<string>());
if (showPlaceholder && string.IsNullOrEmpty(text))
{
console.Write(placeholder!, placeholderStyle);
}

while (true)
{
Expand All @@ -29,6 +35,11 @@ internal static async Task<string> ReadLine(this IAnsiConsole console, Style? st
var key = rawKey.Value;
if (key.Key == ConsoleKey.Enter)
{
if (showPlaceholder && string.IsNullOrEmpty(text))
{
console.Write("\b \b".Repeat(placeholder!.Length));
}

return text;
}

Expand Down Expand Up @@ -73,6 +84,13 @@ internal static async Task<string> ReadLine(this IAnsiConsole console, Style? st

if (!char.IsControl(key.KeyChar))
{
// Delete place holder when a key is press
if (showPlaceholder && string.IsNullOrEmpty(text))
{
console.Write("\b \b".Repeat(placeholder!.Length));
showPlaceholder = false;
}

text += key.KeyChar.ToString();
var output = key.KeyChar.ToString();
console.Write(secret ? output.Mask(mask) : output, style);
Expand Down
13 changes: 12 additions & 1 deletion src/Spectre.Console/Prompts/TextPrompt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,17 @@ public sealed class TextPrompt<T> : IPrompt<T>, IHasCulture
/// </summary>
internal DefaultPromptValue<T>? DefaultValue { get; set; }

/// <summary>
/// Gets or sets the placeholder text (displayed when input is empty).
/// </summary>
public string? Placeholder { get; set; }

/// <summary>
/// Gets or sets the style for the placeholder.
/// Defaults to grey if null.
/// </summary>
public Style? PlaceholderStyle { get; set; }

/// <summary>
/// Initializes a new instance of the <see cref="TextPrompt{T}"/> class.
/// </summary>
Expand Down Expand Up @@ -130,7 +141,7 @@ public async Task<T> ShowAsync(IAnsiConsole console, CancellationToken cancellat

while (true)
{
var input = await console.ReadLine(promptStyle, IsSecret, Mask, choices, cancellationToken).ConfigureAwait(false);
var input = await console.ReadLine(promptStyle, IsSecret, Mask, choices, Placeholder, PlaceholderStyle, cancellationToken).ConfigureAwait(false);

// Nothing entered?
if (string.IsNullOrWhiteSpace(input))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Enter your name: ex: Bob       Alice
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Enter Value: ex: 5     2
50 changes: 50 additions & 0 deletions src/Tests/Spectre.Console.Tests/Unit/Prompts/TextPromptTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -410,4 +410,54 @@ public Task Uses_the_specified_choices_style()
// Then
return Verifier.Verify(console.Output);
}


[Fact]
[Expectation("Placeholder_Shown")]
public Task Should_Display_Placeholder_When_Input_Is_Empty()
{
// Given
var console = new TestConsole
{
EmitAnsiSequences = false
};

console.Input.PushTextWithEnter("Alice");

// When
var prompt = new TextPrompt<string>("Enter your name:")
{
Placeholder = "ex: Bob",
};
console.Prompt(prompt);

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

[Fact]
[Expectation("Placeholder_Style_Applied")]
public Task Should_Use_Specified_Style_For_Placeholder()
{
// Given
var console = new TestConsole
{
EmitAnsiSequences = true
};

console.Input.PushTextWithEnter("2");

// When
var prompt = new TextPrompt<string>("Enter Value:")
{
Placeholder = "ex: 5",
PlaceholderStyle = Color.Red
};

console.Prompt(prompt);

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

}