From 28e12f07dd7002bc23f8ca2eb3de0a52ec0b2b25 Mon Sep 17 00:00:00 2001 From: lyzerk Date: Sun, 15 Nov 2020 14:47:12 +0300 Subject: [PATCH 01/13] use componentbase as abstract class with componentbase and futher. It's simplified the library --- src/Attributes/BaseAttribute.cs | 2 +- src/Attributes/CheckboxAttribute.cs | 4 +- src/Attributes/IComponentAttribute.cs | 2 +- src/Attributes/InputAttribute.cs | 2 +- src/Attributes/SelectAttribute.cs | 4 +- src/Components/CheckboxComponent.cs | 32 +++++++------ src/Components/ComponentBase.cs | 67 +++++++++++++++++++++++---- src/Components/ConsoleBase.cs | 1 + src/Components/IComponent.cs | 2 +- src/Components/InputComponent.cs | 33 ++++++------- src/Components/SelectComponent.cs | 31 +++++++------ src/Prompt.cs | 26 +++++++---- src/PromptConfig.cs | 12 +++++ 13 files changed, 147 insertions(+), 71 deletions(-) create mode 100644 src/PromptConfig.cs diff --git a/src/Attributes/BaseAttribute.cs b/src/Attributes/BaseAttribute.cs index 369d2cd..2eeb18f 100644 --- a/src/Attributes/BaseAttribute.cs +++ b/src/Attributes/BaseAttribute.cs @@ -10,7 +10,7 @@ public abstract class BaseAttribute : Attribute, IComponentAttribute { public abstract ComponentType Type { get; } public abstract Type PropertyType { get; } - public abstract IComponent Component { get; } + public abstract ComponentBase Component { get; } public abstract void SetCallback(PropertyInfo prop, object @class); diff --git a/src/Attributes/CheckboxAttribute.cs b/src/Attributes/CheckboxAttribute.cs index 6ce2af8..e0f46b8 100644 --- a/src/Attributes/CheckboxAttribute.cs +++ b/src/Attributes/CheckboxAttribute.cs @@ -8,7 +8,7 @@ public class CheckboxAttribute : BaseAttribute { // we need generic attribute for better support // https://github.com/dotnet/roslyn/pull/26337 - public override IComponent Component { get; } + public override ComponentBase Component { get; } public override ComponentType Type => ComponentType.Checkbox; public override Type PropertyType => _type; private object _fullComponent; @@ -27,7 +27,7 @@ public CheckboxAttribute(Type type, string text, params object[] vals) _genericType = nonGenericType.MakeGenericType(type); _fullComponent = Activator.CreateInstance(_genericType, input, ConvertList(vals, type)); - this.Component = (IComponent)_fullComponent; + this.Component = (ComponentBase)_fullComponent; _type = type; } diff --git a/src/Attributes/IComponentAttribute.cs b/src/Attributes/IComponentAttribute.cs index a3840dc..4fecab4 100644 --- a/src/Attributes/IComponentAttribute.cs +++ b/src/Attributes/IComponentAttribute.cs @@ -4,7 +4,7 @@ namespace PromptCLI { public interface IComponentAttribute { - IComponent Component { get; } + ComponentBase Component { get; } void SetCallback(PropertyInfo prop, object @class); } } \ No newline at end of file diff --git a/src/Attributes/InputAttribute.cs b/src/Attributes/InputAttribute.cs index ec9756c..315c68d 100644 --- a/src/Attributes/InputAttribute.cs +++ b/src/Attributes/InputAttribute.cs @@ -7,7 +7,7 @@ namespace PromptCLI public class InputAttribute : BaseAttribute { private InputComponent _component; - public override IComponent Component => _component; + public override ComponentBase Component => _component; public override ComponentType Type => ComponentType.Input; public override Type PropertyType => typeof(string); diff --git a/src/Attributes/SelectAttribute.cs b/src/Attributes/SelectAttribute.cs index 1fd03cb..fca82d5 100644 --- a/src/Attributes/SelectAttribute.cs +++ b/src/Attributes/SelectAttribute.cs @@ -12,7 +12,7 @@ public class SelectAttribute : BaseAttribute { // we need generic attribute for better support // https://github.com/dotnet/roslyn/pull/26337 - public override IComponent Component { get; } + public override ComponentBase Component { get; } public override ComponentType Type => ComponentType.Select; public override Type PropertyType => _type; private object _fullComponent; @@ -33,7 +33,7 @@ public SelectAttribute(Type type, string text, params object[] vals) _fullComponent = Activator.CreateInstance(_genericType, input, ConvertList(vals, type)); - this.Component = (IComponent)_fullComponent; + this.Component = (ComponentBase)_fullComponent; _type = type; } diff --git a/src/Components/CheckboxComponent.cs b/src/Components/CheckboxComponent.cs index e9491b3..7320b07 100644 --- a/src/Components/CheckboxComponent.cs +++ b/src/Components/CheckboxComponent.cs @@ -4,18 +4,20 @@ namespace PromptCLI { - public class CheckboxComponent : ComponentBase, IComponent> + public class CheckboxComponent : ComponentBase> { private readonly Input> _input; private readonly List _selects; private readonly bool[] _status; - public ComponentType ComponentType => ComponentType.Checkbox; - public Action> CallbackAction { get; private set; } + private Action> _callback; + + public override ComponentType ComponentType => ComponentType.Checkbox; + public override Action> CallbackAction => _callback; public Range Range => _range; - public Input> Result => _input; - public bool IsCompleted { get; set; } + public override Input> Result => _input; + public override bool IsCompleted { get; set; } public CheckboxComponent(Input> input, List selects, IConsoleBase console) @@ -34,10 +36,10 @@ public CheckboxComponent(Input> input, List selects) } - public void Draw(bool defaultValue = true) + public override void Draw(bool defaultValue = true) { - Console.Write(prefix, ConsoleColor.Green); - Console.WriteLine(_input.Text); + Console.Write(prefix, _config.CursorColor); + Console.WriteLine(_input.Text, _config.QuestionColor); foreach(var item in _selects) { @@ -47,7 +49,7 @@ public void Draw(bool defaultValue = true) SetPosition(); } - public void Handle(ConsoleKeyInfo act) + public override void Handle(ConsoleKeyInfo act) { var (result, key) = IsKeyAvailable(act); if (result == KeyInfo.Unknown) @@ -68,7 +70,7 @@ public void Handle(ConsoleKeyInfo act) WriteCurrent(_status[index] ? '•' : ' ', ConsoleColor.DarkRed); } - public void SetTopPosition(int top) + public override void SetTopPosition(int top) { _offsetTop = top; _cursorPointTop = top + 1; // offset 1 for input at the begining @@ -76,9 +78,9 @@ public void SetTopPosition(int top) _maxTop = _selects.Count + 1; } - public int GetTopPosition() => 1; + public override int GetTopPosition() => 1; - public void Complete() + public override void Complete() { _input.Status = _status.Select((i, index) => (status:i, index)).Where(i => i.status).Select(i => _selects[i.index]); // Clear all drawed lines and set the cursor into component start position @@ -99,11 +101,11 @@ public void Complete() CallbackAction?.Invoke(this.Result.Status); } - public void Bind(IPrompt prompt) => _prompt = prompt; + public override void Bind(IPrompt prompt) => _prompt = prompt; - public IPrompt Callback(Action> callback) + public override IPrompt Callback(Action> callback) { - CallbackAction = callback; + _callback = callback; return _prompt; } } diff --git a/src/Components/ComponentBase.cs b/src/Components/ComponentBase.cs index d8d3c32..30566e6 100644 --- a/src/Components/ComponentBase.cs +++ b/src/Components/ComponentBase.cs @@ -3,17 +3,11 @@ namespace PromptCLI { - public abstract class ComponentBase + public abstract class ComponentBase : IComponent { private readonly IConsoleBase _console; protected const string prefix = "> "; - protected int _cursorPointLeft, _offsetLeft, _maxLeft; - protected int _cursorPointTop, _offsetTop, _maxTop; - protected string _regex; - protected Range _range; - protected IPrompt _prompt; - protected IConsoleBase Console => _console; protected ComponentBase(): this(ConsoleBase.Default) @@ -25,6 +19,16 @@ protected ComponentBase(IConsoleBase console) _console = console; } + + protected int _cursorPointLeft, _offsetLeft, _maxLeft; + protected int _cursorPointTop, _offsetTop, _maxTop; + protected string _regex; + protected Range _range; + protected IPrompt _prompt; + + protected PromptConfig _config; + + protected void Direction(ConsoleKey key) { var (left, top) = (_cursorPointLeft, _cursorPointTop); @@ -69,8 +73,53 @@ act.Key switch protected void WriteCurrent(char val, ConsoleColor? textColor = null) => _console.WritePreservePosition(val, _cursorPointLeft, _cursorPointTop, textColor); + internal void SetConfig(PromptConfig config) + { + _config = config; + } + + #region Abstracts + public abstract ComponentType ComponentType { get; } + + public abstract bool IsCompleted { get; set; } + + public abstract int CursorTop { get; } + + public abstract int CursorLeft { get; } + + public abstract void Complete(); + + public abstract void Draw(bool defaultValue = true); + + public abstract int GetTopPosition(); + + public abstract void Handle(ConsoleKeyInfo act); + + public abstract void SetTopPosition(int top); + + #endregion + + } + + public abstract class ComponentBase : ComponentBase, IComponent + { + protected ComponentBase(): base(ConsoleBase.Default) + { + } + + protected ComponentBase(IConsoleBase console): base(console) + { + } + + public override int CursorLeft => _cursorPointLeft; + public override int CursorTop => _cursorPointTop; + + public abstract Input Result { get; } + + public abstract Action CallbackAction { get; } + + public abstract void Bind(IPrompt prompt); - public int CursorLeft => _cursorPointLeft; - public int CursorTop => _cursorPointTop; + public abstract IPrompt Callback(Action callback); } } \ No newline at end of file diff --git a/src/Components/ConsoleBase.cs b/src/Components/ConsoleBase.cs index 0e5742b..440b74f 100644 --- a/src/Components/ConsoleBase.cs +++ b/src/Components/ConsoleBase.cs @@ -110,5 +110,6 @@ public void WriteLine(string val) { Console.WriteLine(val); } + } } \ No newline at end of file diff --git a/src/Components/IComponent.cs b/src/Components/IComponent.cs index 4743266..1329c54 100644 --- a/src/Components/IComponent.cs +++ b/src/Components/IComponent.cs @@ -25,7 +25,7 @@ public interface IComponent public interface IComponentPrompt { - Action CallbackAction { get;} + Action CallbackAction { get; } void Bind(IPrompt prompt); IPrompt Callback(Action callback); } diff --git a/src/Components/InputComponent.cs b/src/Components/InputComponent.cs index 0e93430..91df4f3 100644 --- a/src/Components/InputComponent.cs +++ b/src/Components/InputComponent.cs @@ -2,19 +2,20 @@ namespace PromptCLI { - public class InputComponent : ComponentBase, IComponent + public class InputComponent : ComponentBase { private readonly string _defaultValue; private readonly Input _input; + private Action _callback; - public ComponentType ComponentType => ComponentType.Input; - public Action CallbackAction { get; private set; } + public override ComponentType ComponentType => ComponentType.Input; + public override Action CallbackAction => _callback; public Range Range => _range; - public Input Result => _input; - public bool IsCompleted { get; set; } + public override Input Result => _input; + public override bool IsCompleted { get; set; } public InputComponent(Input input, IConsoleBase console, string defaultValue = default) - :base(console) + : base(console) { _defaultValue = defaultValue; _input = input; @@ -26,11 +27,11 @@ public InputComponent(Input input, string defaultValue = default) { } - public void Draw(bool defaultValue = true) + public override void Draw(bool defaultValue = true) { int startPoint = prefix.Length + _input.Text.Length + 1; - Console.Write(prefix, ConsoleColor.Green); - Console.Write(_input.Text); + Console.Write(prefix, _config.CursorColor); + Console.Write(_input.Text, _config.QuestionColor); if (defaultValue && !string.IsNullOrEmpty(_defaultValue)) { @@ -54,7 +55,7 @@ private void Reset() SetPosition(); } - public void Handle(ConsoleKeyInfo act) + public override void Handle(ConsoleKeyInfo act) { // Special for each component var (result, key) = IsKeyAvailable(act); @@ -89,13 +90,13 @@ public void Handle(ConsoleKeyInfo act) } - public void SetTopPosition(int top) + public override void SetTopPosition(int top) { _cursorPointTop = top; _maxTop = top + 1; } - public void Complete() + public override void Complete() { // if no input detected, then set the result into the input.status if (string.IsNullOrEmpty(_input.Status) && !string.IsNullOrEmpty(_defaultValue)) @@ -115,13 +116,13 @@ public void Complete() CallbackAction?.Invoke(this.Result.Status); } - public int GetTopPosition() => 1; + public override int GetTopPosition() => 1; - public void Bind(IPrompt prompt) => _prompt = prompt; + public override void Bind(IPrompt prompt) => _prompt = prompt; - public IPrompt Callback(Action callback) + public override IPrompt Callback(Action callback) { - CallbackAction = callback; + _callback = callback; return _prompt; } } diff --git a/src/Components/SelectComponent.cs b/src/Components/SelectComponent.cs index 1aea617..5363ba1 100644 --- a/src/Components/SelectComponent.cs +++ b/src/Components/SelectComponent.cs @@ -3,17 +3,18 @@ namespace PromptCLI { - public class SelectComponent : ComponentBase, IComponent + public class SelectComponent : ComponentBase { private readonly Input _input; private readonly IList _selects; public Range Range => _range; - public ComponentType ComponentType => ComponentType.Select; - public Action CallbackAction { get; private set; } - public Input Result => _input; // _selects[_selectedIndex].Value; - public bool IsCompleted { get; set; } + public override ComponentType ComponentType => ComponentType.Select; + public override Action CallbackAction => _callback; + public override Input Result => _input; // _selects[_selectedIndex].Value; + public override bool IsCompleted { get; set; } private int _selectedIndex = -1; + private Action _callback; public SelectComponent(Input input, IList selects, IConsoleBase console) : base(console) @@ -29,10 +30,10 @@ public SelectComponent(Input input, IList selects) { } - public void Draw(bool defaultValue = true) + public override void Draw(bool defaultValue = true) { - Console.Write(prefix, ConsoleColor.Green); - Console.WriteLine(_input.Text); + Console.Write(prefix, _config.CursorColor); + Console.WriteLine(_input.Text, _config.QuestionColor); foreach (var item in _selects) { @@ -48,7 +49,7 @@ private void ChangeSelected(int index) _selectedIndex = index; _input.Status = _selects[_selectedIndex]; } - public void Handle(ConsoleKeyInfo act) + public override void Handle(ConsoleKeyInfo act) { var (result, key) = IsKeyAvailable(act); if (result == KeyInfo.Unknown) @@ -85,7 +86,7 @@ private void toggle(int index = -1) SetPosition(); } - public void SetTopPosition(int top) + public override void SetTopPosition(int top) { _offsetTop = top; _cursorPointTop = top + 1; // offset 1 for input at the begining @@ -93,7 +94,7 @@ public void SetTopPosition(int top) _maxTop = _selects.Count + 1; } - public void Complete() + public override void Complete() { // Clear all drawed lines and set the cursor into component start position for (int i = 0; i < _selects.Count + 1; i++) @@ -113,13 +114,13 @@ public void Complete() CallbackAction?.Invoke(this.Result.Status); } - public int GetTopPosition() => 1; + public override int GetTopPosition() => 1; - public void Bind(IPrompt prompt) => _prompt = prompt; + public override void Bind(IPrompt prompt) => _prompt = prompt; - public IPrompt Callback(Action callback) + public override IPrompt Callback(Action callback) { - CallbackAction = callback; + _callback = callback; return _prompt; } } diff --git a/src/Prompt.cs b/src/Prompt.cs index 88d21de..a883ea6 100644 --- a/src/Prompt.cs +++ b/src/Prompt.cs @@ -6,31 +6,39 @@ namespace PromptCLI { public interface IPrompt { - IComponentPrompt Add(IComponent comp); - void Add(IComponent comp); + IComponentPrompt Add(ComponentBase comp); + void Add(ComponentBase comp); T Run() where T : class, new(); void Begin(); } public class Prompt : IPrompt { - private readonly Queue _components; - private IComponent _currentComponent; + private readonly Queue _components; + private ComponentBase _currentComponent; private int _offsetTop; + private readonly PromptConfig _config; + public Prompt() + : this(new PromptConfig()) { - _components = new Queue(); + } + + public Prompt(PromptConfig config) + { + _components = new Queue(); _offsetTop = 0; + _config = config; } - public IComponentPrompt Add(IComponent comp) + public IComponentPrompt Add(ComponentBase comp) { comp.Bind(this); _components.Enqueue(comp); return comp; } - public void Add(IComponent comp) + public void Add(ComponentBase comp) { _components.Enqueue(comp); } @@ -57,7 +65,7 @@ private void SetPropertyCallback(PropertyInfo prop, T poco) if (prop.PropertyType != attr.PropertyType) throw new Exception($"{prop.Name} is not valid with {attr.PropertyType}-{prop.PropertyType}"); */ - + this.Add(attr.Component); attr.SetCallback(prop, poco); @@ -76,6 +84,8 @@ public void Begin() while (_components.Count > 0) { _currentComponent = _components.Dequeue(); + + _currentComponent.SetConfig(_config); _currentComponent.SetTopPosition(_offsetTop); _currentComponent.Draw(); diff --git a/src/PromptConfig.cs b/src/PromptConfig.cs new file mode 100644 index 0000000..edfed4a --- /dev/null +++ b/src/PromptConfig.cs @@ -0,0 +1,12 @@ +using System; + +namespace PromptCLI +{ + public class PromptConfig + { + public ConsoleColor QuestionColor { get; set; } = ConsoleColor.Magenta; + public ConsoleColor AnswerColor { get; set; } = ConsoleColor.Cyan; + public ConsoleColor CursorColor { get; set; } = ConsoleColor.Red; + } + +} \ No newline at end of file From 06fa13407f4e050a34fb00ac4b8529fc78d7c7aa Mon Sep 17 00:00:00 2001 From: lyzerk Date: Sun, 15 Nov 2020 15:50:30 +0300 Subject: [PATCH 02/13] base correction --- src/Components/ComponentBase.cs | 19 ++++++------------- src/Prompt.cs | 2 +- src/PromptConfig.cs | 1 + 3 files changed, 8 insertions(+), 14 deletions(-) diff --git a/src/Components/ComponentBase.cs b/src/Components/ComponentBase.cs index 30566e6..e0e50b2 100644 --- a/src/Components/ComponentBase.cs +++ b/src/Components/ComponentBase.cs @@ -28,6 +28,10 @@ protected ComponentBase(IConsoleBase console) protected PromptConfig _config; + private bool topBound(int top) => top > _offsetTop && top < _offsetTop + _maxTop; + private bool leftBound(int left) => left >= _range.Start.Value && left <= _range.End.Value; + private KeyInfo isThisAvailable(char key) => + Regex.Match(key.ToString(), _regex, RegexOptions.IgnoreCase).Success ? KeyInfo.Others : KeyInfo.Unknown; protected void Direction(ConsoleKey key) { @@ -49,13 +53,6 @@ protected void Direction(ConsoleKey key) SetPosition(); } protected void SetPosition() => _console.SetPosition(_cursorPointLeft, _cursorPointTop); - - private bool topBound(int top) => top > _offsetTop && top < _offsetTop + _maxTop; - private bool leftBound(int left) => left >= _range.Start.Value && left <= _range.End.Value; - - private KeyInfo isThisAvailable(char key) => - Regex.Match(key.ToString(), _regex, RegexOptions.IgnoreCase).Success ? KeyInfo.Others : KeyInfo.Unknown; - protected (KeyInfo, ConsoleKey) IsKeyAvailable(ConsoleKeyInfo act) => act.Key switch { @@ -82,10 +79,8 @@ internal void SetConfig(PromptConfig config) public abstract ComponentType ComponentType { get; } public abstract bool IsCompleted { get; set; } - - public abstract int CursorTop { get; } - - public abstract int CursorLeft { get; } + public int CursorLeft => _cursorPointLeft; + public int CursorTop => _cursorPointTop; public abstract void Complete(); @@ -111,8 +106,6 @@ protected ComponentBase(IConsoleBase console): base(console) { } - public override int CursorLeft => _cursorPointLeft; - public override int CursorTop => _cursorPointTop; public abstract Input Result { get; } diff --git a/src/Prompt.cs b/src/Prompt.cs index a883ea6..dbbc32d 100644 --- a/src/Prompt.cs +++ b/src/Prompt.cs @@ -20,7 +20,7 @@ public class Prompt : IPrompt private readonly PromptConfig _config; public Prompt() - : this(new PromptConfig()) + : this(PromptConfig.Default) { } diff --git a/src/PromptConfig.cs b/src/PromptConfig.cs index edfed4a..432c5b0 100644 --- a/src/PromptConfig.cs +++ b/src/PromptConfig.cs @@ -4,6 +4,7 @@ namespace PromptCLI { public class PromptConfig { + public static PromptConfig Default { get; } = new PromptConfig(); public ConsoleColor QuestionColor { get; set; } = ConsoleColor.Magenta; public ConsoleColor AnswerColor { get; set; } = ConsoleColor.Cyan; public ConsoleColor CursorColor { get; set; } = ConsoleColor.Red; From 4ec3efc491e173617c5c9c839c3d6ff7919d2b79 Mon Sep 17 00:00:00 2001 From: lyzerk Date: Sun, 15 Nov 2020 17:03:52 +0300 Subject: [PATCH 03/13] cursor prefix --- src/Components/CheckboxComponent.cs | 2 +- src/Components/ComponentBase.cs | 1 - src/Components/InputComponent.cs | 4 ++-- src/Components/SelectComponent.cs | 2 +- src/PromptConfig.cs | 7 ++++--- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Components/CheckboxComponent.cs b/src/Components/CheckboxComponent.cs index 7320b07..1674cce 100644 --- a/src/Components/CheckboxComponent.cs +++ b/src/Components/CheckboxComponent.cs @@ -38,7 +38,7 @@ public CheckboxComponent(Input> input, List selects) public override void Draw(bool defaultValue = true) { - Console.Write(prefix, _config.CursorColor); + Console.Write(_config.Cursor, _config.CursorColor); Console.WriteLine(_input.Text, _config.QuestionColor); foreach(var item in _selects) diff --git a/src/Components/ComponentBase.cs b/src/Components/ComponentBase.cs index e0e50b2..2917eae 100644 --- a/src/Components/ComponentBase.cs +++ b/src/Components/ComponentBase.cs @@ -6,7 +6,6 @@ namespace PromptCLI public abstract class ComponentBase : IComponent { private readonly IConsoleBase _console; - protected const string prefix = "> "; protected IConsoleBase Console => _console; diff --git a/src/Components/InputComponent.cs b/src/Components/InputComponent.cs index 91df4f3..bcadcfc 100644 --- a/src/Components/InputComponent.cs +++ b/src/Components/InputComponent.cs @@ -29,8 +29,8 @@ public InputComponent(Input input, string defaultValue = default) public override void Draw(bool defaultValue = true) { - int startPoint = prefix.Length + _input.Text.Length + 1; - Console.Write(prefix, _config.CursorColor); + int startPoint = _config.Cursor.Length + _input.Text.Length + 1; + Console.Write(_config.Cursor, _config.CursorColor); Console.Write(_input.Text, _config.QuestionColor); if (defaultValue && !string.IsNullOrEmpty(_defaultValue)) diff --git a/src/Components/SelectComponent.cs b/src/Components/SelectComponent.cs index 5363ba1..4834540 100644 --- a/src/Components/SelectComponent.cs +++ b/src/Components/SelectComponent.cs @@ -32,7 +32,7 @@ public SelectComponent(Input input, IList selects) public override void Draw(bool defaultValue = true) { - Console.Write(prefix, _config.CursorColor); + Console.Write(_config.Cursor, _config.CursorColor); Console.WriteLine(_input.Text, _config.QuestionColor); foreach (var item in _selects) diff --git a/src/PromptConfig.cs b/src/PromptConfig.cs index 432c5b0..a2ae6a6 100644 --- a/src/PromptConfig.cs +++ b/src/PromptConfig.cs @@ -5,9 +5,10 @@ namespace PromptCLI public class PromptConfig { public static PromptConfig Default { get; } = new PromptConfig(); - public ConsoleColor QuestionColor { get; set; } = ConsoleColor.Magenta; - public ConsoleColor AnswerColor { get; set; } = ConsoleColor.Cyan; - public ConsoleColor CursorColor { get; set; } = ConsoleColor.Red; + public ConsoleColor QuestionColor { get; } = ConsoleColor.Magenta; + public ConsoleColor AnswerColor { get; } = ConsoleColor.Cyan; + public ConsoleColor CursorColor { get; } = ConsoleColor.Red; + public string Cursor { get; } = "> "; } } \ No newline at end of file From 1305135628a72220682cbb537156c4ab870decb6 Mon Sep 17 00:00:00 2001 From: lyzerk Date: Sun, 15 Nov 2020 19:30:58 +0300 Subject: [PATCH 04/13] colors and little fixes --- src/Components/CheckboxComponent.cs | 13 +++++++------ src/Components/InputComponent.cs | 4 ++-- src/Components/SelectComponent.cs | 4 ++-- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/Components/CheckboxComponent.cs b/src/Components/CheckboxComponent.cs index 1674cce..31e3ca8 100644 --- a/src/Components/CheckboxComponent.cs +++ b/src/Components/CheckboxComponent.cs @@ -51,10 +51,12 @@ public override void Draw(bool defaultValue = true) public override void Handle(ConsoleKeyInfo act) { + var index = _cursorPointTop - _offsetTop - 1; var (result, key) = IsKeyAvailable(act); if (result == KeyInfo.Unknown) { ClearCurrentPosition(); + Check(); return; } else if (result == KeyInfo.Direction) @@ -63,11 +65,10 @@ public override void Handle(ConsoleKeyInfo act) return; } - var index = _cursorPointTop - _offsetTop - 1; - + void Check() => WriteCurrent(_status[index] ? '•' : ' ', ConsoleColor.DarkRed); + _status[index] = !_status[index]; - - WriteCurrent(_status[index] ? '•' : ' ', ConsoleColor.DarkRed); + Check(); } public override void SetTopPosition(int top) @@ -94,9 +95,9 @@ public override void Complete() SetPosition(); // Write the result - Console.Write(_input.Text); + Console.Write(_input.Text, _config.QuestionColor); Console.Write(" > "); - Console.WriteLine(string.Join(",", Result.Status), ConsoleColor.Cyan); + Console.WriteLine(string.Join(",", Result.Status), _config.AnswerColor); CallbackAction?.Invoke(this.Result.Status); } diff --git a/src/Components/InputComponent.cs b/src/Components/InputComponent.cs index bcadcfc..cafae40 100644 --- a/src/Components/InputComponent.cs +++ b/src/Components/InputComponent.cs @@ -109,9 +109,9 @@ public override void Complete() SetPosition(); // Write the result - Console.Write(_input.Text); + Console.Write(_input.Text, _config.QuestionColor); Console.Write(" > "); - Console.WriteLine(_input.Status, ConsoleColor.Cyan); + Console.WriteLine(_input.Status, _config.AnswerColor); CallbackAction?.Invoke(this.Result.Status); } diff --git a/src/Components/SelectComponent.cs b/src/Components/SelectComponent.cs index 4834540..573fee3 100644 --- a/src/Components/SelectComponent.cs +++ b/src/Components/SelectComponent.cs @@ -107,9 +107,9 @@ public override void Complete() SetPosition(); // Write the result - Console.Write(_input.Text); + Console.Write(_input.Text, _config.QuestionColor); Console.Write(" > "); - Console.WriteLine(Result.Status.ToString(), ConsoleColor.Cyan); + Console.WriteLine(Result.Status.ToString(), _config.AnswerColor); CallbackAction?.Invoke(this.Result.Status); } From 569bb0b18fa430e6f5c9253a6e67f90271d0f31c Mon Sep 17 00:00:00 2001 From: lyzerk Date: Sun, 15 Nov 2020 19:40:58 +0300 Subject: [PATCH 05/13] refactor select --- src/Components/SelectComponent.cs | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/src/Components/SelectComponent.cs b/src/Components/SelectComponent.cs index 573fee3..8b3a1ac 100644 --- a/src/Components/SelectComponent.cs +++ b/src/Components/SelectComponent.cs @@ -40,8 +40,8 @@ public override void Draw(bool defaultValue = true) Console.WriteLine(string.Format("( ) {0}", item)); } - ChangeSelected(0); - toggle(0); + // default toggled value's index + Toggle(1); } private void ChangeSelected(int index) @@ -49,6 +49,7 @@ private void ChangeSelected(int index) _selectedIndex = index; _input.Status = _selects[_selectedIndex]; } + public override void Handle(ConsoleKeyInfo act) { var (result, key) = IsKeyAvailable(act); @@ -65,22 +66,31 @@ public override void Handle(ConsoleKeyInfo act) var index = _cursorPointTop - _offsetTop - 1; - SetPosition(); - toggle(); + ClearOldPosition(); + Toggle(index); + } + + private void Toggle(int index) + { ChangeSelected(index); - toggle(index); + + int tempTop = _cursorPointTop; + _cursorPointTop = _offsetTop + 1 + _selectedIndex; + + SetPosition(); + Console.Write('•', ConsoleColor.DarkRed); + _cursorPointTop = tempTop; + SetPosition(); } - private void toggle(int index = -1) + + private void ClearOldPosition() { int tempTop = _cursorPointTop; _cursorPointTop = _offsetTop + 1 + _selectedIndex; SetPosition(); - if (index > -1) - Console.Write('•', ConsoleColor.DarkRed); - else - Console.Write(' '); + Console.Write(' '); _cursorPointTop = tempTop; SetPosition(); From 0e3911afad166189f39ec512cfc2ffc8a1f350f2 Mon Sep 17 00:00:00 2001 From: lyzerk Date: Sun, 15 Nov 2020 20:15:18 +0300 Subject: [PATCH 06/13] simplify the code --- src/Components/SelectComponent.cs | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/src/Components/SelectComponent.cs b/src/Components/SelectComponent.cs index 8b3a1ac..200303c 100644 --- a/src/Components/SelectComponent.cs +++ b/src/Components/SelectComponent.cs @@ -32,7 +32,7 @@ public SelectComponent(Input input, IList selects) public override void Draw(bool defaultValue = true) { - Console.Write(_config.Cursor, _config.CursorColor); + Console.Write(_config.Cursor, _config.CursorColor); Console.WriteLine(_input.Text, _config.QuestionColor); foreach (var item in _selects) @@ -44,12 +44,6 @@ public override void Draw(bool defaultValue = true) Toggle(1); } - private void ChangeSelected(int index) - { - _selectedIndex = index; - _input.Status = _selects[_selectedIndex]; - } - public override void Handle(ConsoleKeyInfo act) { var (result, key) = IsKeyAvailable(act); @@ -72,26 +66,33 @@ public override void Handle(ConsoleKeyInfo act) private void Toggle(int index) { - ChangeSelected(index); - - int tempTop = _cursorPointTop; + _selectedIndex = index; + _input.Status = _selects[_selectedIndex]; + + // make sure the position is ok _cursorPointTop = _offsetTop + 1 + _selectedIndex; + SetPosition(); - SetPosition(); + // write the label Console.Write('•', ConsoleColor.DarkRed); - _cursorPointTop = tempTop; + + // get backward (old) position SetPosition(); } private void ClearOldPosition() { + // if the selected index exists + if (_selectedIndex < 0) + return; + int tempTop = _cursorPointTop; _cursorPointTop = _offsetTop + 1 + _selectedIndex; SetPosition(); Console.Write(' '); - + _cursorPointTop = tempTop; SetPosition(); } @@ -111,7 +112,7 @@ public override void Complete() { Console.ClearLine(_offsetTop + i); } - + _cursorPointLeft = 0; _cursorPointTop = _offsetTop; SetPosition(); From 7cc6df9ea427e2ec2828b4598a8ef8d5a1f085d1 Mon Sep 17 00:00:00 2001 From: lyzerk Date: Sun, 15 Nov 2020 21:07:42 +0300 Subject: [PATCH 07/13] little fixes --- README.md | 5 +---- src/Components/ComponentBase.cs | 21 +++++++++++++++++++-- src/Components/SelectComponent.cs | 8 ++++++-- tests/PromptTests.cs | 2 +- 4 files changed, 27 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index dbec47d..97a8d42 100644 --- a/README.md +++ b/README.md @@ -37,12 +37,9 @@ public class TestClass static void Main(string[] args) { - var obj = new TestClass(); var prompt = new Prompt(); - prompt.AddClass(obj); - - prompt.Begin(); + var obj = prompt.Run(obj); } ``` diff --git a/src/Components/ComponentBase.cs b/src/Components/ComponentBase.cs index 2917eae..a6a1077 100644 --- a/src/Components/ComponentBase.cs +++ b/src/Components/ComponentBase.cs @@ -9,7 +9,8 @@ public abstract class ComponentBase : IComponent protected IConsoleBase Console => _console; - protected ComponentBase(): this(ConsoleBase.Default) + protected ComponentBase() + : this(ConsoleBase.Default) { } @@ -25,7 +26,23 @@ protected ComponentBase(IConsoleBase console) protected Range _range; protected IPrompt _prompt; - protected PromptConfig _config; + protected PromptConfig _config + { + get + { + // for test cases and individual uses + if (__config == null) + __config = PromptConfig.Default; + + return __config; + } + set + { + __config = value; + } + } + + private PromptConfig __config; private bool topBound(int top) => top > _offsetTop && top < _offsetTop + _maxTop; private bool leftBound(int left) => left >= _range.Start.Value && left <= _range.End.Value; diff --git a/src/Components/SelectComponent.cs b/src/Components/SelectComponent.cs index 200303c..ac0e9b9 100644 --- a/src/Components/SelectComponent.cs +++ b/src/Components/SelectComponent.cs @@ -66,6 +66,7 @@ public override void Handle(ConsoleKeyInfo act) private void Toggle(int index) { + // update flags _selectedIndex = index; _input.Status = _selects[_selectedIndex]; @@ -76,7 +77,7 @@ private void Toggle(int index) // write the label Console.Write('•', ConsoleColor.DarkRed); - // get backward (old) position + // go backward (old) position SetPosition(); } @@ -87,12 +88,15 @@ private void ClearOldPosition() return; int tempTop = _cursorPointTop; - _cursorPointTop = _offsetTop + 1 + _selectedIndex; + // get checked last position + _cursorPointTop = _offsetTop + 1 + _selectedIndex; SetPosition(); + // clear old select Console.Write(' '); + // get back to the last position _cursorPointTop = tempTop; SetPosition(); } diff --git a/tests/PromptTests.cs b/tests/PromptTests.cs index 110cb06..98dd784 100644 --- a/tests/PromptTests.cs +++ b/tests/PromptTests.cs @@ -24,7 +24,7 @@ public void Begin_() { var prompt = new Prompt(); var comp = new Mock>(); - var result = prompt.Add(comp.Object); + // var result = prompt.Add(comp.Object); // prompt.Begin(); } From 6cbd143032119bf0cb9e76b61e46e1152dda1646 Mon Sep 17 00:00:00 2001 From: lyzerk Date: Sun, 15 Nov 2020 21:13:30 +0300 Subject: [PATCH 08/13] re-toggle fix --- src/Components/CheckboxComponent.cs | 3 ++- src/Components/SelectComponent.cs | 9 ++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/Components/CheckboxComponent.cs b/src/Components/CheckboxComponent.cs index 31e3ca8..1c97830 100644 --- a/src/Components/CheckboxComponent.cs +++ b/src/Components/CheckboxComponent.cs @@ -56,6 +56,7 @@ public override void Handle(ConsoleKeyInfo act) if (result == KeyInfo.Unknown) { ClearCurrentPosition(); + // if it is checked before the the unknown char. Re-check it. Check(); return; } @@ -66,7 +67,7 @@ public override void Handle(ConsoleKeyInfo act) } void Check() => WriteCurrent(_status[index] ? '•' : ' ', ConsoleColor.DarkRed); - + _status[index] = !_status[index]; Check(); } diff --git a/src/Components/SelectComponent.cs b/src/Components/SelectComponent.cs index ac0e9b9..33e6b86 100644 --- a/src/Components/SelectComponent.cs +++ b/src/Components/SelectComponent.cs @@ -46,10 +46,18 @@ public override void Draw(bool defaultValue = true) public override void Handle(ConsoleKeyInfo act) { + var index = _cursorPointTop - _offsetTop - 1; var (result, key) = IsKeyAvailable(act); if (result == KeyInfo.Unknown) { ClearCurrentPosition(); + + // If the current index is selected. Then re-toggle it. + if (index == _selectedIndex) + { + Toggle(index); + } + return; } else if (result == KeyInfo.Direction) @@ -58,7 +66,6 @@ public override void Handle(ConsoleKeyInfo act) return; } - var index = _cursorPointTop - _offsetTop - 1; ClearOldPosition(); Toggle(index); From 0b9f49578e5865926e9b982d96ee68d438fde206 Mon Sep 17 00:00:00 2001 From: lyzerk Date: Sun, 15 Nov 2020 21:20:07 +0300 Subject: [PATCH 09/13] change default of the select component --- src/Components/SelectComponent.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Components/SelectComponent.cs b/src/Components/SelectComponent.cs index 33e6b86..59ff2af 100644 --- a/src/Components/SelectComponent.cs +++ b/src/Components/SelectComponent.cs @@ -41,7 +41,7 @@ public override void Draw(bool defaultValue = true) } // default toggled value's index - Toggle(1); + Toggle(0); } public override void Handle(ConsoleKeyInfo act) From bce1719ef8ff6c130e1dac9a35e7970e70312dee Mon Sep 17 00:00:00 2001 From: lyzerk Date: Sun, 15 Nov 2020 21:22:04 +0300 Subject: [PATCH 10/13] color change --- src/PromptConfig.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PromptConfig.cs b/src/PromptConfig.cs index a2ae6a6..e4ed333 100644 --- a/src/PromptConfig.cs +++ b/src/PromptConfig.cs @@ -5,7 +5,7 @@ namespace PromptCLI public class PromptConfig { public static PromptConfig Default { get; } = new PromptConfig(); - public ConsoleColor QuestionColor { get; } = ConsoleColor.Magenta; + public ConsoleColor QuestionColor { get; } = ConsoleColor.Gray; public ConsoleColor AnswerColor { get; } = ConsoleColor.Cyan; public ConsoleColor CursorColor { get; } = ConsoleColor.Red; public string Cursor { get; } = "> "; From 6062a7154908d929e14681aac24c2afa3ee5ac74 Mon Sep 17 00:00:00 2001 From: lyzerk Date: Sun, 15 Nov 2020 21:37:33 +0300 Subject: [PATCH 11/13] README.md Update --- README.md | 32 +++++++------------------------- 1 file changed, 7 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 97a8d42..2219fc4 100644 --- a/README.md +++ b/README.md @@ -6,18 +6,7 @@ PromptCLI is inspired from inquirer.js and enquirer.js. It is a interactive comm ![Basics](https://github.com/lyzerk/PromptCLI/raw/master/assets/gifs/basics.gif "Basics") -# Basics - -```csharp -var prompt = new Prompt(); -prompt.Add(new InputComponent("Project Name", "Project1")); -prompt.Add(new SelectComponent("License Type", new List() { "MIT", "Apache", "GNU" } )); -prompt.Add(new CheckboxComponent("Features", new List() { "Linter", "Router", "Other" })); -prompt.Add(new InputComponent("Description")); -prompt.Begin(); -``` - -## Attributes +# Usage ```csharp public class TestClass @@ -44,32 +33,25 @@ static void Main(string[] args) ``` +## Without POCO class (Callback Action) -## Callback Action - -You can handle callback action after each step +You can handle a callback action after each step ```csharp var project = new Project(); var prompt = new Prompt(); prompt.Add(new InputComponent("Project Name", "Project1")) - .Callback(i => project.ProjectName = i) + .Callback(i => project.ProjectName = i) .Add(new SelectComponent("License Type", new List() { "MIT", "Apache", "GNU" } )) - .Callback(i => project.License = i) + .Callback(i => project.License = i) .Add(new CheckboxComponent("Features", new List() { "Linter", "Router", "Other" })) - .Callback(i => project.Features = i) + .Callback(i => project.Features = i) .Add(new InputComponent("Description")) - .Callback(i => project.Description = i); + .Callback(i => project.Description = i); prompt.Begin(); ``` -# Todo - -- Unit tests -- Define data attributes for specify the components -- Linked list implementation on Prompt class - # Contributions All contributions are welcome if well described. From 9ca027507b0a1d34ea5e0d4254f6183c333284c0 Mon Sep 17 00:00:00 2001 From: lyzerk Date: Sun, 15 Nov 2020 21:39:44 +0300 Subject: [PATCH 12/13] README.md config --- README.md | 11 +++++++++++ src/Prompt.cs | 2 -- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 2219fc4..ad9e961 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,17 @@ prompt.Add(new InputComponent("Project Name", "Project1")) prompt.Begin(); ``` +## Config + +```csharp +public class PromptConfig +{ + public ConsoleColor QuestionColor { get; } = ConsoleColor.Gray; + public ConsoleColor AnswerColor { get; } = ConsoleColor.Cyan; + public ConsoleColor CursorColor { get; } = ConsoleColor.Red; + public string Cursor { get; } = "> "; +} +``` # Contributions All contributions are welcome if well described. diff --git a/src/Prompt.cs b/src/Prompt.cs index dbbc32d..e02816c 100644 --- a/src/Prompt.cs +++ b/src/Prompt.cs @@ -54,8 +54,6 @@ public void Add(ComponentBase comp) } } - - private void SetPropertyCallback(PropertyInfo prop, T poco) { if (!(Attribute.GetCustomAttribute(prop, typeof(BaseAttribute)) is BaseAttribute attr)) From d74fb068003d5bad315c925f480a9aa521fb506d Mon Sep 17 00:00:00 2001 From: lyzerk Date: Sun, 15 Nov 2020 22:20:21 +0300 Subject: [PATCH 13/13] fix unit tests --- tests/CheckboxComponentTests.cs | 10 ++++++---- tests/InputComponentTests.cs | 7 +++---- tests/SelectComponentTests.cs | 6 +++--- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/tests/CheckboxComponentTests.cs b/tests/CheckboxComponentTests.cs index 50ccfd4..5930af5 100644 --- a/tests/CheckboxComponentTests.cs +++ b/tests/CheckboxComponentTests.cs @@ -30,7 +30,7 @@ public void Draw_NList_Drawed(int count) component.Draw(); // make sure the input is wrote - console.Verify(i => i.WriteLine(text), Times.Once); + console.Verify(i => i.WriteLine(text, It.IsAny(), null), Times.Once); foreach (var item in list) console.Verify(i => i.WriteLine(string.Format("[ ] {0}", item)), Times.Once); @@ -47,8 +47,8 @@ public void Draw_Range_ValidWithFormat() CheckboxComponent component = new CheckboxComponent(text, list, console.Object); component.Draw(); - // make sure the input is wrote - console.Verify(i => i.WriteLine(text), Times.Once); + // make sure the input has written + console.Verify(i => i.WriteLine(text, It.IsAny(), null), Times.Once); foreach (var item in list) console.Verify(i => i.WriteLine(string.Format("[ ] {0}", item)), Times.Once); @@ -82,6 +82,8 @@ public void Handle_UnknowChar_NotValid(ConsoleKey key) var list = GetList(3); CheckboxComponent component = new CheckboxComponent(text, list, console.Object); + component.SetTopPosition(0); + component.InvokeHandle(key); console.Verify(i => i.ClearCurrentPosition(component.CursorLeft, component.CursorTop), Times.Once); @@ -240,7 +242,7 @@ public void Complete_Input_Redraw() component.Complete(); - console.Verify(i => i.Write(text)); + console.Verify(i => i.Write(text, It.IsAny(), null)); } [Fact] diff --git a/tests/InputComponentTests.cs b/tests/InputComponentTests.cs index 13c45f8..66dd557 100644 --- a/tests/InputComponentTests.cs +++ b/tests/InputComponentTests.cs @@ -125,9 +125,8 @@ public void Draw_InputText_Write() InputComponent component = new InputComponent(text, console.Object); component.Draw(); - // make sure the input is wrote - console.Verify(i => i.Write(text), Times.Once); + console.Verify(i => i.Write(text, It.IsAny(), null), Times.Once); // and make sure the start length must be higher than text length Assert.True(component.Range.Start.Value > text.Length); } @@ -144,7 +143,7 @@ public void Draw_InputTextWithDefault_Write() // make sure the input is wrote string defaultFormattedVal = string.Format(" ({0})", defaultVal); - console.Verify(i => i.Write(text), Times.Once); + console.Verify(i => i.Write(text, It.IsAny(), null), Times.Once); console.Verify(i => i.Write(defaultFormattedVal, It.IsAny(), null), Times.Once); // and make sure the start length must be higher than text length Assert.True(component.Range.Start.Value > text.Length); @@ -196,7 +195,7 @@ public void Complete_InputText_Write() InputComponent component = new InputComponent(text, console.Object); component.Complete(); - console.Verify(i => i.Write(text), Times.Once); + console.Verify(i => i.Write(text, It.IsAny(), null), Times.Once); } diff --git a/tests/SelectComponentTests.cs b/tests/SelectComponentTests.cs index c9dafff..dd5467a 100644 --- a/tests/SelectComponentTests.cs +++ b/tests/SelectComponentTests.cs @@ -30,7 +30,7 @@ public void Draw_NList_Drawed(int count) component.Draw(); // make sure the input is wrote - console.Verify(i => i.WriteLine(text), Times.Once); + console.Verify(i => i.WriteLine(text, It.IsAny(), null), Times.Once); foreach (var item in list) console.Verify(i => i.WriteLine(string.Format("( ) {0}", item)), Times.Once); @@ -47,7 +47,7 @@ public void Draw_Range_ValidWithFormat() component.Draw(); // make sure the input is wrote - console.Verify(i => i.WriteLine(text), Times.Once); + console.Verify(i => i.WriteLine(text, It.IsAny(), null), Times.Once); foreach (var item in list) console.Verify(i => i.WriteLine(string.Format("( ) {0}", item)), Times.Once); @@ -234,7 +234,7 @@ public void Complete_Input_Redraw() component.Complete(); - console.Verify(i => i.Write(text)); + console.Verify(i => i.Write(text, It.IsAny(), null)); } [Fact]