Stuart Lang 397b742bec
Add selection orompt Search (#1289)
* Add selection prompt search as you type

* Fix small bug

* Simplify

* Simplify

* Remove spacebar as a selection prompt submit key

* Trigger CI

* Update src/Spectre.Console/Prompts/SelectionPrompt.cs

Co-authored-by: Martin Costello <martin@martincostello.com>

* Simplifty Mask method

* Handle multi-selection prompt better

* Update API naming

* Address feedback

* Add some tests

* Remove whitespace

* Improve search and highlighting

* Add test case for previous issue

* Add extra test case

* Make prompt searchable

---------

Co-authored-by: Martin Costello <martin@martincostello.com>
Co-authored-by: Patrik Svensson <patrik@patriksvensson.se>
2024-02-25 12:57:27 +01:00

89 lines
2.6 KiB
C#

namespace Spectre.Console.Tests.Unit;
public sealed class SelectionPromptTests
{
private const string ESC = "\u001b";
[Fact]
public void Should_Not_Throw_When_Selecting_An_Item_With_Escaped_Markup()
{
// Given
var console = new TestConsole();
console.Profile.Capabilities.Interactive = true;
console.Input.PushKey(ConsoleKey.Enter);
var input = "[red]This text will never be red[/]".EscapeMarkup();
// When
var prompt = new SelectionPrompt<string>()
.Title("Select one")
.AddChoices(input);
prompt.Show(console);
// Then
console.Output.ShouldContain(@"[red]This text will never be red[/]");
}
[Fact]
public void Should_Select_The_First_Leaf_Item()
{
// Given
var console = new TestConsole();
console.Profile.Capabilities.Interactive = true;
console.Input.PushKey(ConsoleKey.Enter);
// When
var prompt = new SelectionPrompt<string>()
.Title("Select one")
.Mode(SelectionMode.Leaf)
.AddChoiceGroup("Group one", "A", "B")
.AddChoiceGroup("Group two", "C", "D");
var selection = prompt.Show(console);
// Then
selection.ShouldBe("A");
}
[Fact]
public void Should_Select_The_Last_Leaf_Item_When_Wrapping_Around()
{
// Given
var console = new TestConsole();
console.Profile.Capabilities.Interactive = true;
console.Input.PushKey(ConsoleKey.UpArrow);
console.Input.PushKey(ConsoleKey.Enter);
// When
var prompt = new SelectionPrompt<string>()
.Title("Select one")
.Mode(SelectionMode.Leaf)
.WrapAround()
.AddChoiceGroup("Group one", "A", "B")
.AddChoiceGroup("Group two", "C", "D");
var selection = prompt.Show(console);
// Then
selection.ShouldBe("D");
}
[Fact]
public void Should_Highlight_Search_Term()
{
// Given
var console = new TestConsole();
console.Profile.Capabilities.Interactive = true;
console.EmitAnsiSequences();
console.Input.PushText("1");
console.Input.PushKey(ConsoleKey.Enter);
// When
var prompt = new SelectionPrompt<string>()
.Title("Select one")
.EnableSearch()
.AddChoices("Item 1");
prompt.Show(console);
// Then
console.Output.ShouldContain($"{ESC}[38;5;12m> Item {ESC}[0m{ESC}[1;38;5;12;48;5;11m1{ESC}[0m");
}
}