📝 update documentation
continuous-integration/drone/push Build is passing Details

pull/38/head
Alexandre AGOSTINHO 2 years ago
parent ff996aa421
commit 628fa7045f

@ -8,20 +8,45 @@ namespace ConsoleApp.Menu
{ {
internal abstract partial class Entry internal abstract partial class Entry
{ {
/// <summary>
/// Define a step of the Entry menu, or in other word, an entry itself.
/// </summary>
public class EntryStep public class EntryStep
{ {
#region Attributes & Properties
private Type _entryType; private Type _entryType;
/// <summary>
/// The entry description. This text is generally placed before the input field.
/// </summary>
public string Description { get; private set; } public string Description { get; private set; }
/// <summary>
/// Contain the input gave by the menu.
/// </summary>
public string Input { get; internal set; } public string Input { get; internal set; }
#endregion
internal EntryStep(string description, Type type) #region Constructors
/// <summary>
/// Constructor of the entry step.
/// </summary>
/// <param name="description">The text generally placed before the input in the menu.</param>
/// <param name="type">The type of the returned input.</param>
public EntryStep(string description, Type type)
{ {
Description = description; Description = description;
Input = ""; Input = "";
_entryType = type; _entryType = type;
} }
#endregion
#region Methods
/// <summary>
/// Get the inputed string converted on this entry type.
/// </summary>
/// <returns>The converted string on the entry type.</returns>
/// <exception cref="NotImplementedException">Throw when the entry type converter does not exist here.</exception>
public object GetEntry() public object GetEntry()
{ {
try try
@ -40,6 +65,7 @@ namespace ConsoleApp.Menu
throw new NotImplementedException("Error: parse of this type is not implemented."); throw new NotImplementedException("Error: parse of this type is not implemented.");
} }
#endregion
} }
} }
} }

@ -6,17 +6,31 @@ using System.Threading.Tasks;
namespace ConsoleApp.Menu namespace ConsoleApp.Menu
{ {
/// <summary>
/// Define an Entry menu.
/// <br/>It allows you to navigate through the entries and completes them with a console input.
/// </summary>
internal abstract partial class Entry : Menu<Entry.EntryStep> internal abstract partial class Entry : Menu<Entry.EntryStep>
{ {
#region Attributes & Properties
private List<EntryStep> _steps = new List<EntryStep>(); private List<EntryStep> _steps = new List<EntryStep>();
#endregion
#region Constructors
/// <summary>
/// Constructor of the entry menu, based on the Menu constructor.
/// </summary>
/// <param name="title">The title of this menu.</param>
/// <param name="entrySteps">All the entries of this menu.</param>
protected Entry(string title, params EntryStep[] entrySteps) : base(title) protected Entry(string title, params EntryStep[] entrySteps) : base(title)
{ {
_steps = entrySteps.ToList(); _steps = entrySteps.ToList();
_allSelectors = ConvertEntryStepsInSelector(); _allSelectors = ConvertEntryStepsInSelector();
_selectList = _allSelectors; _selectList = _allSelectors;
} }
#endregion
#region Methods
private List<Selector<EntryStep>> ConvertEntryStepsInSelector() private List<Selector<EntryStep>> ConvertEntryStepsInSelector()
{ {
List<Selector<EntryStep>> newSelectors = new List<Selector<EntryStep>>(); List<Selector<EntryStep>> newSelectors = new List<Selector<EntryStep>>();
@ -26,7 +40,9 @@ namespace ConsoleApp.Menu
} }
return newSelectors; return newSelectors;
} }
#endregion
#region IMenu implementation
public override void WriteMenuMode(ConsoleKeyInfo cki) public override void WriteMenuMode(ConsoleKeyInfo cki)
{ {
if (!WriteMode && cki.Key == ConsoleKey.R) if (!WriteMode && cki.Key == ConsoleKey.R)
@ -98,5 +114,6 @@ namespace ConsoleApp.Menu
"\n\nHint:\n^:previous, v:next, <:ret, -enter-:return, r:write, -escape-:exit search mode"); "\n\nHint:\n^:previous, v:next, <:ret, -enter-:return, r:write, -escape-:exit search mode");
Console.WriteLine(_screenDisplay); Console.WriteLine(_screenDisplay);
} }
#endregion
} }
} }

@ -7,19 +7,66 @@ using System.Threading.Tasks;
namespace ConsoleApp.Menu namespace ConsoleApp.Menu
{ {
/// <summary>
/// Define a console menu with element selection.
/// </summary>
internal interface IMenu internal interface IMenu
{ {
/// <summary>
/// True when enable, False otherwise.
/// </summary>
bool WriteMode { get; set; } bool WriteMode { get; set; }
/// <summary>
/// A string input. Used for search string or entry input.
/// </summary>
StringBuilder InputStr { get; set; } StringBuilder InputStr { get; set; }
/// <summary>
/// Refresh the console with the updated display. This function dose not call Update().
/// </summary>
void Display(); void Display();
/// <summary>
/// Update the parameters of the menu. Generally called before Display() to refresh the informations.
/// </summary>
void Update(); void Update();
/// <summary>
/// Select the next element in the selection list.
/// </summary>
void SelectNext(); void SelectNext();
/// <summary>
/// Select the previous element in the selection list.
/// </summary>
void SelectPrevious(); void SelectPrevious();
/// <summary>
/// Enable the write mode.
/// </summary>
void EnableWriteMode(); void EnableWriteMode();
/// <summary>
/// Disable the write mode.
/// </summary>
void DisableWriteMode(); void DisableWriteMode();
/// <summary>
/// Toogle the write mode.
/// </summary>
void ToggleWriteMode(); void ToggleWriteMode();
/// <summary>
/// Define the comportement of the write mode. For instence: in the standard menu, it is used for the research of a line.
/// </summary>
/// <param name="cki">The key to deal with.</param>
void WriteMenuMode(ConsoleKeyInfo cki); void WriteMenuMode(ConsoleKeyInfo cki);
/// <summary>
/// Execute some actions and then return.
/// </summary>
/// <returns>'null' when there is no menu after this selection. Otherwise the next menu.</returns>
IMenu? Return(); IMenu? Return();
} }
} }

@ -7,6 +7,9 @@ using System.Threading.Tasks;
namespace ConsoleApp.Menu namespace ConsoleApp.Menu
{ {
/// <summary>
/// Main menu of the console. Contain the first interaction menus.
/// </summary>
internal class MainMenu : Menu<IMenu> internal class MainMenu : Menu<IMenu>
{ {
public MainMenu(DataManager dataMgr) public MainMenu(DataManager dataMgr)
@ -16,10 +19,5 @@ namespace ConsoleApp.Menu
new Selector<IMenu>( new Selector<IMenu>(
new ConnectionMenu(), "Connection")) new ConnectionMenu(), "Connection"))
{ } { }
public override IMenu? Return()
{
return CurrentSelected;
}
} }
} }

@ -9,18 +9,30 @@ using System.Threading.Tasks;
namespace ConsoleApp.Menu 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 internal abstract class Menu<T> : IMenu
where T : notnull where T : notnull
{ {
#region Attributes & Properties
protected StringBuilder _screenDisplay; protected StringBuilder _screenDisplay;
protected List<Selector<T>> _allSelectors = new List<Selector<T>>(); protected List<Selector<T>> _allSelectors = new List<Selector<T>>();
protected List<Selector<T>> _selectList = 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 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 public int CurrentLine
{ {
get get
@ -36,9 +48,19 @@ namespace ConsoleApp.Menu
} }
} }
/// <summary>
/// The currently selected object.
/// </summary>
public T? CurrentSelected { get; protected set; } 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) protected Menu(string title)
{ {
Title = title; Title = title;
@ -48,6 +70,11 @@ namespace ConsoleApp.Menu
InputStr = new StringBuilder(); 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) protected Menu(string title, params Selector<T>[] selections ) : this(title)
{ {
if (selections == null || selections.Length == 0) if (selections == null || selections.Length == 0)
@ -60,8 +87,19 @@ namespace ConsoleApp.Menu
_selectList = _allSelectors; _selectList = _allSelectors;
CurrentSelected = _allSelectors[0].Item; 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() protected virtual List<Selector<T>> SearchInSelection()
{ {
@ -158,5 +196,6 @@ namespace ConsoleApp.Menu
else else
EnableWriteMode(); EnableWriteMode();
} }
#endregion
} }
} }

@ -6,14 +6,25 @@ using System.Threading.Tasks;
namespace ConsoleApp.Menu namespace ConsoleApp.Menu
{ {
/// <summary>
/// Define a Plain text menu.
/// <br/>This menu is a bit special. It display some text, and then, the only action that can be performed is the back return. Usefull for testing.
/// </summary>
internal class PlainText : IMenu internal class PlainText : IMenu
{ {
#region Constructors
/// <summary>
/// Constructor of the Plain text menu.
/// </summary>
/// <param name="text">The text buffer to display.</param>
public PlainText(string text) public PlainText(string text)
{ {
InputStr = new StringBuilder(text); InputStr = new StringBuilder(text);
WriteMode = false; WriteMode = false;
} }
#endregion
#region IMenu implementation
public IMenu? Return() { return null; } public IMenu? Return() { return null; }
public void Display() public void Display()
{ {
@ -30,5 +41,6 @@ namespace ConsoleApp.Menu
public void ToggleWriteMode() { return; } public void ToggleWriteMode() { return; }
public void Update() { return; } public void Update() { return; }
public void WriteMenuMode(ConsoleKeyInfo cki) { return; } public void WriteMenuMode(ConsoleKeyInfo cki) { return; }
#endregion
} }
} }

@ -23,7 +23,7 @@ namespace ConsoleApp.Menu
_selectList = _allSelectors; _selectList = _allSelectors;
} }
#region Methodes #region Methods
private List<Selector<Recipe>> ConvertRecipeCollectionInSelectors() private List<Selector<Recipe>> ConvertRecipeCollectionInSelectors()
{ {
List<Selector<Recipe>> newSelectors = new List<Selector<Recipe>>(); List<Selector<Recipe>> newSelectors = new List<Selector<Recipe>>();

@ -9,11 +9,19 @@ using System.Threading.Tasks;
namespace ConsoleApp.Menu namespace ConsoleApp.Menu
{ {
/// <summary>
/// The selector of a menu.
/// </summary>
/// <typeparam name="T">The type of the selector.</typeparam>
internal class Selector<T> : IEquatable<Selector<T>> internal class Selector<T> : IEquatable<Selector<T>>
where T : notnull where T : notnull
{ {
#region Attributes & Properties
private string _line = ""; private string _line = "";
/// <summary>
/// The string that are displayed on the menu.
/// </summary>
public string Line { public string Line {
get => _line; get => _line;
private set private set
@ -24,14 +32,27 @@ namespace ConsoleApp.Menu
_line = value; _line = value;
} }
} }
/// <summary>
/// The item contained in the selector.
/// </summary>
public T Item { get; private set; } public T Item { get; private set; }
#endregion
#region Constructors
/// <summary>
/// The constructor of the selector.
/// </summary>
/// <param name="item">The item to place inside.</param>
/// <param name="line">The string to display in the menu.</param>
public Selector(T item, string line = "") public Selector(T item, string line = "")
{ {
Line = line; Line = line;
Item = item; Item = item;
} }
#endregion
#region IEquatable implementation
public virtual bool Equals(Selector<T>? other) public virtual bool Equals(Selector<T>? other)
{ {
if (other == null) return false; if (other == null) return false;
@ -50,5 +71,6 @@ namespace ConsoleApp.Menu
{ {
return Line.GetHashCode(); return Line.GetHashCode();
} }
#endregion
} }
} }

@ -8,11 +8,29 @@ using System.Threading.Tasks;
namespace ConsoleApp namespace ConsoleApp
{ {
/// <summary>
/// Manage the menus of the console application.
/// </summary>
internal class MenuManager internal class MenuManager
{ {
#region Attributes & Properties
/// <summary>
/// The manager that contains usefull data taken from the model.
/// </summary>
public DataManager DataManager { get; private set; } public DataManager DataManager { get; private set; }
/// <summary>
/// Each menu called are push in this stack. Then, to return back, we pop this stack to retrive the previous menu.
/// </summary>
public Stack<Menu.IMenu> MenuCallStack { get; set; } public Stack<Menu.IMenu> MenuCallStack { get; set; }
#endregion
#region Constructors
/// <summary>
/// Constructor of the MenuManager class. This constructor allows you to give the first menu of the call stack, wich is usefull for testing.
/// </summary>
/// <param name="dataManager">The data manager needed by the menus inside.</param>
/// <param name="firstMenu">The starting menu, the first that will be push on the call stack.</param>
public MenuManager(DataManager dataManager, Menu.IMenu firstMenu) public MenuManager(DataManager dataManager, Menu.IMenu firstMenu)
{ {
DataManager = dataManager; DataManager = dataManager;
@ -21,9 +39,18 @@ namespace ConsoleApp
MenuCallStack.Push(firstMenu); MenuCallStack.Push(firstMenu);
} }
/// <summary>
/// Constructor of the MenuManager class.
/// </summary>
/// <param name="dataManager">The data manager needed by the menus inside.</param>
public MenuManager(DataManager dataManager) : this(dataManager, new MainMenu(dataManager)) public MenuManager(DataManager dataManager) : this(dataManager, new MainMenu(dataManager))
{ } { }
#endregion
#region Methods
/// <summary>
/// Main loop. Loop while the menu call stack is not empty.
/// </summary>
public void Loop() public void Loop()
{ {
ConsoleKeyInfo cki; ConsoleKeyInfo cki;
@ -57,5 +84,6 @@ namespace ConsoleApp
} }
} while (MenuCallStack.Count > 0); } while (MenuCallStack.Count > 0);
} }
#endregion
} }
} }

@ -25,6 +25,3 @@ DataManager dataMgr = new DataManager(allRecipe);
MenuManager menuMgr = new MenuManager(dataMgr); MenuManager menuMgr = new MenuManager(dataMgr);
menuMgr.Loop(); menuMgr.Loop();
// press any key to quit
Console.ReadKey();

Loading…
Cancel
Save