You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
162 lines
4.8 KiB
162 lines
4.8 KiB
using System.Data;
|
|
using Model;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel.Design;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace ConsoleApp.Menu
|
|
{
|
|
internal abstract class Menu<T> : IMenu
|
|
where T : notnull
|
|
{
|
|
protected StringBuilder _screenDisplay;
|
|
|
|
protected List<Selector<T>> _allSelectors = new List<Selector<T>>();
|
|
protected List<Selector<T>> _selectList = new List<Selector<T>>();
|
|
|
|
public string Title { get; private set; }
|
|
public StringBuilder InputStr { get; set; }
|
|
|
|
private int _currentLine;
|
|
public int CurrentLine {
|
|
get
|
|
{
|
|
if (_currentLine >= _selectList.Count) _currentLine = _selectList.Count - 1;
|
|
return _currentLine;
|
|
}
|
|
protected set
|
|
{
|
|
_currentLine = value;
|
|
if (_currentLine <= 0) _currentLine = 0;
|
|
else if (_currentLine >= _selectList.Count) _currentLine = _selectList.Count-1;
|
|
}
|
|
}
|
|
|
|
public T? CurrentSelected { get; protected set; }
|
|
public bool WriteMode { get; set; }
|
|
|
|
protected Menu(string title)
|
|
{
|
|
Title = title;
|
|
CurrentLine = 0;
|
|
WriteMode = false;
|
|
_screenDisplay = new StringBuilder();
|
|
InputStr = new StringBuilder();
|
|
}
|
|
|
|
protected Menu(string title, params Selector<T>[] selections ) : this(title)
|
|
{
|
|
if (selections == null || selections.Length == 0)
|
|
{
|
|
Console.WriteLine("Empty menu...");
|
|
return;
|
|
}
|
|
|
|
_allSelectors = selections.ToList();
|
|
_selectList = _allSelectors;
|
|
CurrentSelected = _allSelectors[0].Item;
|
|
}
|
|
|
|
public abstract IMenu? Return();
|
|
|
|
protected virtual List<Selector<T>> SearchInSelection()
|
|
{
|
|
if (_allSelectors is null)
|
|
throw new Exception("Error: _allSelector is null.");
|
|
|
|
return _allSelectors.FindAll(x =>
|
|
x.Line.ToLower().Contains(InputStr.ToString().ToLower()));
|
|
}
|
|
|
|
public void WriteMenuMode(ConsoleKeyInfo cki)
|
|
{
|
|
if (!WriteMode && cki.Key == ConsoleKey.R)
|
|
{
|
|
EnableWriteMode();
|
|
return;
|
|
}
|
|
|
|
if (WriteMode)
|
|
{
|
|
if (cki.Key == ConsoleKey.Escape)
|
|
{
|
|
DisableWriteMode();
|
|
InputStr.Clear();
|
|
return;
|
|
}
|
|
|
|
if (cki.Key == ConsoleKey.Backspace && InputStr.Length > 0)
|
|
{
|
|
InputStr.Remove(InputStr.Length - 1, 1);
|
|
return;
|
|
}
|
|
|
|
InputStr.Append(cki.KeyChar);
|
|
}
|
|
}
|
|
|
|
public void Update()
|
|
{
|
|
_selectList = SearchInSelection();
|
|
|
|
if (_selectList == null || _selectList.Count == 0)
|
|
{
|
|
CurrentSelected = default;
|
|
return;
|
|
}
|
|
CurrentSelected = _selectList[CurrentLine].Item;
|
|
}
|
|
|
|
public virtual void Display()
|
|
{
|
|
_screenDisplay.Clear();
|
|
Console.Clear();
|
|
|
|
_screenDisplay.AppendLine($"[ {Title} ]");
|
|
_screenDisplay.AppendLine("-------------------------------------------");
|
|
|
|
if (WriteMode)
|
|
{
|
|
_screenDisplay.Append("Search: ");
|
|
_screenDisplay.AppendLine(InputStr.ToString());
|
|
}
|
|
|
|
for (int i = 0; i < _selectList.Count; i++)
|
|
{
|
|
if (CurrentLine == i)
|
|
_screenDisplay.Append($"> ");
|
|
else
|
|
_screenDisplay.Append($" ");
|
|
|
|
_screenDisplay.AppendLine($"{_selectList[i].Line}");
|
|
}
|
|
|
|
if (_selectList == null || _selectList.Count == 0)
|
|
_screenDisplay.AppendLine("Empty...");
|
|
|
|
_screenDisplay.AppendLine(
|
|
"\n\nHint:\n^:previous, v:next, <:ret, -enter-:select, r:search mode, -escape-:exit search mode");
|
|
Console.WriteLine(_screenDisplay);
|
|
}
|
|
|
|
public void SelectNext() => ++CurrentLine;
|
|
|
|
public void SelectPrevious() => --CurrentLine;
|
|
|
|
public void EnableWriteMode() => WriteMode = true;
|
|
|
|
public void DisableWriteMode() => WriteMode = false;
|
|
|
|
public void ToggleWriteMode()
|
|
{
|
|
if (WriteMode)
|
|
DisableWriteMode();
|
|
else
|
|
EnableWriteMode();
|
|
}
|
|
}
|
|
}
|