mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-12-23 01:55:48 +08:00
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>
This commit is contained in:
@@ -2,6 +2,8 @@ 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()
|
||||
{
|
||||
@@ -20,4 +22,67 @@ public sealed class SelectionPromptTests
|
||||
// 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");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user