diff --git a/docs/input/prompts/multiselection.md b/docs/input/prompts/multiselection.md index 2e85133..d5ed617 100644 --- a/docs/input/prompts/multiselection.md +++ b/docs/input/prompts/multiselection.md @@ -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][/] to toggle a fruit, " + + "[green][/] to accept)[/]") .AddChoice("Apple") .AddChoices(new[] { "Apricot", "Avocado", diff --git a/docs/input/prompts/selection.md b/docs/input/prompts/selection.md index e3f846f..620b0e8 100644 --- a/docs/input/prompts/selection.md +++ b/docs/input/prompts/selection.md @@ -15,6 +15,7 @@ var fruit = AnsiConsole.Prompt( new SelectionPrompt() .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", diff --git a/examples/Console/Prompt/Program.cs b/examples/Console/Prompt/Program.cs index 1822e4f..2dc5127 100644 --- a/examples/Console/Prompt/Program.cs +++ b/examples/Console/Prompt/Program.cs @@ -59,6 +59,7 @@ namespace Cursor new MultiSelectionPrompt() .PageSize(10) .Title("What are your [green]favorite fruits[/]?") + .InstructionsText("[grey](Press [blue][/] to toggle a fruit, [green][/] to accept)[/]") .AddChoices(new[] { "Apple", "Apricot", "Avocado", "Banana", "Blackcurrant", "Blueberry", @@ -88,8 +89,8 @@ namespace Cursor return AnsiConsole.Prompt( new TextPrompt("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")); diff --git a/src/Spectre.Console/Widgets/Prompt/MultiSelectionPrompt.cs b/src/Spectre.Console/Widgets/Prompt/MultiSelectionPrompt.cs index 43131d8..260c195 100644 --- a/src/Spectre.Console/Widgets/Prompt/MultiSelectionPrompt.cs +++ b/src/Spectre.Console/Widgets/Prompt/MultiSelectionPrompt.cs @@ -44,6 +44,16 @@ namespace Spectre.Console /// public Style? HighlightStyle { get; set; } + /// + /// Gets or sets the text that will be displayed if there are more choices to show. + /// + public string? MoreChoicesText { get; set; } + + /// + /// Gets or sets the text that instructs the user of how to select items. + /// + public string? InstructionsText { get; set; } + /// /// 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( + console, Title, PageSize, Choices, + Selected, converter, HighlightStyle, + MoreChoicesText, InstructionsText); - var list = new RenderableMultiSelectionList(console, Title, PageSize, Choices, Selected, converter, HighlightStyle); using (new RenderHookScope(console, list)) { console.Cursor.Hide(); diff --git a/src/Spectre.Console/Widgets/Prompt/MultiSelectionPromptExtensions.cs b/src/Spectre.Console/Widgets/Prompt/MultiSelectionPromptExtensions.cs index 4d0609d..ee2c6c8 100644 --- a/src/Spectre.Console/Widgets/Prompt/MultiSelectionPromptExtensions.cs +++ b/src/Spectre.Console/Widgets/Prompt/MultiSelectionPromptExtensions.cs @@ -188,6 +188,42 @@ namespace Spectre.Console return obj; } + /// + /// Sets the text that will be displayed if there are more choices to show. + /// + /// The prompt result type. + /// The prompt. + /// The text to display. + /// The same instance so that multiple calls can be chained. + public static MultiSelectionPrompt MoreChoicesText(this MultiSelectionPrompt obj, string? text) + { + if (obj is null) + { + throw new ArgumentNullException(nameof(obj)); + } + + obj.MoreChoicesText = text; + return obj; + } + + /// + /// Sets the text that instructs the user of how to select items. + /// + /// The prompt result type. + /// The prompt. + /// The text to display. + /// The same instance so that multiple calls can be chained. + public static MultiSelectionPrompt InstructionsText(this MultiSelectionPrompt obj, string? text) + { + if (obj is null) + { + throw new ArgumentNullException(nameof(obj)); + } + + obj.InstructionsText = text; + return obj; + } + /// /// Requires no choice to be selected. /// diff --git a/src/Spectre.Console/Widgets/Prompt/Rendering/RenderableMultiSelectionList.cs b/src/Spectre.Console/Widgets/Prompt/Rendering/RenderableMultiSelectionList.cs index 36ce727..f28ace1 100644 --- a/src/Spectre.Console/Widgets/Prompt/Rendering/RenderableMultiSelectionList.cs +++ b/src/Spectre.Console/Widgets/Prompt/Rendering/RenderableMultiSelectionList.cs @@ -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 to select, to accept)[/]"; private readonly IAnsiConsole _console; private readonly string? _title; + private readonly Markup _moreChoices; + private readonly Markup _instructions; private readonly Style _highlightStyle; public HashSet Selections { get; set; } @@ -18,12 +22,15 @@ namespace Spectre.Console public RenderableMultiSelectionList( IAnsiConsole console, string? title, int pageSize, List choices, HashSet selections, - Func? converter, Style? highlightStyle) + Func? 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(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 to select)[/]")); + // (Press to select) + list.Add(_instructions); return new Rows(list); } diff --git a/src/Spectre.Console/Widgets/Prompt/Rendering/RenderableSelectionList.cs b/src/Spectre.Console/Widgets/Prompt/Rendering/RenderableSelectionList.cs index 0bc6425..552aab5 100644 --- a/src/Spectre.Console/Widgets/Prompt/Rendering/RenderableSelectionList.cs +++ b/src/Spectre.Console/Widgets/Prompt/Rendering/RenderableSelectionList.cs @@ -7,17 +7,23 @@ namespace Spectre.Console internal sealed class RenderableSelectionList : RenderableList { 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 choices, Func? converter, Style? highlightStyle) + public RenderableSelectionList( + IAnsiConsole console, string? title, int requestedPageSize, + List choices, Func? 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); diff --git a/src/Spectre.Console/Widgets/Prompt/SelectionPrompt.cs b/src/Spectre.Console/Widgets/Prompt/SelectionPrompt.cs index 8ac7b40..81c8f25 100644 --- a/src/Spectre.Console/Widgets/Prompt/SelectionPrompt.cs +++ b/src/Spectre.Console/Widgets/Prompt/SelectionPrompt.cs @@ -38,6 +38,11 @@ namespace Spectre.Console /// public Style? HighlightStyle { get; set; } + /// + /// Gets or sets the text that will be displayed if there are more choices to show. + /// + public string? MoreChoicesText { get; set; } + /// /// Initializes a new instance of the class. /// @@ -64,8 +69,10 @@ namespace Spectre.Console } var converter = Converter ?? TypeConverterHelper.ConvertToString; + var list = new RenderableSelectionList( + console, Title, PageSize, Choices, + converter, HighlightStyle, MoreChoicesText); - var list = new RenderableSelectionList(console, Title, PageSize, Choices, converter, HighlightStyle); using (new RenderHookScope(console, list)) { console.Cursor.Hide(); diff --git a/src/Spectre.Console/Widgets/Prompt/SelectionPromptExtensions.cs b/src/Spectre.Console/Widgets/Prompt/SelectionPromptExtensions.cs index cbbc8f4..80d1f26 100644 --- a/src/Spectre.Console/Widgets/Prompt/SelectionPromptExtensions.cs +++ b/src/Spectre.Console/Widgets/Prompt/SelectionPromptExtensions.cs @@ -121,6 +121,24 @@ namespace Spectre.Console return obj; } + /// + /// Sets the text that will be displayed if there are more choices to show. + /// + /// The prompt result type. + /// The prompt. + /// The text to display. + /// The same instance so that multiple calls can be chained. + public static SelectionPrompt MoreChoicesText(this SelectionPrompt obj, string? text) + { + if (obj is null) + { + throw new ArgumentNullException(nameof(obj)); + } + + obj.MoreChoicesText = text; + return obj; + } + /// /// Sets the function to create a display string for a given choice. ///