|
|
|
@ -9,30 +9,53 @@ using System.Threading.Tasks;
|
|
|
|
|
namespace ConsoleApp.Menu
|
|
|
|
|
{
|
|
|
|
|
internal abstract class Menu<T> : IMenu
|
|
|
|
|
where T : notnull
|
|
|
|
|
{
|
|
|
|
|
protected StringBuilder _screenDisplay;
|
|
|
|
|
protected readonly List<Selector<T>> _selectList;
|
|
|
|
|
protected readonly List<Selector<T>>? _selectList;
|
|
|
|
|
|
|
|
|
|
public string Title { get; private set; }
|
|
|
|
|
public StringBuilder InputStr { get; set; }
|
|
|
|
|
public int CurrentLine { get; private set; }
|
|
|
|
|
public T CurrentSelected { get; private set; }
|
|
|
|
|
public T? CurrentSelected { get; private set; }
|
|
|
|
|
public bool WriteMode { get; set; }
|
|
|
|
|
|
|
|
|
|
public Menu(string title, params Selector<T>[] selections )
|
|
|
|
|
private Menu(string title)
|
|
|
|
|
{
|
|
|
|
|
if (selections == null || selections.Length == 0)
|
|
|
|
|
throw new ArgumentException("Error: a menu must contain at least 1 selection");
|
|
|
|
|
|
|
|
|
|
Title = title;
|
|
|
|
|
CurrentLine = 0;
|
|
|
|
|
CurrentSelected = selections[0].Item;
|
|
|
|
|
WriteMode = false;
|
|
|
|
|
_selectList = new List<Selector<T>>(selections);
|
|
|
|
|
_screenDisplay = new StringBuilder();
|
|
|
|
|
InputStr = new StringBuilder();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Menu(string title, params Selector<T>[] selections ) : this(title)
|
|
|
|
|
{
|
|
|
|
|
if (selections == null || selections.Length == 0)
|
|
|
|
|
throw new ArgumentException("Error: a menu must contain at least 1 selection");
|
|
|
|
|
|
|
|
|
|
_selectList = selections.ToList();
|
|
|
|
|
CurrentSelected = _selectList[0].Item;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Menu(string title, Dictionary<T, string> lines) : this(title, CreateSelection(lines))
|
|
|
|
|
{ }
|
|
|
|
|
|
|
|
|
|
public static Selector<T>[] CreateSelection(Dictionary<T, string> lines)
|
|
|
|
|
{
|
|
|
|
|
if (lines is null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(lines));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Selector<T>[] selections = new Selector<T>[lines.Count];
|
|
|
|
|
foreach (KeyValuePair<T, string> line in lines)
|
|
|
|
|
{
|
|
|
|
|
selections.Append(new Selector<T>(line.Key, line.Value));
|
|
|
|
|
}
|
|
|
|
|
return selections;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual void Display()
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(_screenDisplay);
|
|
|
|
@ -61,7 +84,7 @@ namespace ConsoleApp.Menu
|
|
|
|
|
|
|
|
|
|
public void SelectPrevious()
|
|
|
|
|
{
|
|
|
|
|
CurrentSelected = _selectList[--CurrentLine].Item
|
|
|
|
|
CurrentSelected = _selectList[--CurrentLine].Item;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void EnableWriteMode()
|
|
|
|
|