Add support for default choice in selection prompt

Closes #234
This commit is contained in:
Patrik Svensson
2021-01-15 16:32:32 +01:00
committed by Patrik Svensson
parent 63bae278a9
commit 913a7b1e37
3 changed files with 82 additions and 7 deletions

View File

@ -44,6 +44,73 @@ namespace Spectre.Console
return obj;
}
/// <summary>
/// Marks an item as selected.
/// </summary>
/// <typeparam name="T">The prompt result type.</typeparam>
/// <param name="obj">The prompt.</param>
/// <param name="index">The index of the item to select.</param>
/// <returns>The same instance so that multiple calls can be chained.</returns>
public static MultiSelectionPrompt<T> Select<T>(this MultiSelectionPrompt<T> obj, int index)
{
if (obj is null)
{
throw new ArgumentNullException(nameof(obj));
}
if (index < 0)
{
throw new ArgumentException("Index must be greater than zero", nameof(index));
}
obj.Selected.Add(index);
return obj;
}
/// <summary>
/// Marks multiple items as selected.
/// </summary>
/// <typeparam name="T">The prompt result type.</typeparam>
/// <param name="obj">The prompt.</param>
/// <param name="indices">The indices of the items to select.</param>
/// <returns>The same instance so that multiple calls can be chained.</returns>
public static MultiSelectionPrompt<T> Select<T>(this MultiSelectionPrompt<T> obj, params int[] indices)
{
if (obj is null)
{
throw new ArgumentNullException(nameof(obj));
}
foreach (var index in indices)
{
Select(obj, index);
}
return obj;
}
/// <summary>
/// Marks multiple items as selected.
/// </summary>
/// <typeparam name="T">The prompt result type.</typeparam>
/// <param name="obj">The prompt.</param>
/// <param name="indices">The indices of the items to select.</param>
/// <returns>The same instance so that multiple calls can be chained.</returns>
public static MultiSelectionPrompt<T> Select<T>(this MultiSelectionPrompt<T> obj, IEnumerable<int> indices)
{
if (obj is null)
{
throw new ArgumentNullException(nameof(obj));
}
foreach (var index in indices)
{
Select(obj, index);
}
return obj;
}
/// <summary>
/// Adds multiple choices.
/// </summary>
@ -179,4 +246,4 @@ namespace Spectre.Console
return obj;
}
}
}
}