Easy terminal styling for .NET.
This project is largely inspired by Chalk and the API is very similar. Currently the only features missing are support for colorspaces beyond RGB, this may change in future.
- Chainable and fluent API.
- Support for regular styling by
System.Console
, in addition to ANSI 16 colors, ANSI 256 colors and ANSI true colors. - Auto-detection of color support.
- Minimal and focused.
- .NET Standard 2.1
Install-Package SJP.Fabulous
or
dotnet add package SJP.Fabulous
Fabulous
.Green
.Text("Hello, world!")
.WriteLine();
Fabulous has two easy ways to create styled text. The previous method shows one way, the other is to initialize from a string
object.
FabulousText message = "Hello, world!";
message.WriteLine();
You can also combine text with different styling together (including regular string
objects) and write in one go.
var hello = Fabulous.Green.Text("Hello");
var comma = ", ";
var world = Fabulous.Yellow.Text("world!");
var message = hello + comma + world;
message.WriteLine();
You can also use Fabulous.WriteLine()
for writing instead of the fluent approach.
var message = Fabulous.Yellow.Text("Hello, world!");
Fabulous.WriteLine(message);
You can also write with format strings for compatibility with the Console.Write()
and Console.WriteLine()
methods.
var helloMessage = Fabulous.BgWhite.Black.Text("Hello, {0}!");
var name = "Simon";
helloMessage.WriteLine(name); // writes "Hello, Simon!"
Reset
Bold
Dim
Italic
Underline
Hidden
Strikethrough
Black
Red
Green
Yellow
Blue
Magenta
Cyan
White
Grey
orGray
RedBright
GreenBright
YellowBright
BlueBright
MagentaBright
CyanBright
WhiteBright
BgBlack
BgRed
BgGreen
BgYellow
BgBlue
BgMagenta
BgCyan
BgWhite
BgGrey
orBgGray
BgRedBright
BgGreenBright
BgYellowBright
BgBlueBright
BgMagentaBright
BgCyanBright
BgWhiteBright
Example: FabulousConsole.ColorLevel = ConsoleColorMode.Basic;
Setting the ColorLevel
property determines the level of coloring and styling that the console will use. By default this is set to ConsoleColorMode.Standard
, which means that the styling is limited to the feature set that System.Console
provides.
To use the maximum supported color level, you can use the following code:
FabulousConsole.ColorLevel = FabulousConsole.GetMaximumSupportedColorMode();
If you are unsure of which platform your code is currently running in, you can check this property. This is important because cmd.exe
is fairly limited in the amount of styling it can provide, at least on systems pre-dating some versions of Windows 10.
Determines the greatest level of support that the current Windows console can provide. This may turn on ANSI processing for your console instance if it is not already available. This property is available for use, but it is usually not necessary. For most situations it is sufficient to use FabulousConsole.GetMaximumSupportedColorMode()
instead.
Fabulous supports 256 colors and Truecolor (16 million colors) on terminal emulators which support it.
For terminals which do not have support for the chosen color styling, the colors are downsampled to the closest matching color. For 256 color terminals this will likely not matter much, but for 16 colors the effect will be noticeable.
There are a few ways to declare foreground colors in addition to the styling properties listed above (e.g. White
, Green
, Cyan
, etc).
Examples:
Hex("#DEADED")
Rgb(15, 100, 204)
Keyword("orangered")
Keyword(ColorKeyword.OrangeRed)
For background colors, the same methods can be used, but must be prefixed with Bg
instead.
BgHex("#DEADED")
BgRgb(15, 100, 204)
BgKeyword("orangered")
BgKeyword(ColorKeyword.OrangeRed)
Using this API you can easily create helper methods for common scenarios.
public static void WriteSuccessLine(string message)
{
var successMessage = Fabulous.Green.Text("[SUCCESS]") + " " + message;
successMessage.WriteLine();
}
public static void WriteWarningLine(string message)
{
var warningMessage = Fabulous.Yellow.Text("[WARNING]") + " " + message;
warningMessage.WriteLine();
}
public static void WriteErrorLine(string message)
{
var errorMessage = Fabulous.Red.Text("[ERROR]") + " " + message;
errorMessage.WriteLine();
}
When using Windows, using a more sophisticated terminal emulator than cmd.exe
will give you greater access to styling than basic coloring. Try Windows Terminal, cmder, or ConEmu.