Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Confirmation Prompt Styling #1210

Merged
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
34 changes: 34 additions & 0 deletions src/Spectre.Console/Extensions/ConfirmationPromptExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,23 @@ public static ConfirmationPrompt HideChoices(this ConfirmationPrompt obj)
return ShowChoices(obj, false);
}

/// <summary>
/// Sets the style in which the list of choices is displayed.
/// </summary>
/// <param name="obj">The confirmation prompt.</param>
/// <param name="style">The style to use for displaying the choices or <see langword="null"/> to use the default style (blue).</param>
/// <returns>The same instance so that multiple calls can be chained.</returns>
public static ConfirmationPrompt ChoicesStyle(this ConfirmationPrompt obj, Style? style)
{
if (obj is null)
{
throw new ArgumentNullException(nameof(obj));
}

obj.ChoicesStyle = style;
return obj;
}

/// <summary>
/// Show or hide the default value.
/// </summary>
Expand Down Expand Up @@ -79,6 +96,23 @@ public static ConfirmationPrompt HideDefaultValue(this ConfirmationPrompt obj)
return ShowDefaultValue(obj, false);
}

/// <summary>
/// Sets the style in which the default value is displayed.
/// </summary>
/// <param name="obj">The confirmation prompt.</param>
/// <param name="style">The default value style or <see langword="null"/> to use the default style (green).</param>
/// <returns>The same instance so that multiple calls can be chained.</returns>
public static ConfirmationPrompt DefaultValueStyle(this ConfirmationPrompt obj, Style? style)
{
if (obj is null)
{
throw new ArgumentNullException(nameof(obj));
}

obj.DefaultValueStyle = style;
return obj;
}

/// <summary>
/// Sets the "invalid choice" message for the prompt.
/// </summary>
Expand Down
12 changes: 12 additions & 0 deletions src/Spectre.Console/Prompts/ConfirmationPrompt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ public sealed class ConfirmationPrompt : IPrompt<bool>
/// </summary>
public bool ShowDefaultValue { get; set; } = true;

/// <summary>
/// Gets or sets the style in which the default value is displayed. Defaults to green when <see langword="null"/>.
/// </summary>
public Style? DefaultValueStyle { get; set; }

/// <summary>
/// Gets or sets the style in which the list of choices is displayed. Defaults to blue when <see langword="null"/>.
/// </summary>
public Style? ChoicesStyle { get; set; }

/// <summary>
/// Gets or sets the string comparer to use when comparing user input
/// against Yes/No choices.
Expand Down Expand Up @@ -72,8 +82,10 @@ public async Task<bool> ShowAsync(IAnsiConsole console, CancellationToken cancel
.InvalidChoiceMessage(InvalidChoiceMessage)
.ValidationErrorMessage(InvalidChoiceMessage)
.ShowChoices(ShowChoices)
.ChoicesStyle(ChoicesStyle)
.ShowDefaultValue(ShowDefaultValue)
.DefaultValue(DefaultValue ? Yes : No)
.DefaultValueStyle(DefaultValueStyle)
.AddChoice(Yes)
.AddChoice(No);

Expand Down
Loading