Last active
February 25, 2022 23:19
-
-
Save rgregg/219aba24270091928fb5 to your computer and use it in GitHub Desktop.
FancyConsole
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.IO; | |
public class FancyConsole | |
{ | |
private static string _logFileName; | |
private static StreamWriter _logWriter; | |
public static bool WriteVerboseOutput { get; set; } | |
public static string LogFileName | |
{ | |
get { return _logFileName; } | |
set | |
{ | |
_logFileName = value; | |
if (!string.IsNullOrEmpty(_logFileName)) | |
{ | |
_logWriter = new StreamWriter(_logFileName, false); | |
_logWriter.AutoFlush = true; | |
} | |
else | |
{ | |
_logWriter = null; | |
} | |
} | |
} | |
public static void Write(string output) | |
{ | |
if (null != _logWriter) _logWriter.Write(output); | |
Console.Write(output); | |
} | |
public static void Write(ConsoleColor color, string output) | |
{ | |
if (null != _logWriter) _logWriter.Write(output); | |
Console.ForegroundColor = color; | |
Console.Write(output); | |
Console.ResetColor(); | |
} | |
public static void Write(string format, params object[] values) | |
{ | |
if (null != _logWriter) _logWriter.Write(format, values); | |
Console.Write(format, values); | |
} | |
public static void Write(ConsoleColor color, string format, params object[] values) | |
{ | |
if (null != _logWriter) _logWriter.Write(format, values); | |
Console.ForegroundColor = color; | |
Console.Write(format, values); | |
Console.ResetColor(); | |
} | |
public static void WriteLine() | |
{ | |
if (null != _logWriter) _logWriter.WriteLine(); | |
Console.WriteLine(); | |
} | |
public static void WriteLine(string format, params object[] values) | |
{ | |
if (null != _logWriter) _logWriter.WriteLine(format, values); | |
Console.WriteLine(format, values); | |
} | |
public static void WriteLine(string output) | |
{ | |
if (null != _logWriter) _logWriter.WriteLine(output); | |
Console.WriteLine(output); | |
} | |
public static void WriteLine(ConsoleColor color, string format, params object[] values) | |
{ | |
if (null != _logWriter) _logWriter.WriteLine(format, values); | |
Console.ForegroundColor = color; | |
Console.WriteLine(format, values); | |
Console.ResetColor(); | |
} | |
public static void WriteLineIndented(string indentString, ConsoleColor color, string output) | |
{ | |
Console.ForegroundColor = color; | |
WriteLineIndented(indentString, output); | |
Console.ResetColor(); | |
} | |
public static void WriteLineIndented(string indentString, ConsoleColor color, string format, params object[] values) | |
{ | |
Console.ForegroundColor = color; | |
WriteLineIndented(indentString, format, values); | |
Console.ResetColor(); | |
} | |
public static void WriteLineIndented(string indentString, string format, params object[] values) | |
{ | |
using (System.IO.StringReader reader = new System.IO.StringReader(string.Format(format, values))) | |
{ | |
string line; | |
while ((line = reader.ReadLine()) != null) | |
{ | |
WriteLine(string.Concat(indentString, line)); | |
} | |
} | |
} | |
public static void WriteLineIndented(string indentString, string output) | |
{ | |
using (System.IO.StringReader reader = new System.IO.StringReader(output)) | |
{ | |
string line; | |
while ((line = reader.ReadLine()) != null) | |
{ | |
WriteLine(string.Concat(indentString, line)); | |
} | |
} | |
} | |
public static void VerboseWriteLine() | |
{ | |
if (WriteVerboseOutput) | |
{ | |
WriteLine(); | |
} | |
} | |
public static void VerboseWriteLine(string output) | |
{ | |
if (WriteVerboseOutput) | |
{ | |
WriteLine(output); | |
} | |
} | |
public static void VerboseWriteLine(string format, params object[] parameters) | |
{ | |
if (WriteVerboseOutput) | |
{ | |
WriteLine(format, parameters); | |
} | |
} | |
public static void VerboseWriteLineIndented(string indent, string output) | |
{ | |
if (WriteVerboseOutput) | |
{ | |
WriteLineIndented(indent, output); | |
} | |
} | |
public static void VerboseWriteLineIndented(string indent, string format, params object[] parameters) | |
{ | |
if (WriteVerboseOutput) | |
{ | |
WriteLineIndented(indent, format, parameters); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment