Skip to content

Commit

Permalink
Adds prompt tree
Browse files Browse the repository at this point in the history
  • Loading branch information
assaftir committed Jun 30, 2023
1 parent d484e83 commit 8b0cd77
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 0 deletions.
62 changes: 62 additions & 0 deletions examples/Console/Prompt/Program.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Collections.Generic;
using System.Linq;
using Spectre.Console;

namespace Prompt
Expand Down Expand Up @@ -26,6 +28,9 @@ public static void Main(string[] args)
WriteDivider("Lists");
var fruit = AskFruit();

WriteDivider("Prompt Tree");
var deepSpaceObjects = AskDeepSpaceObjects();

WriteDivider("Choices");
var sport = AskSport();

Expand All @@ -52,6 +57,7 @@ public static void Main(string[] args)
.BorderColor(Color.Grey)
.AddRow("[grey]Name[/]", name)
.AddRow("[grey]Favorite fruit[/]", fruit)
.AddRow("[grey]Favorite DSOs[/]", string.Join(", ", deepSpaceObjects))
.AddRow("[grey]Favorite sport[/]", sport)
.AddRow("[grey]Age[/]", age.ToString())
.AddRow("[grey]Password[/]", password)
Expand Down Expand Up @@ -119,6 +125,62 @@ public static string AskFruit()
return fruit;
}

public static IEnumerable<string> AskDeepSpaceObjects()
{
var tree =
new SelectableItems(
"All",
new SelectableItems(
"Galaxies",
new SelectableItems(
"Spiral",
new SelectableItem("Milky Way"),
new SelectableItem("M31 (Andromeda)")),
new SelectableItems(
"Elliptical",
new SelectableItems("M87 (Virgo A)"),
new SelectableItems("M49 (NGC 4472)"))),
new SelectableItems(
"Globular Clusters",
new SelectableItem("NGC 5139 (Omega Centauri)"),
new SelectableItem("M13 (Hercules)"),
new SelectableItem("M3"),
new SelectableItem("M5")),
new SelectableItems(
"Open Clusters",
new SelectableItem("M45 (The Pleiades)"),
new SelectableItem("M44 (The Beehive Cluster)"),
new SelectableItem("M6 (the Butterfly Cluster)"),
new SelectableItem("M7 (Ptolemy's Cluster)")),
new SelectableItems(
"Nebulas",
new SelectableItems(
"Emission",
new SelectableItem("M42 (Orion Nebula)"),
new SelectableItem("M16 (Eagle Nebula)")),
new SelectableItems(
"Planetary",
new SelectableItem("M57 (Ring Nebula)"),
new SelectableItem("M27 (Dumbbell Nebula)"))));

return AnsiConsole.Prompt(
new MultiSelectionPrompt<SelectableItem>()
.AddChoiceTree(
tree,
selectableItem =>
selectableItem is SelectableItems selectableItems
? selectableItems.Children
: Enumerable.Empty<SelectableItem>())
.PageSize(15)
.Title("What are your favorite DSOs?")
.HighlightStyle(
new Style(
Color.Black,
Color.SteelBlue1))
.UseConverter(selectable => selectable.Name))
.Select(selectableItem => selectableItem.Name);
}

public static string AskSport()
{
return AnsiConsole.Prompt(
Expand Down
25 changes: 25 additions & 0 deletions examples/Console/Prompt/SelectableItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System.Collections.Generic;

namespace Prompt
{
internal class SelectableItem
{
public string Name { get; set; }

public SelectableItem(string name)
{
Name = name;
}
}

internal class SelectableItems : SelectableItem
{
public IEnumerable<SelectableItem> Children { get; set; }

public SelectableItems(string name, params SelectableItem[] children) :
base(name)
{
Children = children;
}
}
}
40 changes: 40 additions & 0 deletions src/Spectre.Console/Prompts/MultiSelectionPromptExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,46 @@ public static MultiSelectionPrompt<T> AddChoiceGroup<T>(this MultiSelectionPromp
return obj;
}

/// <summary>
/// Adds choice tree.
/// </summary>
/// <typeparam name="T">The prompt result type.</typeparam>
/// <param name="obj">The prompt.</param>
/// <param name="rootChoice">The tree's root.</param>
/// <param name="getChildChoices">A function to get node child choices.</param>
/// <returns>The same instance so that multiple calls can be chained.</returns>
public static MultiSelectionPrompt<T> AddChoiceTree<T>(this MultiSelectionPrompt<T> obj, T rootChoice, Func<T, IEnumerable<T>> getChildChoices)
where T : notnull
{
var multiSelectionItem = obj.AddChoice(rootChoice);
AddChildChoice(
getChildChoices,
rootChoice,
multiSelectionItem,
rootChoice: true);
return obj;

static void AddChildChoice(
Func<T, IEnumerable<T>> getChildChoices,
T parentChoice,
ISelectionItem<T> selectionItem,
bool rootChoice = false)
{
if (!rootChoice)
{
selectionItem = selectionItem.AddChild(parentChoice);
}

foreach (var childChoice in getChildChoices(parentChoice))
{
AddChildChoice(
getChildChoices,
childChoice,
selectionItem);
}
}
}

/// <summary>
/// Marks an item as selected.
/// </summary>
Expand Down

0 comments on commit 8b0cd77

Please sign in to comment.