-
Notifications
You must be signed in to change notification settings - Fork 0
1. Basic Outputs
The main entry points to outputs are Write
and WriteLine
.
They take one or multiple parameters of type ColoredOutput
which enables to customize each string.
A ColoredOutput
is a wrapper around a string
that also stored 2 ConsoleColor
s, one for foreground, and one for background.
It can be created using extension methods or the constructor, but the best way to use it is with implicit converters.
By default any string can be implicitly converter to a ColoredOutput
with the default colors of System.Console
, but to further ease the usage, another type is used: Color
Color
is a very simple type, which is a very simple wrapper around ConsoleColor
and is implicitly converter to ConsoleColor
, each ConsoleColor
value has a corresponding static field in Color
, i.e Color.Red
. Color
was required to allow using special operators to define the ColoredOutput
. But also as benefit it shorten the amount of code needed to be written.
Now OutputColor
can be represented as mathematical equation in which string * foreground / background
results in the string wrapped as a ColoredOutput
with a selected foreground and background. It requires using the Color
fields for this to work properly. Some examples:
WriteLine("Test" * Color.Red / Color.Blue); // Red foreground and blue background
WriteLine("Test" * Color.Red); // Red foreground and default background (black)
WriteLine("Test" / Color.Blue); // Blue background and default foreground (gray)
So you can see that each part of the equation (aside from the string) can be eliminated to use the defaults. This enables very fluid and intuitive usage, but one with minimal performance overhead.
Write
and WriteLine
have overloads of with up to 6 ColoredOutput
parameters, before defaulting to the params
overload, since ColoredOutput
is a struct, this enables most of not all usual usage scenarios to avoid allocating an array on the heap. Alternatively, perhaps you could optimize by using a ReadOnlySpan<ColoredOutput>
overload, which the params
one is also using internally.