SAPTeam.CommonTK.Console 1.5.3

Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package SAPTeam.CommonTK.Console --version 1.5.3                
NuGet\Install-Package SAPTeam.CommonTK.Console -Version 1.5.3                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SAPTeam.CommonTK.Console" Version="1.5.3" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SAPTeam.CommonTK.Console --version 1.5.3                
#r "nuget: SAPTeam.CommonTK.Console, 1.5.3"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install SAPTeam.CommonTK.Console as a Cake Addin
#addin nuget:?package=SAPTeam.CommonTK.Console&version=1.5.3

// Install SAPTeam.CommonTK.Console as a Cake Tool
#tool nuget:?package=SAPTeam.CommonTK.Console&version=1.5.3                

CommonTK.Console - All in One and Multi Purpose .NET Library for Professional Console actions

Build status Nuget Nuget

This library has many utilities for doing Advanced action Directly with Console using main Win32 P/Invoke APIs. and also has various amazing features for creating beautiful console User Interface.

Installation

You can install this library with Package manager console.

SAPTeam.CommonTK.Console

PM> Install-Package SAPTeam.CommonTK.Console

CommonTK is dependency of Console library, so it will be installed automatically.

Features

Console Form

Console form is the biggest feature in this library. With ConsoleForm you can create an amazing and different UI inside the Console window!

Users can control and select items with keyboard keys. You can Notify users with a ScreenMessage toast notification! Console Forms also support Multi paging without issues. It can manage and handle many pages using a platform named Interface.

You can install the Console Form Template package with the following command to get access to the latest api templates:

dotnet new install SAPTeam.CommonTK.Console.FormTemplates
Create a new Form

In the beginning you must create a Form for defining form Title, Options and Behaviors. Here is a Example of simple Console Form:

public class ExampleForm : ConsoleForm.Form
{
    // Creates Form options. called just one time
    protected override void CreateItems()
    {
        // Create new Sections. if you dont need sections, use "" in section name.
        // Items[""] = new();

        // Sections ordered by name.
        Items["Tools"] = new();
        Items["Actions"] = new();

        // Adds Options to Specified sections.
        Items["Tools"].Add("Create a new File");
        Items["Tools"].Add("Open a File");
        Items["Tools"].Add("Open a Folder");

        Items["Actions"].Add("Close File");
        Items["Actions"].Add("Delete File");
        Items["Actions"].Add("Exit");
    }

    // Shows Title of Form. Called many times after each Refresh or during Form creation.
    protected override void OnTitle()
    {
        Utils.Echo("Example page");
        Utils.Echo("Press ESC to Close this page");

        // Because Console window is not notify program about Events, We have disabled the Close Button and users must enter ESC key to exit.

        // Write a Empty line for better look.
        Utils.Echo();
    }

    // Called just one time when Form is about to start and before reading user inputs.
    protected override void OnStart()
    {
        
    }

    // Called once before close and clearing the contents of form.
    protected override void OnClose()
    {
        
    }

    // This is the main logic Entry point of form. Called every time that a Option is selected by user.
    protected override void OnEnter(ConsoleOption option)
    {
        // This is the names that described in CreateItems().
        switch (option.Text)
        {
            // Handles functionality of option Create a new File.
            case "Create a new File":
                break;
            case "Delete File":
                // Creates and Takes Console control to the SubForm.
                Platform.AddSubForm(new DeleteFile());
                break;
            case "Exit":
                // If this form is a sub form, you must call CloseSubForm();
                Platform.Close();
                break;
            default:
                // notify user with a ScreenMessage.
                Platform.ScreenMessage("There is nothing");
                break;
        }
    }
}

// Create a SubForm.
// Sub Forms creation is same as Form but in initialization and closing is different.
public class DeleteFile : ConsoleForm.Form
{
    // Prohibit close this page with ESC key.
    public override bool IsClosable => false;

    // This Form is Always ceated as sub form.
    public DeleteFile() : base(false) { }

    // You can use all features described in Form. all this functions replaces Form functions until it closed.

    // Creates SubForm options. called during object ceation one time.
    protected override void CreateItems()
    {
        Items[""] = new() { "Yes", "No" };
    }

    protected override void OnTitle()
    {
        Utils.Echo("Delete this file?");
        Utils.Echo();
    }

    // Called when user select an option.
    protected override void OnEnter(ConsoleOption option)
    {
        // This is the names that described in CreateItems(). Yes or No.
        switch (option.Text)
        {
            case "Yes":
            case "No":
                // Return Control to the Form and Close this sub form.
                Platform.CloseSubForm();
                break;
        }
    }
}
Start Form

Console forms can be runned in any Console applications and even UI Applications using ConsoleWindow Context that described later. For Console applications simply Create an Instance of your form and call Start().

For Desktop applications you need to create a Console windows using ConsoleWindow Context:

using ConsoleWindow console = new();
var form = new ExampleForm();
form.Start();

Contexts

This library has three Context classes that can be accessed trough SAPTeam.CommonTK.Contexts.

ConsoleWindow

This context is a key utility for desktop applications, with this Context you can Allocate a new Console window and simply close it.

using (ConsoleWindow console = new())
{
    // Write your code.
    Console.WriteLine("Hello");
}
DisposableWriter

This context gives you a temporary writing session that you can write your Contents in any color that you want and in end, Simply clear exactly what you wrote.

For using this Context you must use Utils.Echo(string) to correctly write your text and record it's coordinates in context.

using (DisposableWriter dw = new(backgroundColor: ConsoleColor.White, foregroundColor: ConsoleColor.Black))
{
    // Write your texts.
    Utils.Echo("Temporary text.");
}
RedirectConsole

Main usage of this Context is in Console Forms for hiding Form creation process.

using (RedirectConsole rc = new())
{
    // Run codes that have unwanted text writings.
}

Colorize

With this struct you can format a text to have different colors in each parts of that.

Put every parts that you want to change it's color in a [] and determine colors in next parameters respectively.

// Create a Colorized string.
Colorize colorizedString = new("Welcome to [Sample App] version [1.2]", ConsoleColor.Red, ConsoleColor.Green);

// Pass this string to Utils.Echo to process and write it.
Utils.Echo(colorizedString);

// In output text all words except "Sample App" and "1.2" Follows global color theme defined in ColorSet.Current.
// First word wrote with Red color and Second one is green.

ColorSet

This is a Data-Type struct for keeping pairs of Foreground and Background colors, for example ScreenMessage uses ColorSet for setting it's color. It also used for setting Global Color Set. This color is used in every methods that defined in this library.

// Creates a new Color Set with White background and Black text color.
ColorSet colors = new(ConsoleColor.White, ConsoleColor.Black);

// Changes current default Color Set. This also changes the System.Console color set.
ColorSet.Current = colors;

// Resets Global Color Set to the default Black/Gray Back/Fore.
ColorSet.Current = new();

Console Manager

This is an Advanced utility intended for desktop Applications for get access to Console Windows using Win32 P/Invoke APIs. A simple usage of methods that provided by ConsoleManager is available with ConsoleWindow Context. but this class give you more options for creating and releasing a Console.

There is Different ways to creating Console window:

  • Allocation (Default): In this way, Library calls AllocConsole() Win32 API to Allocate a new Console window to this application.
  • AttachToParent (Unreliable): In this way Application attaches to the Console that called this application.
  • AttachProcess: Starts a new cmd.exe process, then kill it and uses Console window provided with that process.

There is also some Limitations:

  • Applications can't have more than One Console windows.
  • Closing console window also Kills application process Immediately.

For resolving second issue, we have Disabled the Close button of console window. Applications must implement a way for Calling ConsoleManager.HideConsole() after job is finished, otherwise Console is stay unresponsive. When using ConsoleWindow Context this method called automatically.

Utils

Echo

This method is the main handler of all text-related actions in this library. if you're using COntexts of Colorized Strings you must use this method for writing texts.

ClearLine

Clears Previous or Current line contents.

SetColor

Temporarily changes color of System.Console, but don't change The Global Color Set and simply can be reverted with ResetColor(). Don't confused with System.Console.ResetColor it's don't take care of Global Color Set.

Contribution

Feel free to grab the source, open issues or pull requests.

Credits

Almost all the Classes of this library were extracted from The public API of my private repository Windows Pro and published in two libraries, CommonTK and CommonTK.Console

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net6.0-windows7.0 is compatible.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 is compatible.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.47-alpha 94 4/11/2024
2.0.46-alpha 349 4/11/2024
2.0.44-alpha 224 11/8/2023
2.0.43-alpha 100 11/2/2023
2.0.42-alpha 100 11/2/2023
2.0.41-alpha 138 6/15/2023
2.0.40-alpha 123 6/10/2023
2.0.38-alpha 126 5/11/2023
2.0.37-alpha 122 5/10/2023
2.0.35-alpha 122 5/10/2023
2.0.31-alpha 118 4/23/2023
2.0.30-alpha 134 4/23/2023
2.0.29-alpha 112 4/23/2023
2.0.28-alpha 127 4/23/2023
2.0.25-alpha 116 4/23/2023
2.0.24-alpha 131 4/23/2023
2.0.23-alpha 119 4/23/2023
2.0.21-alpha 122 4/23/2023
2.0.20-alpha 125 4/23/2023
2.0.19-alpha 126 4/23/2023
2.0.18-alpha 123 4/21/2023
2.0.17-alpha 130 4/20/2023
2.0.16-alpha 121 4/20/2023
2.0.15-alpha 130 4/20/2023
2.0.12-alpha 132 4/16/2023
2.0.8-alpha 121 4/15/2023
2.0.7-alpha 123 4/15/2023
2.0.4-alpha 125 4/15/2023
2.0.1-alpha 138 4/14/2023
1.5.4 212 4/14/2023
1.5.3 193 4/6/2023
1.5.1 190 4/6/2023
1.4.6 202 4/4/2023
1.4.5 202 4/2/2023
1.4.4 240 4/1/2023
1.4.3 207 4/1/2023
1.4.2 196 4/1/2023
1.4.1 217 4/1/2023
1.4.0 200 4/1/2023
1.3.0 200 4/1/2023
1.2.11 208 3/31/2023
1.2.9 195 3/31/2023
1.2.8 183 3/31/2023
1.2.7 189 3/31/2023
1.2.6 188 3/30/2023
1.2.5 197 3/30/2023
1.2.4 195 3/30/2023
1.2.3 209 3/29/2023
1.2.2 212 3/29/2023
1.1.8 206 3/28/2023
1.1.1 232 3/28/2023
1.0.3 239 3/26/2023