Skip to content

Instantly share code, notes, and snippets.

@NMyVision
Last active February 12, 2021 21:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NMyVision/05a78d599e34814924c740432f545209 to your computer and use it in GitHub Desktop.
Save NMyVision/05a78d599e34814924c740432f545209 to your computer and use it in GitHub Desktop.
LINQPad Console.Writeline and Console.Write with foreground color support.
void Main()
{
PlainMain();
Util.RawHtml("<hr/>").Dump();
ColorMain();
}
void PlainMain()
{
Console.WriteLine("Line");
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Heading - default");
Console.WriteLine("Heading - override", "font-weight: bold; color: green");
Console.WriteLine("Line");
Console.ResetColor();
Console.Write("Text B");
Console.Write("[ Black ]");
Console.ForegroundColor = ConsoleColor.Magenta;
Console.Write("[ Magenta ]");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.BackgroundColor = ConsoleColor.Blue;
Console.Write("[ DarkMagenta ]");
Console.WriteLine("-");
Console.ResetColor();
Console.Write("End");
}
void ColorMain()
{
ColorConsole Console = new ColorConsole();
Console.WriteLine("Line");
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine( "Heading - default" );
Console.WriteLine( "Heading - override" , "font-weight: bold; color: green" );
Console.WriteLine("Line");
Console.ResetColor();
Console.Write("[ Black ]");
Console.ForegroundColor = ConsoleColor.Magenta;
Console.Write("[ Magenta ]");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.BackgroundColor = ConsoleColor.Blue;
Console.Write("[ DarkMagenta ]");
Console.WriteLine("-"); //
Console.ResetColor();
Console.Write("End");
Console.WriteLine();
Console.WriteLine("With CSS" , "border: solid 1px #efefef; background: whitesmoke; padding: 10px; margin-top: 5px; display: block");
}
#region ColorConsole
public class ColorConsole
{
Queue q = new Queue();
~ColorConsole()
{
DumpQueue();
q = null;
}
public System.ConsoleColor ForegroundColor { get; set; } = ConsoleColor.Black;
public System.ConsoleColor BackgroundColor { get; set; } = ConsoleColor.White;
public void WriteLine() => WriteLine("");
public void WriteLine<T>(T value) => WriteLine(value, ColorStyle );
public void WriteLine<T>(T value, string style)
{
Write(value, style);
DumpQueue();
}
public void Write() => Write("");
public void Write<T>(T value) => Write(value , ColorStyle );
public void Write<T>(T value, string style) => q.Enqueue(Util.WithStyle(value.ToString(), style ));
public void ResetColor()
{
DumpQueue();
ForegroundColor = ConsoleColor.Black;
BackgroundColor = ConsoleColor.White;
}
string ColorStyle => $"color: { ForegroundColor }; background-color: { BackgroundColor }; display: block; ";
void DumpQueue()
{
if (q.Count > 0)
{
Util.HorizontalRun(false,q.ToArray()).Dump();
}
q.Clear();
}
}
#endregion
@amaillo-mc
Copy link

Pretty cool implementation!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment