|
|
|
@ -9,18 +9,30 @@ using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace ConsoleApp.Menu
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Define a selection menu.
|
|
|
|
|
/// <br/>It allows you to navigate through the selections and search with a console input.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T">The type (or the implementation) of the selection.</typeparam>
|
|
|
|
|
internal abstract class Menu<T> : IMenu
|
|
|
|
|
where T : notnull
|
|
|
|
|
{
|
|
|
|
|
#region Attributes & Properties
|
|
|
|
|
protected StringBuilder _screenDisplay;
|
|
|
|
|
|
|
|
|
|
protected List<Selector<T>> _allSelectors = new List<Selector<T>>();
|
|
|
|
|
protected List<Selector<T>> _selectList = new List<Selector<T>>();
|
|
|
|
|
|
|
|
|
|
private int _currentLine;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Title of the menu.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public string Title { get; private set; }
|
|
|
|
|
public StringBuilder InputStr { get; set; }
|
|
|
|
|
|
|
|
|
|
private int _currentLine;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The current line of the selection list.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public int CurrentLine
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
@ -36,9 +48,19 @@ namespace ConsoleApp.Menu
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The currently selected object.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public T? CurrentSelected { get; protected set; }
|
|
|
|
|
public bool WriteMode { get; set; }
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Constructors
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Base constructor of the Menu class.
|
|
|
|
|
/// <br/>This one is incomplete and need to be completed in the inherited class constructors.
|
|
|
|
|
/// <br/>Basically, the '_allSelection' and '_selectList' attribute initialization are missing.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="title">The title of the Menu.</param>
|
|
|
|
|
protected Menu(string title)
|
|
|
|
|
{
|
|
|
|
|
Title = title;
|
|
|
|
@ -48,6 +70,11 @@ namespace ConsoleApp.Menu
|
|
|
|
|
InputStr = new StringBuilder();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Constructor of the Menu class. This constructor allows you to directly pass the selections.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="title">The title of the menu.</param>
|
|
|
|
|
/// <param name="selections">The selections of the menu.</param>
|
|
|
|
|
protected Menu(string title, params Selector<T>[] selections ) : this(title)
|
|
|
|
|
{
|
|
|
|
|
if (selections == null || selections.Length == 0)
|
|
|
|
@ -60,8 +87,19 @@ namespace ConsoleApp.Menu
|
|
|
|
|
_selectList = _allSelectors;
|
|
|
|
|
CurrentSelected = _allSelectors[0].Item;
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
public abstract IMenu? Return();
|
|
|
|
|
#region IMenu implementation
|
|
|
|
|
public StringBuilder InputStr { get; set; }
|
|
|
|
|
public bool WriteMode { get; set; }
|
|
|
|
|
|
|
|
|
|
public virtual IMenu? Return()
|
|
|
|
|
{
|
|
|
|
|
if (CurrentSelected is null)
|
|
|
|
|
throw new Exception("Error: CurrentSelected is null.");
|
|
|
|
|
|
|
|
|
|
return (IMenu)CurrentSelected;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected virtual List<Selector<T>> SearchInSelection()
|
|
|
|
|
{
|
|
|
|
@ -158,5 +196,6 @@ namespace ConsoleApp.Menu
|
|
|
|
|
else
|
|
|
|
|
EnableWriteMode();
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|