├── src ├── CLUI │ ├── README.md │ ├── Interfaces │ │ ├── ITextDisplay.cs │ │ ├── IInputHandler.cs │ │ ├── IClickable.cs │ │ ├── IFocusable.cs │ │ ├── ILayout.cs │ │ └── IComponent.cs │ ├── Enums │ │ └── Enums.cs │ ├── CLUI.csproj │ ├── Components │ │ ├── PasswordBox.cs │ │ ├── Checkbox.cs │ │ ├── Rect.cs │ │ ├── Label.cs │ │ ├── Button.cs │ │ ├── TextBox.cs │ │ └── Dropdown.cs │ ├── Layouts │ │ ├── Stackpanel.cs │ │ └── GridPanel.cs │ └── Window.cs ├── ShowCase │ ├── ShowCase.csproj │ └── Program.cs └── CLUI.sln ├── LICENSE ├── README.md ├── .gitattributes └── .gitignore /src/CLUI/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/HugoW5/CLUI/HEAD/src/CLUI/README.md -------------------------------------------------------------------------------- /src/CLUI/Interfaces/ITextDisplay.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace CLUI.Interfaces 8 | { 9 | public interface ITextDisplay 10 | { 11 | string Text { get; set; } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/CLUI/Interfaces/IInputHandler.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace CLUI.Interfaces 8 | { 9 | public interface IInputHandler : ITextDisplay 10 | { 11 | public void HandleInput(); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/CLUI/Interfaces/IClickable.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace CLUI.Interfaces 8 | { 9 | public interface IClickable : ITextDisplay 10 | { 11 | public Delegate Click { get; set; } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/ShowCase/ShowCase.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net8.0 6 | enable 7 | enable 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /src/CLUI/Interfaces/IFocusable.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace CLUI.Interfaces 8 | { 9 | public interface IFocusable 10 | { 11 | void OnFocus(); // component gets focus 12 | void OnBlur(); // component loses focus 13 | bool IsFocused { get; set; } // focus state 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/CLUI/Interfaces/ILayout.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace CLUI.Interfaces 8 | { 9 | public interface ILayout : IComponent 10 | { 11 | public List Children { get; } 12 | public void AddChild(IComponent child); 13 | public void RemoveChild(IComponent child); 14 | public void Arrange(); 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/CLUI/Enums/Enums.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace CLUI.Enums 8 | { 9 | public enum HorizontalAlignment 10 | { 11 | Left, 12 | Center, 13 | Right, 14 | } 15 | public enum VerticalAlignment 16 | { 17 | Top, 18 | Middle, 19 | Bottom 20 | } 21 | public enum StackingAlignment 22 | { 23 | Vertical, 24 | Horizontal 25 | } 26 | } -------------------------------------------------------------------------------- /src/CLUI/Interfaces/IComponent.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace CLUI.Interfaces 8 | { 9 | public interface IComponent 10 | { 11 | public int X { get; set; } 12 | public int Y { get; set; } 13 | public int Width { get; set; } 14 | public int Height { get; set; } 15 | public string Id { get; set; } 16 | public ConsoleColor BackGroundColor { get; set; } 17 | /// 18 | /// Renders the component at the specified offset postion. 19 | /// 20 | /// The horizontal offset 21 | /// The vertical offset 22 | public void Render(int offsetX, int offsetY); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/CLUI/CLUI.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net8.0 5 | enable 6 | enable 7 | True 8 | CLUI 9 | Command Line User Interface. 10 | A simple frontend library for console applications 11 | https://github.com/HugoW5/CLUI 12 | README.md 13 | https://github.com/HugoW5/CLUI 14 | git 15 | UI;CLI;CLUI;TUI;GUI; 16 | 1.1.2 17 | Introduced GetComponentById method in Window.cs to retrieve components by Id. 18 | Added an Id propery to every component. 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2025 Hugo 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/CLUI/Components/PasswordBox.cs: -------------------------------------------------------------------------------- 1 | namespace CLUI.Components 2 | { 3 | /// 4 | /// PasswordBox is derived from TextBox. 5 | /// Acts as a TextBox but maskes the inputed charcaters. 6 | /// 7 | public class PasswordBox : TextBox 8 | { 9 | /// 10 | /// Overirdes the TextBox Render method and masking the characters with an asterisk. 11 | /// 12 | /// 13 | /// 14 | public override void Render(int offsetX, int offsetY) 15 | { 16 | if (Height != 1) 17 | { 18 | Height = 1; 19 | } 20 | if (IsFocused) 21 | { 22 | Console.BackgroundColor = FocusColors.Background; 23 | Console.ForegroundColor = FocusColors.Foreground; 24 | } 25 | else 26 | { 27 | Console.BackgroundColor = BackGroundColor; 28 | Console.ForegroundColor = ForeGroundColor; 29 | } 30 | for (int i = 0; i < Width; i++) 31 | { 32 | Console.SetCursorPosition(i + X + offsetX, +Y + offsetY); 33 | Console.Write(' '); 34 | } 35 | Console.SetCursorPosition(X + offsetX, Y + offsetY); 36 | //display placeholder 37 | if (Text.Length == 0) 38 | { 39 | Console.Write(PlaceHolder); 40 | Console.SetCursorPosition(X + offsetX, Y + offsetY); 41 | } 42 | else 43 | { 44 | Console.Write(new string('*', Text.Length)); 45 | } 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/CLUI.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 17 4 | VisualStudioVersion = 17.11.35327.3 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CLUI", "CLUI\CLUI.csproj", "{7E37542D-CE81-4E80-BDC9-56E78DFFA204}" 7 | EndProject 8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ShowCase", "ShowCase\ShowCase.csproj", "{50C8C6C2-430B-42AF-ACAF-E9B5BA9562BC}" 9 | EndProject 10 | Global 11 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 12 | Debug|Any CPU = Debug|Any CPU 13 | Release|Any CPU = Release|Any CPU 14 | EndGlobalSection 15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 16 | {7E37542D-CE81-4E80-BDC9-56E78DFFA204}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 17 | {7E37542D-CE81-4E80-BDC9-56E78DFFA204}.Debug|Any CPU.Build.0 = Debug|Any CPU 18 | {7E37542D-CE81-4E80-BDC9-56E78DFFA204}.Release|Any CPU.ActiveCfg = Release|Any CPU 19 | {7E37542D-CE81-4E80-BDC9-56E78DFFA204}.Release|Any CPU.Build.0 = Release|Any CPU 20 | {50C8C6C2-430B-42AF-ACAF-E9B5BA9562BC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 21 | {50C8C6C2-430B-42AF-ACAF-E9B5BA9562BC}.Debug|Any CPU.Build.0 = Debug|Any CPU 22 | {50C8C6C2-430B-42AF-ACAF-E9B5BA9562BC}.Release|Any CPU.ActiveCfg = Release|Any CPU 23 | {50C8C6C2-430B-42AF-ACAF-E9B5BA9562BC}.Release|Any CPU.Build.0 = Release|Any CPU 24 | EndGlobalSection 25 | GlobalSection(SolutionProperties) = preSolution 26 | HideSolutionNode = FALSE 27 | EndGlobalSection 28 | GlobalSection(ExtensibilityGlobals) = postSolution 29 | SolutionGuid = {C01BC4E4-AB88-4BEA-BD4F-37234E81780A} 30 | EndGlobalSection 31 | EndGlobal 32 | -------------------------------------------------------------------------------- /src/CLUI/Components/Checkbox.cs: -------------------------------------------------------------------------------- 1 | namespace CLUI.Components 2 | { 3 | /// 4 | /// The checkbox component derives from Button but the Click method is not a settable deleagte, use the OnClicked method insted. 5 | /// Incorporates fields such as Checked (bool), CheckedText And UnCheckedText. 6 | /// 7 | public class Checkbox : Button 8 | { 9 | public bool Checked { get; set; } = false; 10 | /// 11 | /// Text to be displayed if the box is checked 12 | /// 13 | public string CheckedText { get; set; } = "[X]"; 14 | /// 15 | /// Text to be displayed if the box is unchecked 16 | /// 17 | public string UnCheckedText { get; set; } = "[ ]"; 18 | public override Delegate Click 19 | { 20 | get => new Action(Toggle); 21 | set => throw new NotSupportedException("Cannot set click property on a checkbox"); 22 | } 23 | public Delegate OnClicked { get; set; } = (bool _checked) => { throw new NotImplementedException(); }; 24 | public override void Render(int offsetX, int offsetY) 25 | { 26 | Text = Checked ? CheckedText : UnCheckedText; 27 | base.Render(offsetX, offsetY); 28 | } 29 | /// 30 | /// Togles the checkbox on and off. 31 | /// 32 | private void Toggle() 33 | { 34 | Checked = !Checked; 35 | Text = Checked ? CheckedText : UnCheckedText; 36 | OnClicked.DynamicInvoke(Checked); 37 | Update(); 38 | } 39 | /// 40 | /// Sets the new bool value and updates the component by re-rendering it 41 | /// 42 | /// 43 | public void SetValue(bool setValue) 44 | { 45 | Checked = setValue; 46 | Text = Checked ? UnCheckedText : CheckedText; 47 | Update(); 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CLUI ![NuGet Version](https://img.shields.io/nuget/v/CLUI) ![GitHub branch check runs](https://img.shields.io/github/check-runs/HugoW5/CLUI/master) 2 | 3 | ## Nuget 4 | https://www.nuget.org/packages/CLUI 5 | ## Example app 6 | ![clui](https://github.com/user-attachments/assets/b6d332a5-23ab-447b-9bd1-ec73282600a2) 7 | 8 | ## Components 9 | 1. Button 10 | 2. Label 11 | 3. Rect 12 | 4. TextBox 13 | 5. PasswordBox 14 | 6. Checkbox 15 | 7. Dropdown 16 | 8. GridPanel 17 | 9. StackPanel 18 | 19 | ## Label 20 | ```cs 21 | Window window = new Window(0, 0, 30, 12); 22 | window.AddComponent(new Label 23 | { 24 | X = 15, 25 | Y = 3, 26 | Width = 20, 27 | HorizontalAlignment = HorizontalAlignment.Center, 28 | Text = "Label1", 29 | }); 30 | window.Render(); 31 | window.HandleInput(); 32 | ``` 33 | 34 | ## Label & Dropdown 35 | 36 | ```cs 37 | Window window = new Window(0, 0, 30, 12); 38 | window.AddComponent(new Label 39 | { 40 | X = 15, 41 | Y = 3, 42 | Width = 20, 43 | HorizontalAlignment = HorizontalAlignment.Center, 44 | Text = "Label1", 45 | }); 46 | window.AddComponent(new Dropdown 47 | { 48 | X = 0, 49 | Y = 0, 50 | Options = { 51 | "Volvo", 52 | "Saab", 53 | "BMW", 54 | "Opel", 55 | "Skóda" 56 | }, 57 | OnSelected = (int index) => 58 | { 59 | var label = ((Label)window.components[0]); 60 | label.Text = index.ToString(); 61 | label.Render(window.X, window.Y); 62 | } 63 | }); 64 | window.Render(); 65 | window.HandleInput(); 66 | ``` 67 | 68 | ## Label & Checkbox 69 | 70 | ```cs 71 | Window window = new Window(0, 0, 30, 12); 72 | window.AddComponent(new Label 73 | { 74 | Width = 25, 75 | HorizontalAlignment = HorizontalAlignment.Center, 76 | X = 5, 77 | Y = 0, 78 | Text = "Label 1", 79 | }); 80 | window.AddComponent(new Checkbox 81 | { 82 | X = 0, 83 | Y = 0, 84 | Checked = true, 85 | OnClicked = (bool _checked) => 86 | { 87 | string text = (_checked ? "Check box is cheked" : "Check box is not cheked"); 88 | var label = ((Label)window.components[0]); 89 | label.Text = text; 90 | label.Update(); 91 | } 92 | }); 93 | ``` 94 | -------------------------------------------------------------------------------- /src/CLUI/Components/Rect.cs: -------------------------------------------------------------------------------- 1 | using CLUI.Interfaces; 2 | 3 | namespace CLUI.Components 4 | { 5 | /// 6 | /// The Rect component is only used as styling 7 | /// Supports attibues such as BorderColor and BorderThickness. 8 | /// 9 | public class Rect : IComponent 10 | { 11 | public int X { get; set; } 12 | public int Y { get; set; } 13 | public int Width { get; set; } 14 | public int Height { get; set; } = 1; 15 | public string Id { get; set; } = string.Empty; 16 | public int BorderThickness { get; set; } = 0; // only set to 1 17 | public ConsoleColor BorderColor { get; set; } = ConsoleColor.DarkGray; 18 | public ConsoleColor BackGroundColor { get; set; } = ConsoleColor.Gray; 19 | 20 | 21 | public void Render(int offsetX, int offsetY) 22 | { 23 | 24 | Console.BackgroundColor = BackGroundColor; 25 | for (int i = 0; i < Width; i++) 26 | { 27 | for (int j = 0; j < Height; j++) 28 | { 29 | 30 | Console.SetCursorPosition(i + X + offsetX, j + Y + offsetY); 31 | Console.WriteLine(' '); 32 | } 33 | } 34 | if (BorderThickness > 0) 35 | { 36 | Console.BackgroundColor = BorderColor; 37 | //Vertical Borders 38 | for (int i = 0; i < Height; i++) 39 | { 40 | Console.SetCursorPosition(X + offsetX, i + offsetY + Y); 41 | Console.Write(' '); 42 | Console.SetCursorPosition(X + Width + offsetX, i + offsetY + Y); 43 | Console.Write(' '); 44 | } 45 | //Horizontal Borders 46 | for (int i = 0; i < Width; i++) 47 | { 48 | Console.SetCursorPosition(X + i + offsetX, offsetY + Y); 49 | Console.Write(' '); 50 | Console.SetCursorPosition(X + i + offsetX, Height + Y + offsetY); 51 | Console.Write(" "); // 2 because it work 52 | } 53 | 54 | } 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/CLUI/Components/Label.cs: -------------------------------------------------------------------------------- 1 | using CLUI.Enums; 2 | using CLUI.Interfaces; 3 | 4 | namespace CLUI.Components 5 | { 6 | /// 7 | /// Represent a Label component used for rendering basic text. 8 | /// Supports attibutes such as HorizontalAlignment. 9 | /// 10 | public class Label : IComponent 11 | { 12 | public int X { get; set; } 13 | public int Y { get; set; } 14 | public int Width { get; set; } = 0; 15 | public int Height { get; set; } = 1; 16 | public string Text { get; set; } = "text"; 17 | public ConsoleColor BackGroundColor { get; set; } = ConsoleColor.Gray; 18 | public ConsoleColor ForeGroundColor { get; set; } = ConsoleColor.Black; 19 | /// 20 | /// HorizontalAlignment Enum can be 21 | /// Left or Center. Default is Left 22 | /// 23 | public HorizontalAlignment HorizontalAlignment { get; set; } = 0; 24 | public string Id { get; set; } = string.Empty; 25 | 26 | private int _offsetX = 0; 27 | private int _offsetY = 0; 28 | public void Render(int offsetX, int offsetY) 29 | { 30 | _offsetX = offsetX; 31 | _offsetY = offsetY; 32 | 33 | if (Width == 0) 34 | { 35 | Width = Text.Length; 36 | } 37 | 38 | Console.ForegroundColor = ForeGroundColor; 39 | Console.BackgroundColor = BackGroundColor; 40 | if (HorizontalAlignment == HorizontalAlignment.Left) 41 | { 42 | if (Width < Text.Length) 43 | { 44 | Width = Text.Length; 45 | } 46 | Console.SetCursorPosition(X + offsetX, Y + offsetY); 47 | string outputText = Text; 48 | outputText += new string(' ', Width - Text.Length); 49 | Console.Write(outputText); 50 | } 51 | //Make the text appear in the center using a left offset calculated by the Label Witdh and text length 52 | if (HorizontalAlignment == HorizontalAlignment.Center) 53 | { 54 | int totalWhitespace = Width - Text.Length+1; 55 | if (totalWhitespace < 0) totalWhitespace = 0; // ensure no negative offset 56 | int leftPadding = totalWhitespace / 2; 57 | int rightPadding = totalWhitespace - leftPadding; 58 | 59 | Console.SetCursorPosition(X + offsetX, Y + offsetY); 60 | string outputText = new string(' ', leftPadding) + Text + new string(' ', rightPadding); 61 | Console.Write(outputText); 62 | } 63 | } 64 | /// 65 | /// Re-renders the Label compoent at it's last known postion. 66 | /// 67 | public void Update() 68 | { 69 | Render(_offsetX, _offsetY); 70 | } 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Set default behavior to automatically normalize line endings. 3 | ############################################################################### 4 | * text=auto 5 | 6 | ############################################################################### 7 | # Set default behavior for command prompt diff. 8 | # 9 | # This is need for earlier builds of msysgit that does not have it on by 10 | # default for csharp files. 11 | # Note: This is only used by command line 12 | ############################################################################### 13 | #*.cs diff=csharp 14 | 15 | ############################################################################### 16 | # Set the merge driver for project and solution files 17 | # 18 | # Merging from the command prompt will add diff markers to the files if there 19 | # are conflicts (Merging from VS is not affected by the settings below, in VS 20 | # the diff markers are never inserted). Diff markers may cause the following 21 | # file extensions to fail to load in VS. An alternative would be to treat 22 | # these files as binary and thus will always conflict and require user 23 | # intervention with every merge. To do so, just uncomment the entries below 24 | ############################################################################### 25 | #*.sln merge=binary 26 | #*.csproj merge=binary 27 | #*.vbproj merge=binary 28 | #*.vcxproj merge=binary 29 | #*.vcproj merge=binary 30 | #*.dbproj merge=binary 31 | #*.fsproj merge=binary 32 | #*.lsproj merge=binary 33 | #*.wixproj merge=binary 34 | #*.modelproj merge=binary 35 | #*.sqlproj merge=binary 36 | #*.wwaproj merge=binary 37 | 38 | ############################################################################### 39 | # behavior for image files 40 | # 41 | # image files are treated as binary by default. 42 | ############################################################################### 43 | #*.jpg binary 44 | #*.png binary 45 | #*.gif binary 46 | 47 | ############################################################################### 48 | # diff behavior for common document formats 49 | # 50 | # Convert binary document formats to text before diffing them. This feature 51 | # is only available from the command line. Turn it on by uncommenting the 52 | # entries below. 53 | ############################################################################### 54 | #*.doc diff=astextplain 55 | #*.DOC diff=astextplain 56 | #*.docx diff=astextplain 57 | #*.DOCX diff=astextplain 58 | #*.dot diff=astextplain 59 | #*.DOT diff=astextplain 60 | #*.pdf diff=astextplain 61 | #*.PDF diff=astextplain 62 | #*.rtf diff=astextplain 63 | #*.RTF diff=astextplain 64 | -------------------------------------------------------------------------------- /src/CLUI/Components/Button.cs: -------------------------------------------------------------------------------- 1 | using CLUI.Enums; 2 | using CLUI.Interfaces; 3 | 4 | namespace CLUI.Components 5 | { 6 | /// 7 | /// Represents a button component. 8 | /// Supporting focus, click handling and rendering 9 | /// 10 | public class Button : IComponent, IFocusable, IClickable 11 | { 12 | public string Text { get; set; } = ""; 13 | public bool IsFocused { get; set; } = false; 14 | public int X { get; set; } 15 | public int Y { get; set; } 16 | public int Width { get; set; } = 1; 17 | public int Height { get; set; } = 1; 18 | public string Id { get; set; } = string.Empty; 19 | public HorizontalAlignment HorizontalAlignment { get; set; } 20 | public virtual Delegate Click { get; set; } = void () => { Console.Write("\a"); }; // virtual for use in Checkbox 21 | public ConsoleColor BackGroundColor { get; set; } = ConsoleColor.DarkBlue; 22 | public ConsoleColor ForeGroundColor { get; set; } = ConsoleColor.DarkGray; 23 | /// 24 | /// (Background, Foreground) 25 | /// Colors when focused 26 | /// 27 | public (ConsoleColor Background, ConsoleColor Foreground) FocusColors { get; set; } = (ConsoleColor.Blue, ConsoleColor.White); 28 | 29 | private int _offsetX = 0; 30 | private int _offsetY = 0; 31 | /// 32 | /// Handles the focus event by setting the button as focues and triggering a re-render 33 | /// 34 | public void OnFocus() 35 | { 36 | IsFocused = true; 37 | Render(_offsetX, _offsetY); 38 | } 39 | /// 40 | /// Handles the focus event by setting the button as not focused and triggering a re-render 41 | /// 42 | public void OnBlur() 43 | { 44 | IsFocused = false; 45 | Render(_offsetX, _offsetY); 46 | } 47 | public virtual void Render(int offsetX, int offsetY) 48 | { 49 | _offsetX = offsetX; 50 | _offsetY = offsetY; 51 | 52 | if (Width == 1) 53 | { 54 | Width = Text.Length; 55 | } 56 | if (IsFocused) 57 | { 58 | Console.BackgroundColor = FocusColors.Background; 59 | Console.ForegroundColor = FocusColors.Foreground; 60 | } 61 | else 62 | { 63 | Console.BackgroundColor = BackGroundColor; 64 | Console.ForegroundColor = ForeGroundColor; 65 | } 66 | if (HorizontalAlignment == HorizontalAlignment.Left) 67 | { 68 | if (Width < Text.Length) 69 | { 70 | Width = Text.Length; 71 | } 72 | Console.SetCursorPosition(X + offsetX, Y + offsetY); 73 | string outputText = Text; 74 | outputText += new string(' ', Width - Text.Length); 75 | Console.Write(outputText); 76 | } 77 | if (HorizontalAlignment == HorizontalAlignment.Center) 78 | { 79 | int totalWhitespace = Width+1 - Text.Length; 80 | if (totalWhitespace < 0) totalWhitespace = 0; // no negative offset 81 | int leftPadding = totalWhitespace / 2; 82 | int rightPadding = totalWhitespace - leftPadding; 83 | 84 | Console.SetCursorPosition(X + offsetX, Y + offsetY); 85 | string outputText = new string(' ', leftPadding) + Text + new string(' ', rightPadding); 86 | Console.Write(outputText); 87 | Console.ResetColor(); 88 | } 89 | } 90 | /// 91 | /// Updates the button by re-rendering it at it's last known offset postion 92 | /// 93 | public void Update() 94 | { 95 | Render(_offsetX, _offsetY); 96 | } 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /src/ShowCase/Program.cs: -------------------------------------------------------------------------------- 1 | using CLUI; 2 | using CLUI.Components; 3 | using CLUI.Enums; 4 | using CLUI.Layouts; 5 | using System.Net.WebSockets; 6 | using System.Runtime.CompilerServices; 7 | using System.Security.Principal; 8 | 9 | 10 | namespace ShowCase 11 | { 12 | internal class Program 13 | { 14 | static void Main(string[] args) 15 | { 16 | Console.CursorVisible = false; 17 | Window window = new Window(0, 0,40, 20); 18 | 19 | int count = 1; 20 | window.AddComponent(new Label 21 | { 22 | X = 0, 23 | Y = 2, 24 | Width = window.Width, 25 | HorizontalAlignment = HorizontalAlignment.Center, 26 | Text = count.ToString(), 27 | BackGroundColor = ConsoleColor.White, 28 | Id = "count" 29 | }); 30 | 31 | window.AddComponent(new StackPanel 32 | { 33 | X = 0, 34 | Y = 4, 35 | Width = window.Width, 36 | BackGroundColor = ConsoleColor.White, 37 | StackingAlignment = StackingAlignment.Horizontal, 38 | Spacing = 2, 39 | Children = { 40 | new Button{ 41 | Text="Add (+)", 42 | Width = (window.Width/2)-1, 43 | HorizontalAlignment= HorizontalAlignment.Center, 44 | BackGroundColor= ConsoleColor.DarkGreen, 45 | FocusColors=(ConsoleColor.Green, 46 | ConsoleColor.White), 47 | Click= () => { 48 | count++; 49 | ((Label)window.GetComponentById("count")).Text = "Amount: " + count.ToString(); 50 | ((Label)window.GetComponentById("count")).Update(); 51 | } 52 | }, 53 | new Button{ 54 | Text="Subtract (-)", 55 | Width = (window.Width/2)-1, 56 | HorizontalAlignment= HorizontalAlignment.Center, 57 | BackGroundColor= ConsoleColor.DarkRed, 58 | FocusColors=(ConsoleColor.Red, 59 | ConsoleColor.White), 60 | Click= () => { 61 | count--; 62 | ((Label)window.GetComponentById("count")).Text = "Amount: " + count.ToString(); 63 | ((Label)window.GetComponentById("count")).Update(); 64 | } 65 | }, 66 | } 67 | }); 68 | 69 | window.AddComponent(new Button 70 | { 71 | X = 5, 72 | Y = 6, 73 | Width = window.Width-10, 74 | Text = "Reset Counter", 75 | HorizontalAlignment = HorizontalAlignment.Center, 76 | Click = () => 77 | { 78 | count = 0; 79 | ((Label)window.GetComponentById("count")).Text = "Amount: " + count.ToString(); 80 | ((Label)window.GetComponentById("count")).Update(); 81 | } 82 | }); 83 | 84 | 85 | window.AddComponent(new Dropdown 86 | { 87 | X = 12, 88 | Y = 8, 89 | Options = { 90 | "+10", 91 | "+100", 92 | "+1000", 93 | "+1000000", 94 | "+1000000000", 95 | }, 96 | OnSelected = (int index) => 97 | { 98 | int plusCount = 0; 99 | switch (index) 100 | { 101 | case 0: 102 | plusCount = 10; 103 | break; 104 | case 1: 105 | plusCount = 100; 106 | break; 107 | case 2: 108 | plusCount = 1000; 109 | break; 110 | case 3: 111 | plusCount = 1000000; 112 | break; 113 | case 4: 114 | plusCount = 1000000000; 115 | break; 116 | } 117 | 118 | count += plusCount; 119 | ((Label)window.GetComponentById("count")).Text = "Amount: " + count.ToString(); 120 | ((Label)window.GetComponentById("count")).Update(); 121 | } 122 | 123 | }); 124 | 125 | window.Render(); 126 | window.HandleInput(); 127 | } 128 | } 129 | } 130 | -------------------------------------------------------------------------------- /src/CLUI/Layouts/Stackpanel.cs: -------------------------------------------------------------------------------- 1 | using CLUI.Interfaces; 2 | using CLUI.Enums; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | using CLUI.Components; 9 | 10 | namespace CLUI.Layouts 11 | { 12 | /// 13 | /// Represent a stack-based layout panel. 14 | /// The StackPanel arranges child components either horizontally or vertically. 15 | /// Supports Adding and Remoing children. 16 | /// 17 | public class StackPanel : ILayout 18 | { 19 | public int X { get; set; } 20 | public int Y { get; set; } 21 | public int Width { get; set; } 22 | public int Height { get; set; } 23 | public string Id { get; set; } = string.Empty; 24 | public ConsoleColor BackGroundColor { get; set; } 25 | public ConsoleColor ForeGroundColor { get; set; } 26 | public List Children { get; private set; } = new List(); 27 | /// 28 | /// The StackingAlignment Enum sets how the Arrange() method arrages the child components. 29 | /// It Supports Horizontal and Vertical Stacking. 30 | /// 31 | public StackingAlignment StackingAlignment { get; set; } = StackingAlignment.Vertical; // Default to vertical stacking 32 | public int Spacing { get; set; } = 0; // Space between children 33 | 34 | /// 35 | /// Add a child to the StackPanel. 36 | /// And arrange the StackPanel. 37 | /// 38 | /// The child to add to the StackPanel 39 | public void AddChild(IComponent child) 40 | { 41 | Children.Add(child); 42 | Arrange(); 43 | } 44 | /// 45 | /// Add a list of child components. 46 | /// 47 | /// The list of components 48 | public void AddChild(List children) 49 | { 50 | foreach (IComponent child in children) 51 | { 52 | Children.Add(child); 53 | } 54 | Arrange(); 55 | } 56 | /// 57 | /// Removes a child from the StackPanel 58 | /// 59 | /// The child to remove 60 | public void RemoveChild(IComponent child) 61 | { 62 | Children.Remove(child); 63 | Arrange(); 64 | } 65 | /// 66 | /// Arranges the child-components inside the StackPanel. 67 | /// 68 | public void Arrange() 69 | { 70 | int offsetX = X; 71 | int offsetY = Y; 72 | 73 | //Make sure that every component with text has the minimun width of the text. 74 | foreach (var child in Children) 75 | { 76 | if (child is ITextDisplay && child is IComponent tmpComponent) 77 | { 78 | if (tmpComponent is ITextDisplay textDisplay) 79 | { 80 | if (tmpComponent.Width < textDisplay.Text.Length) 81 | { 82 | tmpComponent.Width = textDisplay.Text.Length; 83 | } 84 | } 85 | 86 | } 87 | 88 | child.X = offsetX; 89 | child.Y = offsetY; 90 | 91 | if (StackingAlignment == 0) 92 | { 93 | offsetY += child.Height + Spacing; 94 | } 95 | else 96 | { 97 | offsetX += child.Width + Spacing; 98 | } 99 | } 100 | } 101 | 102 | public void Render(int offsetX, int offsetY) 103 | { 104 | //Arrange the child components 105 | Arrange(); 106 | 107 | Console.BackgroundColor = BackGroundColor; 108 | for (int row = 0; row < Height; row++) 109 | { 110 | Console.SetCursorPosition(offsetX + X, offsetY + Y + row); 111 | Console.Write(new string(' ', Width+1)); 112 | } 113 | Console.ResetColor(); 114 | } 115 | } 116 | 117 | } 118 | -------------------------------------------------------------------------------- /src/CLUI/Components/TextBox.cs: -------------------------------------------------------------------------------- 1 | using CLUI.Interfaces; 2 | 3 | namespace CLUI.Components 4 | { 5 | /// 6 | /// Textbox component is used for taking user input. 7 | /// Supports attibutes such as Placeholder. 8 | /// - - Does Not support arrow-key navigation in the text. 9 | /// 10 | public class TextBox : IComponent, IFocusable, IInputHandler 11 | { 12 | public bool IsFocused { get; set; } = false; 13 | public int X { get; set; } 14 | public int Y { get; set; } 15 | public int Width { get; set; } 16 | public int Height { get; set; } = 1; 17 | /// 18 | /// The text displayed on the textbox input when it is empty. 19 | /// 20 | public string PlaceHolder { get; set; } = ""; 21 | /// 22 | /// The user-inputted text gets stored in this variable 23 | /// 24 | public string Text { get; set; } = ""; 25 | public string Id { get; set; } = string.Empty; 26 | public ConsoleColor BackGroundColor { get; set; } = ConsoleColor.DarkGray; 27 | public ConsoleColor ForeGroundColor { get; set; } = ConsoleColor.Gray; 28 | /// 29 | /// (Background, Foreground) 30 | /// Colors when focused 31 | /// 32 | public (ConsoleColor Background, ConsoleColor Foreground) FocusColors { get; set; } = (ConsoleColor.Gray, ConsoleColor.Black); 33 | 34 | private int _offsetX = 0; 35 | private int _offsetY = 0; 36 | /// 37 | /// Handles user-input. Seeks for ESC, Tab and Enter, 38 | /// The Textbox component exits input-mode if one of those keys are clicked. 39 | /// 40 | public void HandleInput() 41 | { 42 | bool runFunction = true; 43 | while (runFunction) 44 | { 45 | Console.CursorVisible = true; 46 | if (Console.KeyAvailable) 47 | { 48 | ConsoleKeyInfo key = Console.ReadKey(intercept: true); 49 | switch (key.Key) 50 | { 51 | case ConsoleKey.Tab: 52 | // Move focus to the next component 53 | Console.CursorVisible = false; 54 | runFunction = false; 55 | break; 56 | case ConsoleKey.Escape: 57 | // Move close this and focus to the next component 58 | Console.CursorVisible = false; 59 | runFunction = false; 60 | break; 61 | case ConsoleKey.Enter: 62 | // Move close this and focus to the next component 63 | Console.CursorVisible = false; 64 | runFunction = false; 65 | break; 66 | default: 67 | GetInput(key); 68 | break; 69 | } 70 | } 71 | } 72 | } 73 | /// 74 | /// Gets the user inputted key presses and converts them into 75 | /// chars inorder to add them to the Text attribute. 76 | /// Supports backspace by removing the last char in the text-string 77 | /// 78 | /// The Key (ConsoleKeyInfo) 79 | private void GetInput(ConsoleKeyInfo key) 80 | { 81 | if (key.Key == ConsoleKey.Backspace) 82 | { 83 | if (Text.Length > 0) 84 | { 85 | Text = Text.Substring(0, Text.Length - 1); 86 | } 87 | } 88 | else 89 | { 90 | if (Text.Length <= Width-1) 91 | { 92 | Text += key.KeyChar; 93 | } 94 | } 95 | 96 | Render(_offsetX, _offsetY); 97 | } 98 | public void OnBlur() 99 | { 100 | IsFocused = false; 101 | Render(_offsetX, _offsetY); 102 | } 103 | 104 | public void OnFocus() 105 | { 106 | IsFocused = true; 107 | Render(_offsetX, _offsetY); 108 | } 109 | 110 | public virtual void Render(int offsetX, int offsetY) 111 | { 112 | _offsetX = offsetX; 113 | _offsetY = offsetY; 114 | //height can only be = 1 115 | if (Height != 1) 116 | { 117 | Height = 1; 118 | } 119 | if (IsFocused) 120 | { 121 | Console.BackgroundColor = FocusColors.Background; 122 | Console.ForegroundColor = FocusColors.Foreground; 123 | } 124 | else 125 | { 126 | Console.BackgroundColor = BackGroundColor; 127 | Console.ForegroundColor = ForeGroundColor; 128 | } 129 | for (int i = 0; i < Width; i++) 130 | { 131 | Console.SetCursorPosition(i + X + offsetX, +Y + offsetY); 132 | Console.Write(' '); 133 | } 134 | Console.SetCursorPosition(X + offsetX, Y + offsetY); 135 | //display placeholder 136 | if(Text.Length == 0) 137 | { 138 | Console.Write(PlaceHolder); 139 | Console.SetCursorPosition(X + offsetX, Y + offsetY); 140 | } 141 | else 142 | { 143 | Console.Write(Text); 144 | } 145 | } 146 | } 147 | } 148 | -------------------------------------------------------------------------------- /src/CLUI/Layouts/GridPanel.cs: -------------------------------------------------------------------------------- 1 | using CLUI.Interfaces; 2 | using System; 3 | using System.Collections.Generic; 4 | 5 | namespace CLUI.Layouts 6 | { 7 | /// 8 | /// The GridPanel compoent Represents a grid-based layout. 9 | /// It organizes child compnents into a specifed numbers of collumns and rows. 10 | /// Provied methods to Add, Remove and Arrange componnts within the grid. 11 | /// 12 | public class GridPanel : ILayout 13 | { 14 | public int X { get; set; } 15 | public int Y { get; set; } 16 | public int Width { get; set; } 17 | public int Height { get; set; } 18 | public string Id { get; set; } = string.Empty; 19 | public ConsoleColor BackGroundColor { get; set; } 20 | public ConsoleColor ForeGroundColor { get; set; } 21 | 22 | public int Rows { get; } 23 | public int Columns { get; } 24 | /// 25 | /// Space between cells 26 | /// 27 | public int Spacing { get; set; } = 0; 28 | 29 | public List Children { get; private set; } = new List(); 30 | 31 | private IComponent?[,] grid; // 2D array to hold components in grid cells 32 | 33 | public GridPanel(int rows, int columns) 34 | { 35 | Rows = rows; 36 | Columns = columns; 37 | grid = new IComponent[rows, columns]; // Initialize grid 38 | } 39 | 40 | public void AddChild(IComponent child) 41 | { 42 | // Find first available position (row, column) for the child 43 | for (int row = 0; row < Rows; row++) 44 | { 45 | for (int col = 0; col < Columns; col++) 46 | { 47 | if (grid[row, col] == null) 48 | { 49 | grid[row, col] = child; 50 | Children.Add(child); 51 | Arrange(); 52 | return; 53 | } 54 | } 55 | } 56 | 57 | throw new InvalidOperationException("No available space in the grid."); 58 | } 59 | /// 60 | /// Adds a child to the grid in the specifed row and column. 61 | /// Add arranges the componets. 62 | /// 63 | /// The component 64 | /// The row number 65 | /// The column number 66 | /// Row or column is out of grid bounds 67 | /// Cell is already occupied. 68 | public void AddChild(IComponent child, int row, int column) 69 | { 70 | if (row >= Rows || column >= Columns) 71 | throw new ArgumentOutOfRangeException("Row or column is out of grid bounds."); 72 | 73 | if (grid[row, column] != null) 74 | throw new InvalidOperationException($"Cell ({row}, {column}) is already occupied."); 75 | 76 | grid[row, column] = child; 77 | Children.Add(child); 78 | Arrange(); 79 | } 80 | /// 81 | /// Removes the specifed child from the grid. 82 | /// And Arranges the components when it's deleted. 83 | /// 84 | /// 85 | /// The specifed child is not in the grid 86 | public void RemoveChild(IComponent child) 87 | { 88 | // Find and remove child from grid 89 | for (int row = 0; row < Rows; row++) 90 | { 91 | for (int col = 0; col < Columns; col++) 92 | { 93 | if (grid[row, col] == child) 94 | { 95 | grid[row, col] = null; 96 | Children.Remove(child); 97 | Arrange(); 98 | return; 99 | } 100 | } 101 | } 102 | 103 | throw new InvalidOperationException("Child not found in grid."); 104 | } 105 | 106 | /// 107 | /// Arranges the child-components inside the grid. 108 | /// 109 | public void Arrange() 110 | { 111 | int cellWidth = Width / Columns; 112 | int cellHeight = Height / Rows; 113 | 114 | foreach (var child in Children) 115 | { 116 | // Find the position for the child 117 | for (int row = 0; row < Rows; row++) 118 | { 119 | for (int col = 0; col < Columns; col++) 120 | { 121 | if (grid[row, col] == child) 122 | { 123 | child.X = X + col * (cellWidth + Spacing); 124 | child.Y = Y + row * (cellHeight + Spacing); 125 | child.Width = Math.Min(cellWidth, child.Width); 126 | child.Height = Math.Min(cellHeight, child.Height); 127 | break; 128 | } 129 | } 130 | } 131 | } 132 | } 133 | 134 | public void Render(int offsetX, int offsetY) 135 | { 136 | // Render background 137 | Console.BackgroundColor = BackGroundColor; 138 | for (int row = 0; row < Height; row++) 139 | { 140 | Console.SetCursorPosition(offsetX + X, offsetY + Y + row); 141 | Console.Write(new string(' ', Width)); 142 | } 143 | } 144 | } 145 | } 146 | -------------------------------------------------------------------------------- /src/CLUI/Components/Dropdown.cs: -------------------------------------------------------------------------------- 1 | using CLUI.Interfaces; 2 | using IComponent = CLUI.Interfaces.IComponent; 3 | 4 | namespace CLUI.Components 5 | { 6 | /// 7 | /// Represents a dropdown component that allows users to select an option from a list. 8 | /// The dropdown is navigated by the use of arrow-keys 9 | /// 10 | public class Dropdown : IComponent, IFocusable, IInputHandler 11 | { 12 | public int X { get; set; } 13 | public int Y { get; set; } 14 | public int Width { get; set; } = 0; 15 | public int Height { get; set; } = 1; 16 | public string Id { get; set; } = string.Empty; 17 | public ConsoleColor BackGroundColor { get; set; } = ConsoleColor.DarkBlue; 18 | public ConsoleColor ForeGroundColor { get; set; } = ConsoleColor.DarkGray; 19 | /// 20 | /// (Background, Foreground) 21 | /// Colors when focused 22 | /// 23 | public (ConsoleColor Background, ConsoleColor Foreground) FocusColors { get; set; } = (ConsoleColor.Blue, ConsoleColor.White); 24 | public List Options { get; set; } = new List(); 25 | public int SelectedIndex { get; set; } = -1; // -1 = no selection 26 | /// 27 | /// Runs when the user has selected an item 28 | /// Parameter index is the selected items postions in Options list 29 | /// 30 | public Delegate OnSelected { get; set; } = (int index) => { throw new NotImplementedException(); }; 31 | public bool IsOpen { get; private set; } = false; 32 | public bool IsFocused { get; set; } = false; 33 | public string Text { get; set; } = "Select..."; 34 | 35 | private string? SelectedValue = null; 36 | private int _offsetX = 0; 37 | private int _offsetY = 0; 38 | /// 39 | /// Re-renders the dropdown component with the options listed. 40 | /// 41 | public void Open() 42 | { 43 | if (!IsOpen) 44 | { 45 | IsOpen = true; 46 | Height = Options.Count + 1; 47 | } 48 | //Re render with open or closed togglew 49 | Render(_offsetX, _offsetY); 50 | } 51 | /// 52 | /// Re-redners the dropdown compoent with the option-list closed 53 | /// 54 | public void Close() 55 | { 56 | if (IsOpen) 57 | { 58 | IsOpen = false; 59 | Height = 1; 60 | //OnDropdownClosed?.Invoke(); 61 | ClearOptions(_offsetX, _offsetY); 62 | Render(_offsetX, _offsetY); 63 | } 64 | } 65 | 66 | //Render the dropdown 67 | public void Render(int offsetX, int offsetY) 68 | { 69 | //Calculate actual position 70 | _offsetX = offsetX; 71 | _offsetY = offsetY; 72 | if (Width == 0) 73 | { 74 | //take the largets and make it the width 75 | Width = Text.Length; 76 | if (Options.OrderByDescending(s => s.Length).First().Length > Width) 77 | { 78 | Width = Options.OrderByDescending(s => s.Length).First().Length; 79 | } 80 | } 81 | 82 | if (IsFocused) 83 | { 84 | Console.BackgroundColor = FocusColors.Background; 85 | Console.ForegroundColor = FocusColors.Foreground; 86 | } 87 | else 88 | { 89 | Console.BackgroundColor = BackGroundColor; 90 | Console.ForegroundColor = ForeGroundColor; 91 | } 92 | 93 | //Render the collapsed state (selected value) 94 | //Make the selected item wider if it needs to 95 | string selected = $"{SelectedValue ?? Text}"; 96 | string formatedSelected = "[" + selected + new string(' ', Width - selected.Length) + "]"; 97 | Console.SetCursorPosition(offsetX + X, offsetY + Y); 98 | Console.Write(formatedSelected); 99 | 100 | // Render options if the dropdown is open 101 | if (IsOpen) 102 | { 103 | for (int i = 0; i < Options.Count; i++) 104 | { 105 | Console.SetCursorPosition(offsetX + X, offsetY + Y + i + 1); 106 | if (i == SelectedIndex) 107 | { 108 | Console.BackgroundColor = ConsoleColor.Gray; //Highlight selected option 109 | Console.ForegroundColor = ConsoleColor.Black; 110 | } 111 | else 112 | { 113 | Console.BackgroundColor = BackGroundColor; 114 | Console.ForegroundColor = ConsoleColor.White; 115 | } 116 | Console.Write(Options[i].PadRight(Width)); 117 | } 118 | } 119 | 120 | Console.ResetColor(); 121 | } 122 | /// 123 | /// Handles user input, navigation and selecting options 124 | /// 125 | public void HandleInput() 126 | { 127 | Open(); 128 | bool runFunction = true; 129 | while (runFunction) 130 | { 131 | if (Console.KeyAvailable) 132 | { 133 | ConsoleKey key = Console.ReadKey(intercept: true).Key; 134 | switch (key) 135 | { 136 | case ConsoleKey.Tab: 137 | //Move focus to the next component 138 | Close(); 139 | runFunction = false; 140 | break; 141 | case ConsoleKey.Escape: 142 | //Move close this and focus to the next component 143 | Close(); 144 | runFunction = false; 145 | break; 146 | case ConsoleKey.Enter or ConsoleKey.Spacebar: 147 | if (SelectedIndex >= 0 && SelectedIndex < Options.Count) 148 | { 149 | OnOptionSelected(Options[SelectedIndex]); 150 | } 151 | Close(); 152 | runFunction = false; 153 | break; 154 | case ConsoleKey.UpArrow: 155 | if (SelectedIndex > 0) 156 | { 157 | SelectedIndex--; 158 | Render(_offsetX, _offsetY); 159 | } 160 | break; 161 | case ConsoleKey.DownArrow: 162 | if (SelectedIndex < Options.Count - 1) 163 | { 164 | SelectedIndex++; 165 | Render(_offsetX, _offsetY); 166 | } 167 | break; 168 | } 169 | } 170 | } 171 | } 172 | /// 173 | /// Invokes the OnSelected deleate with the selected index as an int parameter. 174 | /// 175 | /// 176 | private void OnOptionSelected(string opt) 177 | { 178 | SelectedValue = opt; 179 | //call the delegate on selected with the index parameter 180 | OnSelected.DynamicInvoke(SelectedIndex); 181 | } 182 | // Focus handling methods 183 | public void OnFocus() 184 | { 185 | IsFocused = true; 186 | Render(_offsetX, _offsetY); 187 | } 188 | public void OnBlur() 189 | { 190 | IsFocused = false; 191 | //Close(); 192 | Render(_offsetX, _offsetY); 193 | } 194 | /// 195 | /// This method runs when the dropdown is closed. 196 | /// It overwrites the area occupied by the dropdown-options with white. 197 | /// 198 | /// Horizontal rendering offset 199 | /// Vertical rendering offset 200 | private void ClearOptions(int offsetX, int offsetY) 201 | { 202 | Console.BackgroundColor = ConsoleColor.White; 203 | for (int i = 0; i < Options.Count; i++) 204 | { 205 | Console.SetCursorPosition(offsetX + X, offsetY + Y + i + 1); 206 | Console.Write(new string(' ', Width)); // Overwrite the area with white spaces 207 | } 208 | Console.ResetColor(); 209 | } 210 | } 211 | } 212 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.rsuser 8 | *.suo 9 | *.user 10 | *.userosscache 11 | *.sln.docstates 12 | 13 | # User-specific files (MonoDevelop/Xamarin Studio) 14 | *.userprefs 15 | 16 | # Mono auto generated files 17 | mono_crash.* 18 | 19 | # Build results 20 | [Dd]ebug/ 21 | [Dd]ebugPublic/ 22 | [Rr]elease/ 23 | [Rr]eleases/ 24 | x64/ 25 | x86/ 26 | [Ww][Ii][Nn]32/ 27 | [Aa][Rr][Mm]/ 28 | [Aa][Rr][Mm]64/ 29 | bld/ 30 | [Bb]in/ 31 | [Oo]bj/ 32 | [Oo]ut/ 33 | [Ll]og/ 34 | [Ll]ogs/ 35 | 36 | # Visual Studio 2015/2017 cache/options directory 37 | .vs/ 38 | # Uncomment if you have tasks that create the project's static files in wwwroot 39 | #wwwroot/ 40 | 41 | # Visual Studio 2017 auto generated files 42 | Generated\ Files/ 43 | 44 | # MSTest test Results 45 | [Tt]est[Rr]esult*/ 46 | [Bb]uild[Ll]og.* 47 | 48 | # NUnit 49 | *.VisualState.xml 50 | TestResult.xml 51 | nunit-*.xml 52 | 53 | # Build Results of an ATL Project 54 | [Dd]ebugPS/ 55 | [Rr]eleasePS/ 56 | dlldata.c 57 | 58 | # Benchmark Results 59 | BenchmarkDotNet.Artifacts/ 60 | 61 | # .NET Core 62 | project.lock.json 63 | project.fragment.lock.json 64 | artifacts/ 65 | 66 | # ASP.NET Scaffolding 67 | ScaffoldingReadMe.txt 68 | 69 | # StyleCop 70 | StyleCopReport.xml 71 | 72 | # Files built by Visual Studio 73 | *_i.c 74 | *_p.c 75 | *_h.h 76 | *.ilk 77 | *.meta 78 | *.obj 79 | *.iobj 80 | *.pch 81 | *.pdb 82 | *.ipdb 83 | *.pgc 84 | *.pgd 85 | *.rsp 86 | *.sbr 87 | *.tlb 88 | *.tli 89 | *.tlh 90 | *.tmp 91 | *.tmp_proj 92 | *_wpftmp.csproj 93 | *.log 94 | *.vspscc 95 | *.vssscc 96 | .builds 97 | *.pidb 98 | *.svclog 99 | *.scc 100 | 101 | # Chutzpah Test files 102 | _Chutzpah* 103 | 104 | # Visual C++ cache files 105 | ipch/ 106 | *.aps 107 | *.ncb 108 | *.opendb 109 | *.opensdf 110 | *.sdf 111 | *.cachefile 112 | *.VC.db 113 | *.VC.VC.opendb 114 | 115 | # Visual Studio profiler 116 | *.psess 117 | *.vsp 118 | *.vspx 119 | *.sap 120 | 121 | # Visual Studio Trace Files 122 | *.e2e 123 | 124 | # TFS 2012 Local Workspace 125 | $tf/ 126 | 127 | # Guidance Automation Toolkit 128 | *.gpState 129 | 130 | # ReSharper is a .NET coding add-in 131 | _ReSharper*/ 132 | *.[Rr]e[Ss]harper 133 | *.DotSettings.user 134 | 135 | # TeamCity is a build add-in 136 | _TeamCity* 137 | 138 | # DotCover is a Code Coverage Tool 139 | *.dotCover 140 | 141 | # AxoCover is a Code Coverage Tool 142 | .axoCover/* 143 | !.axoCover/settings.json 144 | 145 | # Coverlet is a free, cross platform Code Coverage Tool 146 | coverage*.json 147 | coverage*.xml 148 | coverage*.info 149 | 150 | # Visual Studio code coverage results 151 | *.coverage 152 | *.coveragexml 153 | 154 | # NCrunch 155 | _NCrunch_* 156 | .*crunch*.local.xml 157 | nCrunchTemp_* 158 | 159 | # MightyMoose 160 | *.mm.* 161 | AutoTest.Net/ 162 | 163 | # Web workbench (sass) 164 | .sass-cache/ 165 | 166 | # Installshield output folder 167 | [Ee]xpress/ 168 | 169 | # DocProject is a documentation generator add-in 170 | DocProject/buildhelp/ 171 | DocProject/Help/*.HxT 172 | DocProject/Help/*.HxC 173 | DocProject/Help/*.hhc 174 | DocProject/Help/*.hhk 175 | DocProject/Help/*.hhp 176 | DocProject/Help/Html2 177 | DocProject/Help/html 178 | 179 | # Click-Once directory 180 | publish/ 181 | 182 | # Publish Web Output 183 | *.[Pp]ublish.xml 184 | *.azurePubxml 185 | # Note: Comment the next line if you want to checkin your web deploy settings, 186 | # but database connection strings (with potential passwords) will be unencrypted 187 | *.pubxml 188 | *.publishproj 189 | 190 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 191 | # checkin your Azure Web App publish settings, but sensitive information contained 192 | # in these scripts will be unencrypted 193 | PublishScripts/ 194 | 195 | # NuGet Packages 196 | *.nupkg 197 | # NuGet Symbol Packages 198 | *.snupkg 199 | # The packages folder can be ignored because of Package Restore 200 | **/[Pp]ackages/* 201 | # except build/, which is used as an MSBuild target. 202 | !**/[Pp]ackages/build/ 203 | # Uncomment if necessary however generally it will be regenerated when needed 204 | #!**/[Pp]ackages/repositories.config 205 | # NuGet v3's project.json files produces more ignorable files 206 | *.nuget.props 207 | *.nuget.targets 208 | 209 | # Microsoft Azure Build Output 210 | csx/ 211 | *.build.csdef 212 | 213 | # Microsoft Azure Emulator 214 | ecf/ 215 | rcf/ 216 | 217 | # Windows Store app package directories and files 218 | AppPackages/ 219 | BundleArtifacts/ 220 | Package.StoreAssociation.xml 221 | _pkginfo.txt 222 | *.appx 223 | *.appxbundle 224 | *.appxupload 225 | 226 | # Visual Studio cache files 227 | # files ending in .cache can be ignored 228 | *.[Cc]ache 229 | # but keep track of directories ending in .cache 230 | !?*.[Cc]ache/ 231 | 232 | # Others 233 | ClientBin/ 234 | ~$* 235 | *~ 236 | *.dbmdl 237 | *.dbproj.schemaview 238 | *.jfm 239 | *.pfx 240 | *.publishsettings 241 | orleans.codegen.cs 242 | 243 | # Including strong name files can present a security risk 244 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 245 | #*.snk 246 | 247 | # Since there are multiple workflows, uncomment next line to ignore bower_components 248 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 249 | #bower_components/ 250 | 251 | # RIA/Silverlight projects 252 | Generated_Code/ 253 | 254 | # Backup & report files from converting an old project file 255 | # to a newer Visual Studio version. Backup files are not needed, 256 | # because we have git ;-) 257 | _UpgradeReport_Files/ 258 | Backup*/ 259 | UpgradeLog*.XML 260 | UpgradeLog*.htm 261 | ServiceFabricBackup/ 262 | *.rptproj.bak 263 | 264 | # SQL Server files 265 | *.mdf 266 | *.ldf 267 | *.ndf 268 | 269 | # Business Intelligence projects 270 | *.rdl.data 271 | *.bim.layout 272 | *.bim_*.settings 273 | *.rptproj.rsuser 274 | *- [Bb]ackup.rdl 275 | *- [Bb]ackup ([0-9]).rdl 276 | *- [Bb]ackup ([0-9][0-9]).rdl 277 | 278 | # Microsoft Fakes 279 | FakesAssemblies/ 280 | 281 | # GhostDoc plugin setting file 282 | *.GhostDoc.xml 283 | 284 | # Node.js Tools for Visual Studio 285 | .ntvs_analysis.dat 286 | node_modules/ 287 | 288 | # Visual Studio 6 build log 289 | *.plg 290 | 291 | # Visual Studio 6 workspace options file 292 | *.opt 293 | 294 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 295 | *.vbw 296 | 297 | # Visual Studio LightSwitch build output 298 | **/*.HTMLClient/GeneratedArtifacts 299 | **/*.DesktopClient/GeneratedArtifacts 300 | **/*.DesktopClient/ModelManifest.xml 301 | **/*.Server/GeneratedArtifacts 302 | **/*.Server/ModelManifest.xml 303 | _Pvt_Extensions 304 | 305 | # Paket dependency manager 306 | .paket/paket.exe 307 | paket-files/ 308 | 309 | # FAKE - F# Make 310 | .fake/ 311 | 312 | # CodeRush personal settings 313 | .cr/personal 314 | 315 | # Python Tools for Visual Studio (PTVS) 316 | __pycache__/ 317 | *.pyc 318 | 319 | # Cake - Uncomment if you are using it 320 | # tools/** 321 | # !tools/packages.config 322 | 323 | # Tabs Studio 324 | *.tss 325 | 326 | # Telerik's JustMock configuration file 327 | *.jmconfig 328 | 329 | # BizTalk build output 330 | *.btp.cs 331 | *.btm.cs 332 | *.odx.cs 333 | *.xsd.cs 334 | 335 | # OpenCover UI analysis results 336 | OpenCover/ 337 | 338 | # Azure Stream Analytics local run output 339 | ASALocalRun/ 340 | 341 | # MSBuild Binary and Structured Log 342 | *.binlog 343 | 344 | # NVidia Nsight GPU debugger configuration file 345 | *.nvuser 346 | 347 | # MFractors (Xamarin productivity tool) working folder 348 | .mfractor/ 349 | 350 | # Local History for Visual Studio 351 | .localhistory/ 352 | 353 | # BeatPulse healthcheck temp database 354 | healthchecksdb 355 | 356 | # Backup folder for Package Reference Convert tool in Visual Studio 2017 357 | MigrationBackup/ 358 | 359 | # Ionide (cross platform F# VS Code tools) working folder 360 | .ionide/ 361 | 362 | # Fody - auto-generated XML schema 363 | FodyWeavers.xsd -------------------------------------------------------------------------------- /src/CLUI/Window.cs: -------------------------------------------------------------------------------- 1 | using CLUI.Interfaces; 2 | using Microsoft.VisualBasic; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.ComponentModel; 6 | using System.Linq; 7 | using System.Text; 8 | using System.Threading.Tasks; 9 | using IComponent = CLUI.Interfaces.IComponent; 10 | 11 | namespace CLUI 12 | { 13 | /// 14 | /// Represents a window in the console that can contain Components. 15 | /// Provides method for adding, removing and rendering components, and userinput. 16 | /// Supports windows postiononing, background color and window borders. 17 | /// 18 | /// 19 | public class Window 20 | { 21 | public int X { get; set; } 22 | public int Y { get; set; } 23 | public int Width { get; set; } 24 | public int Height { get; set; } 25 | public int BorderThickness { get; set; } = 0; 26 | public ConsoleColor BorderColor { get; set; } 27 | public ConsoleColor BackgroundColor { get; set; } = ConsoleColor.White; 28 | public Window(int x, int y, int width, int height) 29 | { 30 | X = x; 31 | Y = y; 32 | Width = width; 33 | Height = height; 34 | } 35 | public Window(int x, int y, int width, int height, ConsoleColor backgroundColor) 36 | { 37 | X = x; 38 | Y = y; 39 | Width = width; 40 | Height = height; 41 | BackgroundColor = backgroundColor; 42 | } 43 | public Window(int x, int y, int width, int height, ConsoleColor backgroundColor, ConsoleColor borderColor) 44 | { 45 | X = x; 46 | Y = y; 47 | Width = width; 48 | Height = height; 49 | BackgroundColor = backgroundColor; 50 | BorderThickness = 1; 51 | BorderColor = borderColor; 52 | } 53 | 54 | public List components = new List(); 55 | private int focusedIndex = 0; 56 | private bool runFunction = true; 57 | /// 58 | /// Add a component to the window 59 | /// 60 | /// The component to add 61 | public void AddComponent(IComponent component) 62 | { 63 | components.Add(component); 64 | } 65 | /// 66 | /// Removes a component from the window. 67 | /// 68 | /// The component to remove 69 | public void RemoveComponent(IComponent component) 70 | { 71 | components.Remove(component); 72 | } 73 | /// 74 | /// Renders the window and all of the components inside the window. 75 | /// 76 | public void Render() 77 | { 78 | //Render Window 79 | Console.BackgroundColor = BackgroundColor; 80 | for (int i = 0; i <= Width; i++) 81 | { 82 | for (int j = 0; j <= Height; j++) 83 | { 84 | Console.SetCursorPosition(X + i, Y + j); 85 | Console.Write(' '); 86 | } 87 | } 88 | if (BorderThickness > 0) 89 | { 90 | Console.BackgroundColor = BorderColor; 91 | //Vertical Borders 92 | for (int i = 0; i < Height; i++) 93 | { 94 | Console.SetCursorPosition(X, (i + Y)); 95 | Console.Write(' '); 96 | Console.SetCursorPosition(X + (Width), (i + Y)); 97 | Console.Write(' '); 98 | } 99 | //Horizontal Borders 100 | for (int i = 0; i < Width; i++) 101 | { 102 | Console.SetCursorPosition(X + (i), Y); 103 | Console.Write(' '); 104 | Console.SetCursorPosition(X + (i), Height + (Y)); 105 | Console.Write(" "); // 2 because it work 106 | } 107 | 108 | } 109 | Console.ResetColor(); 110 | 111 | 112 | //Render componets 113 | 114 | // Add the children of the Layout component 115 | List tmpComponents = new List(components); 116 | List newComponents = new List(); 117 | 118 | foreach (var component in components) 119 | { 120 | if (component is ILayout tmpLayout) 121 | { 122 | tmpComponents.Add(tmpLayout); 123 | foreach (var child in tmpLayout.Children) 124 | { 125 | tmpComponents.Add(child); 126 | } 127 | } 128 | } 129 | 130 | for (int i = 0; i < tmpComponents.Count; i++) 131 | { 132 | if (!components.Contains(tmpComponents[i]) && !newComponents.Contains(tmpComponents[i])) 133 | { 134 | newComponents.Add(tmpComponents[i]); 135 | } 136 | } 137 | 138 | // Add new components to the original list after iteration 139 | components.AddRange(newComponents); 140 | 141 | 142 | foreach (IComponent component in components) 143 | { 144 | //Render component with window postion offset 145 | component.Render(X, Y); 146 | } 147 | } 148 | /// 149 | /// Method for disposing windows 150 | /// 151 | public void Dispose() 152 | { 153 | runFunction = false; 154 | } 155 | /// 156 | /// Handels user-input to the application/window. 157 | /// 158 | public void HandleInput() 159 | { 160 | runFunction = true; 161 | while (runFunction) 162 | { 163 | if (Console.KeyAvailable) 164 | { 165 | var keyInfo = Console.ReadKey(intercept: true); 166 | var key = keyInfo.Key; 167 | var modifiers = keyInfo.Modifiers; 168 | 169 | switch (key) 170 | { 171 | case ConsoleKey.Tab: 172 | if (modifiers.HasFlag(ConsoleModifiers.Shift)) 173 | { 174 | // Handle Shift+Tab 175 | MoveFocus(-1); 176 | } 177 | else 178 | { 179 | // Handle Tab 180 | MoveFocus(1); 181 | } 182 | break; 183 | case ConsoleKey.DownArrow or ConsoleKey.RightArrow: 184 | MoveFocus(1); 185 | break; 186 | case ConsoleKey.UpArrow or ConsoleKey.LeftArrow: 187 | MoveFocus(-1); 188 | break; 189 | case ConsoleKey.Escape: 190 | runFunction = false; 191 | return; 192 | case ConsoleKey.Enter or ConsoleKey.Spacebar: 193 | HandleComponentClick(components[focusedIndex]); 194 | break; 195 | } 196 | } 197 | } 198 | } 199 | /// 200 | /// Handles component clicks 201 | /// 202 | /// The clicked component 203 | private void HandleComponentClick(IComponent component) 204 | { 205 | if (component is IInputHandler clickableInputHandler) 206 | { 207 | clickableInputHandler.HandleInput(); 208 | return; 209 | } 210 | //find component functions 211 | if (component is IClickable currenctClickable) 212 | { 213 | currenctClickable.Click.DynamicInvoke(); 214 | return; 215 | } 216 | 217 | } 218 | /// 219 | /// Moves focus to another compoent in the components list. 220 | /// Moves the focus either back or forward. 221 | /// 222 | /// Eiter -1 or +1, moves back or forward 223 | private void MoveFocus(int nextMove) 224 | { 225 | // Blur the current focusable component if applicable 226 | if (focusedIndex >= 0 && components[focusedIndex] is IFocusable currentFocusable) 227 | { 228 | currentFocusable.OnBlur(); 229 | } 230 | 231 | do 232 | { 233 | if (nextMove < 0 && focusedIndex == 0) 234 | { 235 | focusedIndex = components.Count - 1; 236 | } 237 | else if (nextMove > 0 && focusedIndex == components.Count - 1) 238 | { 239 | focusedIndex = 0; 240 | } 241 | else 242 | { 243 | focusedIndex = (focusedIndex + nextMove + components.Count) % components.Count; 244 | } 245 | } while (components[focusedIndex] is not IFocusable); 246 | 247 | // Focus the new focusable component 248 | if (components[focusedIndex] is IFocusable newFocusable) 249 | { 250 | newFocusable.OnFocus(); 251 | } 252 | } 253 | 254 | /// 255 | /// Retrive an component using the componets Id. 256 | /// 257 | /// The specifed id 258 | /// Return the compoonent with the speficied if 259 | /// No component with the specified id exits 260 | public IComponent GetComponentById(string id) 261 | { 262 | IComponent? tmpComponent = components.FirstOrDefault(c => c.Id == id); 263 | if (tmpComponent != null) 264 | { 265 | return tmpComponent; 266 | } 267 | else 268 | { 269 | throw new InvalidOperationException($"Component with ID '{id}' was not found."); 270 | } 271 | } 272 | 273 | } 274 | } 275 | --------------------------------------------------------------------------------