Gapotchenko.FX.Console
2024.1.3
Prefix Reserved
dotnet add package Gapotchenko.FX.Console --version 2024.1.3
NuGet\Install-Package Gapotchenko.FX.Console -Version 2024.1.3
<PackageReference Include="Gapotchenko.FX.Console" Version="2024.1.3" />
paket add Gapotchenko.FX.Console --version 2024.1.3
#r "nuget: Gapotchenko.FX.Console, 2024.1.3"
// Install Gapotchenko.FX.Console as a Cake Addin #addin nuget:?package=Gapotchenko.FX.Console&version=2024.1.3 // Install Gapotchenko.FX.Console as a Cake Tool #tool nuget:?package=Gapotchenko.FX.Console&version=2024.1.3
Overview
The module provides virtual terminal functionality, console traits, and other useful primitives for .NET console apps.
Virtual Terminal
From the very beginning, computers used to employ teletypes as primary input and output devices. A teletype usually consisted of a keyboard and a printer, both placed inside a single case.
At later stages of development, teletypes were swapped with specialized computer terminals such as VT100. Those electronic devices provided not only the basic input/output capabilities, but also colors, pseudographics, and a custom control language based around a then-emerging ANSI X3.64 standard.
Unix operating systems have a built-in support for ANSI escape sequences that constitute the control language defined by the standard. Windows ignored that practice for a long time, up until Windows 10 version 1511.
What ANSI Escape Sequences Are Useful For?
Indeed, .NET base class library already provides
System.Console
class with
ForegroundColor
,
BackroundColor
and other properties for controlling the console.
ANSI escape sequences become handy when the complexity of console output reaches a certain level. Just take a look at the example below:
It would be a very involved code to render such output with a set of imperative calls.
But we can do better with ANSI escape sequences:
using Gapotchenko.FX.Console;
using System;
VirtualTerminal.EnableProcessing();
Console.WriteLine(
" \x1b[35m__ _ \x1b[34m_ _ ______ _______ \n" +
"\x1b[42;32m██████\x1b[49m \x1b[35m / _| | | \x1b[34m| \\ | | ____|__ __|\n" +
"\x1b[42;32m██\x1b[49m __ _ ___\x1b[35m| |_ _ _ ___ ___ __ _| |_ ___ _ __\x1b[34m| \\| | |__ | | \n" +
"\x1b[42;32m█████\x1b[49m / _` |_ /\x1b[35m _| | | / __|/ __/ _` | __/ _ \\| '__\x1b[34m| . ` | __| | | \n" +
"\x1b[42;32m██\x1b[49m | (_| |/ /\x1b[35m| | | |_| \\__ \\ (_| (_| | || (_) | |\x1b[34m_ | |\\ | |____ | | \n" +
"\x1b[42;32m██████\x1b[49m\\__,_/___|\x1b[35m_| \\__,_|___/\\___\\__,_/___\\___/|_(\x1b[34m_)|_| \\_|______| |_|\x1b[0m");
Please note that the implementation starts with a call to VirtualTerminal.EnableProcessing
method.
It is provided by Gapotchenko.FX.Console
module and ensures that support of ANSI escape sequences is activated for the console.
In case when the host OS does not provide a native support for them, the method switches to a virtual terminal emulation.
In this way, ANSI X3.64 control language is guaranteed to work on the widest range of host operating systems and terminals.
Console Traits
Gapotchenko.FX.Console
module provides ConsoleTraits
class that allows you to programmatically retrieve the current console capabilities.
IsColorAvailable
ConsoleTraits.IsColorAvailable
boolean property indicates whether console color output is available.
Console color is usually always available unless program standard output streams are redirected.
IsColorInhibited
ConsoleTraits.IsColorInhibited
boolean property indicates whether console color output is inhibited.
Console color may be inhibited by the host system or a user preference.
For example, a NO_COLOR
environment variable can be used to inhibit console colors as described by the corresponding specification.
IsColorEnabled
ConsoleTraits.IsColorEnabled
boolean property indicates whether console color output is enabled.
The console color is enabled when it is available and not inhibited.
The value of the property can be used by a program to automatically tune the output according to the usage context and user preference. Like so:
if (ConsoleTraits.IsColorEnabled)
{
Console.WriteLine(
" \x1b[35m__ _ \x1b[34m_ _ ______ _______ \n" +
"\x1b[42;32m██████\x1b[49m \x1b[35m / _| | | \x1b[34m| \\ | | ____|__ __|\n" +
"\x1b[42;32m██\x1b[49m __ _ ___\x1b[35m| |_ _ _ ___ ___ __ _| |_ ___ _ __\x1b[34m| \\| | |__ | | \n" +
"\x1b[42;32m█████\x1b[49m / _` |_ /\x1b[35m _| | | / __|/ __/ _` | __/ _ \\| '__\x1b[34m| . ` | __| | | \n" +
"\x1b[42;32m██\x1b[49m | (_| |/ /\x1b[35m| | | |_| \\__ \\ (_| (_| | || (_) | |\x1b[34m_ | |\\ | |____ | | \n" +
"\x1b[42;32m██████\x1b[49m\\__,_/___|\x1b[35m_| \\__,_|___/\\___\\__,_/___\\___/|_(\x1b[34m_)|_| \\_|______| |_|\x1b[0m");
}
else
{
Console.WriteLine(
@" ______ __ _ _ _ ______ _______
| ____| / _| | | | \ | | ____|__ __|
| |__ __ _ ___| |_ _ _ ___ ___ __ _| |_ ___ _ __| \| | |__ | |
| __| / _` |_ / _| | | / __|/ __/ _` | __/ _ \| '__| . ` | __| | |
| |___| (_| |/ /| | | |_| \__ \ (_| (_| | || (_) | |_ | |\ | |____ | |
|______\__,_/___|_| \__,_|___/\___\__,_/___\___/|_(_)|_| \_|______| |_|");
}
This is important in situations when the color output is not available. For example, console output redirection inhibits color by default. So the code above will produce slightly different outputs depending on whether the console output is written to the color screen or redirected to a file which has no notion of a color at all.
WillDisappearOnExit
ConsoleTraits.WillDisappearOnExit
boolean property indicates whether a console window will immediately disappear on a program exit.
Such a situation can occur when a console app is directly launched from a graphical shell.
In Windows, you can achieve that by creating a desktop shortcut to a console app and then running it.
A program can use the value of WillDisappearOnExit
property to hold the console window in a visible state so that the user could read the output.
Like so:
if (ConsoleTraits.WillDisappearOnExit)
{
Console.WriteLine();
Console.WriteLine("Press any key to exit . . .");
Console.ReadKey(true);
}
MoreTextWriter
for Paginated Output
MoreTextWriter
class from Gapotchenko.FX.Console
module automatically manages pagination when written text exceeds the height of a console area visible to the user.
The functionality is similar to more
command-line utility but can be used right within a program.
This allows you to provide an additional convenience for end users.
Let's take a look on example:
By pressing Page Down
and Down Arrow
keys it is possible to scroll the console output.
The standard Space
and Enter
are supported as well.
Below is a simple program that demonstrates the use of MoreTextWriter
:
using Gapotchenko.FX.Console;
using System;
var more = new MoreTextWriter(Console.Out);
for (int i = 1; i < 100; ++i)
more.WriteLine(i);
It produces the following output:
The keys, colors, and styling are fully customizable by deriving a class from the MoreTextWriter
.
Commonly Used Types
Gapotchenko.FX.Console.ConsoleTraits
Gapotchenko.FX.Console.VirtualTerminal
Other Modules
Let's continue with a look at some other modules provided by Gapotchenko.FX:
- Gapotchenko.FX
- Gapotchenko.FX.AppModel.Information
- Gapotchenko.FX.Collections
- ➴ Gapotchenko.FX.Console
- Gapotchenko.FX.Data
- Gapotchenko.FX.Diagnostics
- Gapotchenko.FX.IO
- Gapotchenko.FX.Linq
- Gapotchenko.FX.Math
- Gapotchenko.FX.Memory
- Gapotchenko.FX.Security.Cryptography
- Gapotchenko.FX.Text
- Gapotchenko.FX.Threading
- Gapotchenko.FX.Tuples
Or look at the full list of modules.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 is compatible. net5.0-windows was computed. net6.0 is compatible. 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. net7.0 is compatible. 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 is compatible. 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. net9.0 is compatible. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 is compatible. netcoreapp2.2 was computed. netcoreapp3.0 is compatible. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 is compatible. |
.NET Framework | net461 is compatible. net462 was computed. net463 was computed. net47 was computed. net471 is compatible. net472 is compatible. 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. |
-
.NETCoreApp 2.1
- Gapotchenko.FX (>= 2024.1.3)
- Gapotchenko.FX.Diagnostics.Process (>= 2024.1.3)
- Gapotchenko.FX.Text (>= 2024.1.3)
-
.NETCoreApp 3.0
- Gapotchenko.FX (>= 2024.1.3)
- Gapotchenko.FX.Diagnostics.Process (>= 2024.1.3)
- Gapotchenko.FX.Text (>= 2024.1.3)
-
.NETFramework 4.6.1
- Gapotchenko.FX (>= 2024.1.3)
- Gapotchenko.FX.Diagnostics.Process (>= 2024.1.3)
- Gapotchenko.FX.Text (>= 2024.1.3)
- System.Memory (>= 4.5.3)
-
.NETFramework 4.7.1
- Gapotchenko.FX (>= 2024.1.3)
- Gapotchenko.FX.Diagnostics.Process (>= 2024.1.3)
- Gapotchenko.FX.Text (>= 2024.1.3)
- System.Memory (>= 4.5.3)
-
.NETFramework 4.7.2
- Gapotchenko.FX (>= 2024.1.3)
- Gapotchenko.FX.Diagnostics.Process (>= 2024.1.3)
- Gapotchenko.FX.Text (>= 2024.1.3)
- System.Memory (>= 4.5.3)
-
.NETStandard 2.0
- Gapotchenko.FX (>= 2024.1.3)
- Gapotchenko.FX.Diagnostics.Process (>= 2024.1.3)
- Gapotchenko.FX.Text (>= 2024.1.3)
- System.Memory (>= 4.5.3)
-
.NETStandard 2.1
- Gapotchenko.FX (>= 2024.1.3)
- Gapotchenko.FX.Diagnostics.Process (>= 2024.1.3)
- Gapotchenko.FX.Text (>= 2024.1.3)
-
net5.0
- Gapotchenko.FX (>= 2024.1.3)
- Gapotchenko.FX.Diagnostics.Process (>= 2024.1.3)
- Gapotchenko.FX.Text (>= 2024.1.3)
-
net6.0
- Gapotchenko.FX (>= 2024.1.3)
- Gapotchenko.FX.Diagnostics.Process (>= 2024.1.3)
- Gapotchenko.FX.Text (>= 2024.1.3)
-
net7.0
- Gapotchenko.FX (>= 2024.1.3)
- Gapotchenko.FX.Diagnostics.Process (>= 2024.1.3)
- Gapotchenko.FX.Text (>= 2024.1.3)
-
net8.0
- Gapotchenko.FX (>= 2024.1.3)
- Gapotchenko.FX.Diagnostics.Process (>= 2024.1.3)
- Gapotchenko.FX.Text (>= 2024.1.3)
-
net9.0
- Gapotchenko.FX (>= 2024.1.3)
- Gapotchenko.FX.Diagnostics.Process (>= 2024.1.3)
- Gapotchenko.FX.Text (>= 2024.1.3)
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 |
---|---|---|
2024.1.3 | 86 | 11/10/2024 |
2022.2.7 | 486 | 5/1/2022 |
2022.2.5 | 419 | 5/1/2022 |
2022.1.4 | 450 | 4/6/2022 |
2021.2.21 | 475 | 1/21/2022 |
2021.2.20 | 426 | 1/17/2022 |
2021.1.5 | 380 | 7/6/2021 |
2020.2.2-beta | 324 | 11/21/2020 |
2020.1.15 | 457 | 11/5/2020 |
2020.1.9-beta | 332 | 7/14/2020 |
2020.1.8-beta | 313 | 7/14/2020 |
2020.1.7-beta | 339 | 7/14/2020 |