From d0146a0e14c0b1bfa35c925abc82ceb6af24447f Mon Sep 17 00:00:00 2001 From: Alexandre Agostinho Date: Sat, 13 May 2023 19:20:47 +0200 Subject: [PATCH] first version of entry menu (working) --- MCTG/ConsoleApp/Menu/ConnectionMenu.cs | 22 +++++++ MCTG/ConsoleApp/Menu/Entry.EntryStep.cs | 9 +-- MCTG/ConsoleApp/Menu/Entry.cs | 86 ++++++++++++++++++++++--- MCTG/ConsoleApp/Menu/Menu.cs | 9 +-- MCTG/ConsoleApp/Program.cs | 5 +- 5 files changed, 110 insertions(+), 21 deletions(-) create mode 100644 MCTG/ConsoleApp/Menu/ConnectionMenu.cs diff --git a/MCTG/ConsoleApp/Menu/ConnectionMenu.cs b/MCTG/ConsoleApp/Menu/ConnectionMenu.cs new file mode 100644 index 0000000..7dc7b09 --- /dev/null +++ b/MCTG/ConsoleApp/Menu/ConnectionMenu.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConsoleApp.Menu +{ + internal class ConnectionMenu : Entry + { + public ConnectionMenu(params EntryStep[] entrySteps) + : base("Connection", entrySteps) + { } + + public override IMenu? Return() + { + string username = _selectList[0].Item.Input; + string password = _selectList[1].Item.Input; + return new PlainText($"User: {username} connected with password: {password}"); + } + } +} diff --git a/MCTG/ConsoleApp/Menu/Entry.EntryStep.cs b/MCTG/ConsoleApp/Menu/Entry.EntryStep.cs index 2e8e471..06dfc88 100644 --- a/MCTG/ConsoleApp/Menu/Entry.EntryStep.cs +++ b/MCTG/ConsoleApp/Menu/Entry.EntryStep.cs @@ -13,22 +13,17 @@ namespace ConsoleApp.Menu private Type _entryType; public string Description { get; private set; } - public string? Input { get; private set; } + public string Input { get; internal set; } internal EntryStep(string description, Type type) { Description = description; - Input = null; + Input = ""; _entryType = type; } public object GetEntry() { - Input = Console.ReadLine(); - - if (Input is null) - throw new ArgumentException("Error: input is null."); - try { if (_entryType == typeof(string)) diff --git a/MCTG/ConsoleApp/Menu/Entry.cs b/MCTG/ConsoleApp/Menu/Entry.cs index 21f323e..de4cd0d 100644 --- a/MCTG/ConsoleApp/Menu/Entry.cs +++ b/MCTG/ConsoleApp/Menu/Entry.cs @@ -8,27 +8,95 @@ namespace ConsoleApp.Menu { internal abstract partial class Entry : Menu { - private List> _allSelectors; + private List _steps = new List(); - protected Entry(string title) : base(title) + protected Entry(string title, params EntryStep[] entrySteps) : base(title) { - _allSelectors = _selectList; + _steps = entrySteps.ToList(); + _allSelectors = ConvertEntryStepsInSelector(); + _selectList = _allSelectors; } - protected override List> SearchInSelection() + private List> ConvertEntryStepsInSelector() { - return _allSelectors; + List> newSelectors = new List>(); + foreach (EntryStep step in _steps) + { + newSelectors.Add(new Selector(step, step.Description)); + } + return newSelectors; } - public override IMenu? Return() + public override void WriteMenuMode(ConsoleKeyInfo cki) { - if (CurrentSelected == null) - throw new Exception("Error: CurrentSelected is null."); + if (!WriteMode && cki.Key == ConsoleKey.R) + { + EnableWriteMode(); + return; + } + if (WriteMode) + { + if (cki.Key == ConsoleKey.Escape) + { + if (CurrentSelected is null) + throw new Exception("Error: CurrentSelected is null."); - return null; + CurrentSelected.Input = InputStr.ToString(); + DisableWriteMode(); + InputStr.Clear(); + return; + } + + if (cki.Key == ConsoleKey.Backspace && InputStr.Length > 0) + { + InputStr.Remove(InputStr.Length - 1, 1); + return; + } + + InputStr.Append(cki.KeyChar); + } } + public override void Update() + { + if (_selectList == null || _selectList.Count == 0) + { + CurrentSelected = default; + return; + } + CurrentSelected = _selectList[CurrentLine].Item; + } + + public override void Display() + { + _screenDisplay.Clear(); + Console.Clear(); + + _screenDisplay.AppendLine($"[ {Title} ]"); + _screenDisplay.AppendLine("-------------------------------------------"); + for (int i = 0; i < _selectList.Count; i++) + { + if (CurrentLine == i) + _screenDisplay.Append($"> "); + else + _screenDisplay.Append($" "); + + _screenDisplay.Append($"{_selectList[i].Line} {_selectList[i].Item.Input}"); + + if (CurrentLine == i && WriteMode) + _screenDisplay.Append(InputStr.ToString()); + + _screenDisplay.AppendLine(); + } + + if (_selectList == null || _selectList.Count == 0) + _screenDisplay.AppendLine("Empty..."); + + _screenDisplay.AppendLine( + "\n\nHint:\n^:previous, v:next, <:ret, -enter-:return, r:write, -escape-:exit search mode"); + Console.WriteLine(_screenDisplay); + } } } diff --git a/MCTG/ConsoleApp/Menu/Menu.cs b/MCTG/ConsoleApp/Menu/Menu.cs index 1f38516..bc8e7b1 100644 --- a/MCTG/ConsoleApp/Menu/Menu.cs +++ b/MCTG/ConsoleApp/Menu/Menu.cs @@ -21,7 +21,8 @@ namespace ConsoleApp.Menu public StringBuilder InputStr { get; set; } private int _currentLine; - public int CurrentLine { + public int CurrentLine + { get { if (_currentLine >= _selectList.Count) _currentLine = _selectList.Count - 1; @@ -71,7 +72,7 @@ namespace ConsoleApp.Menu x.Line.ToLower().Contains(InputStr.ToString().ToLower())); } - public void WriteMenuMode(ConsoleKeyInfo cki) + public virtual void WriteMenuMode(ConsoleKeyInfo cki) { if (!WriteMode && cki.Key == ConsoleKey.R) { @@ -98,7 +99,7 @@ namespace ConsoleApp.Menu } } - public void Update() + public virtual void Update() { _selectList = SearchInSelection(); @@ -138,7 +139,7 @@ namespace ConsoleApp.Menu _screenDisplay.AppendLine("Empty..."); _screenDisplay.AppendLine( - "\n\nHint:\n^:previous, v:next, <:ret, -enter-:select, r:search mode, -escape-:exit search mode"); + "\n\nHint:\n^:previous, v:next, <:ret, -enter-:select, r:search, -escape-:exit search mode"); Console.WriteLine(_screenDisplay); } diff --git a/MCTG/ConsoleApp/Program.cs b/MCTG/ConsoleApp/Program.cs index 1863be2..a4cef53 100644 --- a/MCTG/ConsoleApp/Program.cs +++ b/MCTG/ConsoleApp/Program.cs @@ -22,7 +22,10 @@ if (allRecipe == null) DataManager dataMgr = new DataManager(allRecipe); -MenuManager menuMgr = new MenuManager(dataMgr, new SearcherRecipe(dataMgr.AllRecipes)); +MenuManager menuMgr = new MenuManager(dataMgr, new ConnectionMenu( + new Entry.EntryStep("Username: ", typeof(string)), + new Entry.EntryStep("Password: ", typeof(string)))); + menuMgr.Loop(); // press any key to quit