using System;
using System.Collections.Generic;
namespace Spectre.Console
{
///
/// Contains extension methods for .
///
public static class SelectionPromptExtensions
{
///
/// Sets the selection mode.
///
/// The prompt result type.
/// The prompt.
/// The selection mode.
/// The same instance so that multiple calls can be chained.
public static SelectionPrompt Mode(this SelectionPrompt obj, SelectionMode mode)
where T : notnull
{
if (obj is null)
{
throw new ArgumentNullException(nameof(obj));
}
obj.Mode = mode;
return obj;
}
///
/// Adds multiple choices.
///
/// The prompt result type.
/// The prompt.
/// The choices to add.
/// The same instance so that multiple calls can be chained.
public static SelectionPrompt AddChoices(this SelectionPrompt obj, params T[] choices)
where T : notnull
{
if (obj is null)
{
throw new ArgumentNullException(nameof(obj));
}
foreach (var choice in choices)
{
obj.AddChoice(choice);
}
return obj;
}
///
/// Adds multiple choices.
///
/// The prompt result type.
/// The prompt.
/// The choices to add.
/// The same instance so that multiple calls can be chained.
public static SelectionPrompt AddChoices(this SelectionPrompt obj, IEnumerable choices)
where T : notnull
{
if (obj is null)
{
throw new ArgumentNullException(nameof(obj));
}
foreach (var choice in choices)
{
obj.AddChoice(choice);
}
return obj;
}
///
/// Adds multiple grouped choices.
///
/// The prompt result type.
/// The prompt.
/// The group.
/// The choices to add.
/// The same instance so that multiple calls can be chained.
public static SelectionPrompt AddChoiceGroup(this SelectionPrompt obj, T group, IEnumerable choices)
where T : notnull
{
if (obj is null)
{
throw new ArgumentNullException(nameof(obj));
}
var root = obj.AddChoice(group);
foreach (var choice in choices)
{
root.AddChild(choice);
}
return obj;
}
///
/// Sets the title.
///
/// The prompt result type.
/// The prompt.
/// The title markup text.
/// The same instance so that multiple calls can be chained.
public static SelectionPrompt Title(this SelectionPrompt obj, string? title)
where T : notnull
{
if (obj is null)
{
throw new ArgumentNullException(nameof(obj));
}
obj.Title = title;
return obj;
}
///
/// Sets how many choices that are displayed to the user.
///
/// The prompt result type.
/// The prompt.
/// The number of choices that are displayed to the user.
/// The same instance so that multiple calls can be chained.
public static SelectionPrompt PageSize(this SelectionPrompt obj, int pageSize)
where T : notnull
{
if (obj is null)
{
throw new ArgumentNullException(nameof(obj));
}
if (pageSize <= 2)
{
throw new ArgumentException("Page size must be greater or equal to 3.", nameof(pageSize));
}
obj.PageSize = pageSize;
return obj;
}
///
/// Sets the highlight style of the selected choice.
///
/// The prompt result type.
/// The prompt.
/// The highlight style of the selected choice.
/// The same instance so that multiple calls can be chained.
public static SelectionPrompt HighlightStyle(this SelectionPrompt obj, Style highlightStyle)
where T : notnull
{
if (obj is null)
{
throw new ArgumentNullException(nameof(obj));
}
obj.HighlightStyle = highlightStyle;
return obj;
}
///
/// Sets the text that will be displayed if there are more choices to show.
///
/// The prompt result type.
/// The prompt.
/// The text to display.
/// The same instance so that multiple calls can be chained.
public static SelectionPrompt MoreChoicesText(this SelectionPrompt obj, string? text)
where T : notnull
{
if (obj is null)
{
throw new ArgumentNullException(nameof(obj));
}
obj.MoreChoicesText = text;
return obj;
}
///
/// Sets the function to create a display string for a given choice.
///
/// The prompt type.
/// The prompt.
/// The function to get a display string for a given choice.
/// The same instance so that multiple calls can be chained.
public static SelectionPrompt UseConverter(this SelectionPrompt obj, Func? displaySelector)
where T : notnull
{
if (obj is null)
{
throw new ArgumentNullException(nameof(obj));
}
obj.Converter = displaySelector;
return obj;
}
}
}