{"id":20258785,"url":"https://github.com/tareqimbasher/konsole","last_synced_at":"2025-04-11T01:12:57.149Z","repository":{"id":105885022,"uuid":"362944727","full_name":"tareqimbasher/konsole","owner":"tareqimbasher","description":"A light-weight System.Console utility wrapper for .NET","archived":false,"fork":false,"pushed_at":"2024-01-16T20:47:13.000Z","size":239,"stargazers_count":6,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-11T01:12:51.477Z","etag":null,"topics":["colors","console","csharp","dotnet","formatting","progress-bar","prompt","utility"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tareqimbasher.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-04-29T20:53:09.000Z","updated_at":"2025-01-13T09:00:55.000Z","dependencies_parsed_at":"2024-11-14T11:11:37.017Z","dependency_job_id":"12c1a667-d8fb-4aac-b589-70f9e83de2df","html_url":"https://github.com/tareqimbasher/konsole","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tareqimbasher%2Fkonsole","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tareqimbasher%2Fkonsole/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tareqimbasher%2Fkonsole/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tareqimbasher%2Fkonsole/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tareqimbasher","download_url":"https://codeload.github.com/tareqimbasher/konsole/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248322571,"owners_count":21084337,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["colors","console","csharp","dotnet","formatting","progress-bar","prompt","utility"],"created_at":"2024-11-14T11:11:30.120Z","updated_at":"2025-04-11T01:12:57.125Z","avatar_url":"https://github.com/tareqimbasher.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# \u003cimg align=\"left\" width=\"48\" height=\"48\" src=\"https://github.com/tareqimbasher/konsole/blob/main/docs/icon-128.png?raw=true\"\u003e Konsole\n\n[![dev](https://github.com/tareqimbasher/konsole/actions/workflows/dev.yml/badge.svg)](https://github.com/tareqimbasher/konsole/actions/workflows/dev.yml)\n\nA light-weight System.Console utility wrapper for .NET with lots of useful and \ncommon extensions.\n\n## Install\n\nPackage Manager\n\n```\nInstall-Package Konsole\n```\n\n.NET CLI\n\n```\ndotnet add package Konsole\n```\n\n\n## Why?\nWhen writing applications geared for use with the console, I often end up rolling my own \nhelpers for formatting output, getting user input..etc. This library aims to provide a \ngood collection of utilities and extensions to save you from having to do it every time.\n\n\n## Usage\n\nYou can use the built-in `Konsole` class to quickly get up and running:\n\n```csharp\nvar console = new Konsole();\n```\n\nIt should be a drop-in replacement for `System.Console`\n\n```csharp\nconsole.WriteLine(\"Lorem ipsum\");\n```\n\n\n#### Defaults\n\nYou can change the defaults for an `IKonsole` instance. Below are the defaults out of the box:\n\n```csharp\nconsole.Defaults.ForegroundColor = Console.ForegroundColor;\nconsole.Defaults.BackgroundColor = Console.BackgroundColor;\n\nconsole.Defaults.Info = (console, text) =\u003e console.WriteLine(text, ConsoleColor.White);\nconsole.Defaults.Debug = (console, text) =\u003e console.WriteLine(text, ConsoleColor.Green);\nconsole.Defaults.Warn = (console, text) =\u003e console.WriteLine(text, ConsoleColor.Yellow);\nconsole.Defaults.Error = (console, text) =\u003e console.WriteLine(text, ConsoleColor.Red);\n\nconsole.Defaults.PostWriteAction = (console, text) =\u003e { }; // Gets called after every write to the console\n```\n\n#### Chaining\nAll methods are chainable.\n\n```csharp\nconsole.WriteLine(\"Header\")\n    .WriteDivider()\n    .Write(\"Status: \").WriteLine(\"Loaded\");\n```\n\n\n#### Coloring Output\n\n`Write()` and `WriteLine()` have overrides to specify foreground and background colors.\n\n```csharp\nconsole.Write(\"Status: \", ConsoleColor.Green).WriteLine(\"Loaded\");\n```\n\nYou can set the current foreground and background colors.\n\n```csharp\nconsole.ForegroundColor = ConsoleColor.Green;\nconsole.BackgroundColor = ConsoleColor.Black;\n```\n\nUse `ResetColors()` to reset the current foreground and background colors as defined in `console.Defaults`\n\n```csharp\nconsole.ResetColors();\n```\n\n#### Formatting Output\n\n```csharp\nconsole.Clear();\nconsole.ClearCurrentLine();\nconsole.ReplaceCurrentLine(\"New text\");\nconsole.WriteDivider();\nconsole.List(\"Vanilla\", \"Chocolate\", \"Strawberry\");\nconsole.OrderedList(\"Vanilla\", \"Chocolate\", \"Strawberry\");\nconsole.WriteLineAlignCenter(\"Center aligned text\");\nconsole.WriteLineAlignRight(\"Right aligned text\");\nconsole.Debug(\"Debug message\");\nconsole.Info(\"Info message\");\nconsole.Warn(\"Warning message\");\nconsole.Error(\"Error message\");\n```\n\n\n#### Scoping\nUse `WithForeColor()`, `WithBackColor()`, and `WithColors()` to apply colors within a scope without changing the \ndefault colors of the `IKonsole` instance you're using.\n\n```csharp\nconsole.WriteLine(\"Default color text\")\n    .WithForeColor(ConsoleColor.Green).WriteLine(\"Green text\");\n\nconsole.WriteLine(\"Still uses default color text\");\n```\n\n\n#### Prompts\n\nUse the `Ask()` method to get input from the user. Use the `Ask\u003cT\u003e` overload to convert user input into the specified \ndata type.\n\n```csharp\nstring name = console.Ask(\"What is your name?\");\nDateTime birthDate = console.Ask\u003cDateTime\u003e(\"What is your birth date?\");\nint power = console.Ask\u003cint\u003e(\"What is Goku's power level?\");\nvar favs = console.Ask(\"What are your favorite programming languages:\",\n    new[] { \"C#\", \"JavaScript\", \"Python\", \"COBOL\", \"Ruby\" });\n```\n\n\u003e :bulb: Create a custom `TypeConverter` to convert user input to a non-primitive data type. \nInternally, this method uses `TypeConverter.ConvertFromInvariantString(input)` to do the conversion.\n\nUse the `Confirm()` method to get a yes/no answer from the user.\n\n```csharp\nbool hasPets = console.Confirm(\"Do you have any pets?\");\nbool proceed = console.Confirm(\"Do you want to continue?\", defaultAnswer: false);\n```\n\n#### Progress Bars\n\nA single progress bar\n\n```csharp\nvar progressBar = console.ProgressBar(\"Loading file\");\nprogressBar.Update(30); // Set progress to 30%\n```\n\nMultiple progress bars can working simultaneously when added to a group\n\n```csharp\nvar group = console.ProgressBarGroup();\nvar progressBar1 = group.ProgressBar(\"File 1\");\nvar progressBar2 = group.ProgressBar(\"File 2\");\n\nprogressBar1.Update(50);\nprogressBar2.Update(35);\n```\n\n## Extending the Konsole\n\nYou can create your own implementation of the `IKonsole` interface, or inherit\nfrom `Konsole` and override the members you need:\n\n```csharp\npublic class MyConsole : Konsole\n{\n    public override IKonsole Write(string text)\n    {\n        return base.Write($\"My app says: {text}\");\n    }\n}\n```\n\n\u003cbr/\u003e\n\n## Code Samples\n\nCheckout the `Konsole.Samples` project for a working example.\n\n#### Output Formatting\n\n```csharp\nIKonsole console = new Konsole();\n\n// Sample 1\nconsole.WriteLine(\"Sample 1: The Basics\").WriteDivider();\nconsole.WriteLine(\"1. Text with default colors.\");\nconsole.WithForeColor(ConsoleColor.Cyan).WriteLine(\"2. Cyan text on the default background\");\nconsole.WithBackColor(ConsoleColor.Blue).WriteLine(\"3. Default text on a blue background\");\nconsole.WithForeColor(ConsoleColor.Black).WithBackColor(ConsoleColor.Red).WriteLine(\"4. Black text on a red background\");\n\nconsole.ForegroundColor = ConsoleColor.Red;\nconsole.BackgroundColor = ConsoleColor.Yellow;\n\nconsole.WriteLine(\"5. Text color is now red and background is now yellow\");\nconsole.WithForeColor(ConsoleColor.Green).WriteLine(\"6. Green text with a yellow background\");\n\n\n// Sample 2\nconsole.WriteLine().ResetColors().WriteLine(\"Sample 2: Ice Cream Menu\");\nconsole\n    .WithForeColor(ConsoleColor.Green)\n    .WriteDivider()\n    .WriteLine(\"Sunny's Ice Cream Shop\")\n    .WriteDivider()\n    .WithForeColor(ConsoleColor.Cyan).WriteLine(\"These are your options:\")\n    .WithForeColor(ConsoleColor.White).WriteLine(\"   1. Vanilla\")\n    .WithForeColor(ConsoleColor.DarkMagenta).WriteLine(\"   2. Chocolate\")\n    .WithForeColor(ConsoleColor.Red).WriteLine(\"   3. Strawberry\");\n\n\n// Sample 3\nconsole.WriteLine().WriteLine(\"Sample 3: Lists\").WriteDivider();\nconsole.WriteLine(\"Ice Cream Flavors\").List(\"Vanilla\", \"Chocolate\", \"Strawberry\");\nconsole.WriteLine(\"Toppings\").OrderedList(\"Sprinkles\", \"Strawberries\", \"Syrup\");\n\n\n// Sample 4\nconsole.WriteLine().WriteLine(\"Sample 4: Text Alignment\").WriteDivider();\nconsole.WriteLine(\"This text is left aligned\");\nconsole.WriteLineAlignCenter(\"This text is center aligned\");\nconsole.WriteLineAlignRight(\"This text is right aligned\");\nconsole.Write(\"Left aligned\").WriteLineAlignRight(\"Right aligned\");\n```\n\nThis results in the following output:\n\n![preview-output-formatting](https://github.com/tareqimbasher/konsole/blob/main/docs/preview-output-formatting.png?raw=true)\n\n#### Prompts\n\n```csharp\nstring name = console.Ask(\"What is your name?\");\nDateTime birthDate = console.Ask\u003cDateTime\u003e(\"What is your birth date?\");\nbool hasPets = console.Confirm(\"Do you have any pets?\");\nint power = console.Ask\u003cint\u003e(\"What is Goku's power level?\");\nvar favs = console.Ask(\"What are your favorite programming languages:\", new[] { \"C#\", \"JavaScript\", \"Python\", \"COBOL\", \"Ruby\" });\n\nconsole\n    .WithForeColor(ConsoleColor.Cyan).WriteLine(\"\\nProfile\").WriteDivider().ResetColors()\n    .WriteLine($\"{\"Name:\",-20} {name}\")\n    .WriteLine($\"{\"Age:\",-20} {Math.Floor((DateTime.Today - birthDate.Date).TotalDays / 365)}\")\n    .WriteLine($\"{\"Pets:\",-20} {(hasPets ? \"Yes\" : \"No\")}\")\n    .WriteLine($\"{\"DBZ Fan?:\",-20} {(power \u003e 9000 ? \"Yep!\" : \"No :(\")}\")\n    .WriteLine(\"Favorite Languages:\").List(favs);\n```\n\nThis results in the following output:\n\n![preview-prompts](https://github.com/tareqimbasher/konsole/blob/main/docs/preview-prompts.png?raw=true)\n\n\n#### Progress Bars\n\n##### Single Progress Bar\n\n```csharp\nvar progressBar = console.ProgressBar(\"Loading file\");\n\nfor (int i = 1; i \u003c= 10; i++)\n{\n    progressBar.Update(i * 10);\n}\n\nconsole.Info(\"Loading complete.\");\n```\n\n##### Progress Bar Group\nMultiple progress bars running asynchronously.\n\n```csharp\nprivate void Process()\n{\n    console.WriteLine(\"\\nProgress Bars\").WriteDivider();\n    var tasks = new List\u003cTask\u003e();\n\n    var group = console.WithForeColor(ConsoleColor.DarkCyan).ProgressBarGroup();\n            \n    foreach (var i in Enumerable.Range(1, 5))\n    {\n        var progressBar = group.ProgressBar($\"Async Operation {i}\");\n        tasks.Add(new Task(() =\u003e Work(progressBar, 50 + (i * 50) + (i % 2 * 70))));\n    }\n\n    tasks.ForEach(t =\u003e t.Start());\n    Task.WaitAll(tasks.ToArray());\n\n    console.WriteLine().Info(\"Loading is complete!\").WriteLine();\n}\n\n\nprivate void Work(ProgressBar progressBar, int progressDuration)\n{\n    var text = progressBar.Text;\n    for (int i = 0; i \u003c 10; i++)\n    {\n        progressBar.Update((i + 1) * 10, $\"{text} | Item {i + 1} of 10\");\n        Thread.Sleep(progressDuration);\n    };\n}\n\n```\n\nThis results in the following output:\n\n![preview-progressbars](https://github.com/tareqimbasher/konsole/blob/main/docs/preview-progressbars.gif?raw=true)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftareqimbasher%2Fkonsole","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftareqimbasher%2Fkonsole","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftareqimbasher%2Fkonsole/lists"}