From 54b99ca2d56d553535956c4bf18e2e9464c2b319 Mon Sep 17 00:00:00 2001 From: S-Dhir <73019620+S-Dhir@users.noreply.github.com> Date: Wed, 28 Sep 2022 11:22:28 +0530 Subject: [PATCH] Improved renderer It doesn't clear the entire thing now. Also added an overload to select which allows developers to set default options. Also made show() private because it is already called in select --- Checkbox/Checkbox.cs | 86 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 82 insertions(+), 4 deletions(-) diff --git a/Checkbox/Checkbox.cs b/Checkbox/Checkbox.cs index 64b3e85..0d59537 100644 --- a/Checkbox/Checkbox.cs +++ b/Checkbox/Checkbox.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; +using System.Text; namespace Checkbox { @@ -15,6 +16,7 @@ public class Checkbox private bool _multiSelect; private bool _required; private bool _error; + private (int Left, int Top) Pos = Console.GetCursorPosition(); private ConsoleKey _key; private ConsoleKey _prevKey; @@ -22,6 +24,7 @@ public Checkbox(string displayText, params string[] options) { _multiSelect = false; _required = true; + Init(displayText, options); } @@ -56,9 +59,12 @@ private CheckboxReturn[] ReturnData() return l.ToArray(); } - public void Show() + private void Show() { - Console.Clear(); + + Console.SetCursorPosition(Pos.Left,Pos.Top); + Console.Write(new string(' ', Console.BufferWidth)); + Console.SetCursorPosition(Pos.Left, Pos.Top); Console.WriteLine(_displayText); Console.WriteLine("(Use Arrow keys to navigate up and down, Space bar to select and Enter to submit)"); @@ -67,7 +73,7 @@ public void Show() Console.ForegroundColor = option.Selected ? (option.Hovered ? ConsoleColor.Blue : ConsoleColor.DarkBlue) : (option.Hovered ? ConsoleColor.White : ConsoleColor.DarkGray); - + Console.WriteLine((option.Selected ? "[*]~ " : "[ ]~ ") + $"{option.Option}"); } Console.ResetColor(); @@ -130,6 +136,78 @@ public CheckboxReturn[] Select() _prevKey = _key; } + return ReturnData(); + } + public CheckboxReturn[] Select(int defaultOption) + { + Show(); + bool end = false; + while (!end) + { + _key = Console.KeyAvailable ? Console.ReadKey(true).Key : ConsoleKey.D9; + if (_key == _prevKey) continue; + _options[_hoveredIndex].Hovered = false; + + for(int io = 0; io<=defaultOption;) { + io++; + _hoveredIndex = _hoveredIndex + 1 < _options.Count ? _hoveredIndex + 1 : 0; + } + _options[_hoveredIndex].Selected = !_options[_hoveredIndex].Selected; + if (!_multiSelect) + { + if (_selectedIndex > -1 && _hoveredIndex != _selectedIndex) + _options[_selectedIndex].Selected = false; + _selectedIndex = _hoveredIndex; + } + + _error = false; + + switch (_key) + { + case ConsoleKey.UpArrow: + case ConsoleKey.W: + _hoveredIndex = _hoveredIndex - 1 >= 0 ? _hoveredIndex - 1 : _options.Count - 1; + break; + + case ConsoleKey.DownArrow: + case ConsoleKey.S: + _hoveredIndex = _hoveredIndex + 1 < _options.Count ? _hoveredIndex + 1 : 0; + break; + + case ConsoleKey.Spacebar: + _options[_hoveredIndex].Selected = !_options[_hoveredIndex].Selected; + if (!_multiSelect) + { + if (_selectedIndex > -1 && _hoveredIndex != _selectedIndex) + _options[_selectedIndex].Selected = false; + _selectedIndex = _hoveredIndex; + } + + _error = false; + break; + + case ConsoleKey.Enter: + if (_required) + { + foreach (var option in _options) + { + if (!option.Selected) continue; + end = true; + break; + } + + if (!end) _error = true; + } + else end = true; + + break; + } + + _options[_hoveredIndex].Hovered = true; + Show(); + _prevKey = _key; + } + return ReturnData(); } } @@ -174,4 +252,4 @@ public CheckboxReturn(int index, string option) public string Option => _option; } -} \ No newline at end of file +}