Allow custom instructions for prompts

Closes #229
This commit is contained in:
Patrik Svensson 2021-03-14 23:35:08 +01:00 committed by Phil Scott
parent 9502aaf2b9
commit c2bab0ebf8
9 changed files with 105 additions and 9 deletions

View File

@ -16,6 +16,10 @@ var fruits = AnsiConsole.Prompt(
.Title("What are your [green]favorite fruits[/]?")
.NotRequired() // Not required to have a favorite fruit
.PageSize(10)
.MoreChoicesText("[grey](Move up and down to reveal more fruits)[/]")
.InstructionsText(
"[grey](Press [blue]<space>[/] to toggle a fruit, " +
"[green]<enter>[/] to accept)[/]")
.AddChoice("Apple")
.AddChoices(new[] {
"Apricot", "Avocado",

View File

@ -15,6 +15,7 @@ var fruit = AnsiConsole.Prompt(
new SelectionPrompt<string>()
.Title("What's your [green]favorite fruit[/]?")
.PageSize(10)
.MoreChoicesText("[grey](Move up and down to reveal more fruits)[/]")
.AddChoice("Apple")
.AddChoices(new[] {
"Apricot", "Avocado",

View File

@ -59,6 +59,7 @@ namespace Cursor
new MultiSelectionPrompt<string>()
.PageSize(10)
.Title("What are your [green]favorite fruits[/]?")
.InstructionsText("[grey](Press [blue]<space>[/] to toggle a fruit, [green]<enter>[/] to accept)[/]")
.AddChoices(new[]
{
"Apple", "Apricot", "Avocado", "Banana", "Blackcurrant", "Blueberry",
@ -88,8 +89,8 @@ namespace Cursor
return AnsiConsole.Prompt(
new TextPrompt<string>("What's your [green]favorite sport[/]?")
.InvalidChoiceMessage("[red]That's not a valid fruit[/]")
.DefaultValue("Lol")
.InvalidChoiceMessage("[red]That's not a sport![/]")
.DefaultValue("Sport?")
.AddChoice("Soccer")
.AddChoice("Hockey")
.AddChoice("Basketball"));

View File

@ -44,6 +44,16 @@ namespace Spectre.Console
/// </summary>
public Style? HighlightStyle { get; set; }
/// <summary>
/// Gets or sets the text that will be displayed if there are more choices to show.
/// </summary>
public string? MoreChoicesText { get; set; }
/// <summary>
/// Gets or sets the text that instructs the user of how to select items.
/// </summary>
public string? InstructionsText { get; set; }
/// <summary>
/// Gets or sets a value indicating whether or not
/// at least one selection is required.
@ -77,8 +87,11 @@ namespace Spectre.Console
}
var converter = Converter ?? TypeConverterHelper.ConvertToString;
var list = new RenderableMultiSelectionList<T>(
console, Title, PageSize, Choices,
Selected, converter, HighlightStyle,
MoreChoicesText, InstructionsText);
var list = new RenderableMultiSelectionList<T>(console, Title, PageSize, Choices, Selected, converter, HighlightStyle);
using (new RenderHookScope(console, list))
{
console.Cursor.Hide();

View File

@ -188,6 +188,42 @@ namespace Spectre.Console
return obj;
}
/// <summary>
/// Sets the text that will be displayed if there are more choices to show.
/// </summary>
/// <typeparam name="T">The prompt result type.</typeparam>
/// <param name="obj">The prompt.</param>
/// <param name="text">The text to display.</param>
/// <returns>The same instance so that multiple calls can be chained.</returns>
public static MultiSelectionPrompt<T> MoreChoicesText<T>(this MultiSelectionPrompt<T> obj, string? text)
{
if (obj is null)
{
throw new ArgumentNullException(nameof(obj));
}
obj.MoreChoicesText = text;
return obj;
}
/// <summary>
/// Sets the text that instructs the user of how to select items.
/// </summary>
/// <typeparam name="T">The prompt result type.</typeparam>
/// <param name="obj">The prompt.</param>
/// <param name="text">The text to display.</param>
/// <returns>The same instance so that multiple calls can be chained.</returns>
public static MultiSelectionPrompt<T> InstructionsText<T>(this MultiSelectionPrompt<T> obj, string? text)
{
if (obj is null)
{
throw new ArgumentNullException(nameof(obj));
}
obj.InstructionsText = text;
return obj;
}
/// <summary>
/// Requires no choice to be selected.
/// </summary>

View File

@ -8,9 +8,13 @@ namespace Spectre.Console
{
private const string Checkbox = "[[ ]]";
private const string SelectedCheckbox = "[[X]]";
private const string MoreChoicesText = "[grey](Move up and down to reveal more choices)[/]";
private const string InstructionsText = "[grey](Press <space> to select, <enter> to accept)[/]";
private readonly IAnsiConsole _console;
private readonly string? _title;
private readonly Markup _moreChoices;
private readonly Markup _instructions;
private readonly Style _highlightStyle;
public HashSet<int> Selections { get; set; }
@ -18,12 +22,15 @@ namespace Spectre.Console
public RenderableMultiSelectionList(
IAnsiConsole console, string? title, int pageSize,
List<T> choices, HashSet<int> selections,
Func<T, string>? converter, Style? highlightStyle)
Func<T, string>? converter, Style? highlightStyle,
string? moreChoicesText, string? instructionsText)
: base(console, pageSize, choices, converter)
{
_console = console ?? throw new ArgumentNullException(nameof(console));
_title = title;
_highlightStyle = highlightStyle ?? new Style(foreground: Color.Blue);
_moreChoices = new Markup(moreChoicesText ?? MoreChoicesText);
_instructions = new Markup(instructionsText ?? InstructionsText);
Selections = new HashSet<int>(selections);
}
@ -93,10 +100,12 @@ namespace Spectre.Console
if (scrollable)
{
list.Add(new Markup("[grey](Move up and down to reveal more choices)[/]"));
// (Move up and down to reveal more choices)
list.Add(_moreChoices);
}
list.Add(new Markup("[grey](Press <space> to select)[/]"));
// (Press <space> to select)
list.Add(_instructions);
return new Rows(list);
}

View File

@ -7,17 +7,23 @@ namespace Spectre.Console
internal sealed class RenderableSelectionList<T> : RenderableList<T>
{
private const string Prompt = ">";
private const string MoreChoicesText = "[grey](Move up and down to reveal more choices)[/]";
private readonly IAnsiConsole _console;
private readonly string? _title;
private readonly Markup _moreChoices;
private readonly Style _highlightStyle;
public RenderableSelectionList(IAnsiConsole console, string? title, int requestedPageSize, List<T> choices, Func<T, string>? converter, Style? highlightStyle)
public RenderableSelectionList(
IAnsiConsole console, string? title, int requestedPageSize,
List<T> choices, Func<T, string>? converter, Style? highlightStyle,
string? moreChoices)
: base(console, requestedPageSize, choices, converter)
{
_console = console ?? throw new ArgumentNullException(nameof(console));
_title = title;
_highlightStyle = highlightStyle ?? new Style(foreground: Color.Blue);
_moreChoices = new Markup(moreChoices ?? MoreChoicesText);
}
protected override int CalculatePageSize(int requestedPageSize)
@ -67,8 +73,9 @@ namespace Spectre.Console
if (scrollable)
{
// (Move up and down to reveal more choices)
list.Add(Text.Empty);
list.Add(new Markup("[grey](Move up and down to reveal more choices)[/]"));
list.Add(_moreChoices);
}
return new Rows(list);

View File

@ -38,6 +38,11 @@ namespace Spectre.Console
/// </summary>
public Style? HighlightStyle { get; set; }
/// <summary>
/// Gets or sets the text that will be displayed if there are more choices to show.
/// </summary>
public string? MoreChoicesText { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="SelectionPrompt{T}"/> class.
/// </summary>
@ -64,8 +69,10 @@ namespace Spectre.Console
}
var converter = Converter ?? TypeConverterHelper.ConvertToString;
var list = new RenderableSelectionList<T>(
console, Title, PageSize, Choices,
converter, HighlightStyle, MoreChoicesText);
var list = new RenderableSelectionList<T>(console, Title, PageSize, Choices, converter, HighlightStyle);
using (new RenderHookScope(console, list))
{
console.Cursor.Hide();

View File

@ -121,6 +121,24 @@ namespace Spectre.Console
return obj;
}
/// <summary>
/// Sets the text that will be displayed if there are more choices to show.
/// </summary>
/// <typeparam name="T">The prompt result type.</typeparam>
/// <param name="obj">The prompt.</param>
/// <param name="text">The text to display.</param>
/// <returns>The same instance so that multiple calls can be chained.</returns>
public static SelectionPrompt<T> MoreChoicesText<T>(this SelectionPrompt<T> obj, string? text)
{
if (obj is null)
{
throw new ArgumentNullException(nameof(obj));
}
obj.MoreChoicesText = text;
return obj;
}
/// <summary>
/// Sets the function to create a display string for a given choice.
/// </summary>