mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-06-19 21:38:16 +08:00
(#502) Added GetParent and GetParents to MultiSelectionPrompt
So it it possible to find the parent(s) of a given item.
This commit is contained in:

committed by
Patrik Svensson

parent
f5a2735501
commit
949f35defd
@ -112,6 +112,41 @@ namespace Spectre.Console
|
||||
.ToList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns all parent items of the given <paramref name="item"/>.
|
||||
/// </summary>
|
||||
/// <param name="item">The item for which to find the parents.</param>
|
||||
/// <returns>The parent items, or an empty list, if the given item has no parents.</returns>
|
||||
public IEnumerable<T> GetParents(T item)
|
||||
{
|
||||
var promptItem = Tree.Find(item);
|
||||
if (promptItem == null)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(item), "item not found in tree.");
|
||||
}
|
||||
|
||||
var parents = new List<ListPromptItem<T>>();
|
||||
while (promptItem.Parent != null)
|
||||
{
|
||||
promptItem = promptItem.Parent;
|
||||
parents.Add(promptItem);
|
||||
}
|
||||
|
||||
return parents
|
||||
.ReverseEnumerable()
|
||||
.Select(x => x.Data);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the parent item of the given <paramref name="item"/>.
|
||||
/// </summary>
|
||||
/// <param name="item">The item for which to find the parent.</param>
|
||||
/// <returns>The parent item, or <c>null</c> if the given item has no parent.</returns>
|
||||
public T? GetParent(T item)
|
||||
{
|
||||
return GetParents(item).LastOrDefault();
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
ListPromptInputResult IListPromptStrategy<T>.HandleInput(ConsoleKeyInfo key, ListPromptState<T> state)
|
||||
{
|
||||
|
Reference in New Issue
Block a user