Skip to content

Instantly share code, notes, and snippets.

@UltraSabreman
Last active August 29, 2015 14:04
Show Gist options
  • Save UltraSabreman/782d374dc48fa0206260 to your computer and use it in GitHub Desktop.
Save UltraSabreman/782d374dc48fa0206260 to your computer and use it in GitHub Desktop.
Lets you print any number of strings with any colors in one function call. Way less cumbersome then constantly switching colors.
/// <summary>
/// Lets you print stuff. Any number of objects.
/// To change forground color, pass one console color and then another object (ie: print(ConsoleColor.Red, "this will be red")).
/// To change both fore and back colors, path two console colors then another object (ie: print(ConsoleColor.Red, ConsoleColor.White, "this will be red on white")).
/// </summary>
/// <param name="stuff">Console colors, and objects to print.</param>
public static void PrintLine() { PrintLine(null); }
public static void PrintLine(params object[] stuff) { Print(stuff); Console.WriteLine(); }
public static void Print(params object[] stuff) {
if (stuff == null) {
Console.WriteLine();
return;
}
ConsoleColor oldf = Console.ForegroundColor;
ConsoleColor oldb = Console.BackgroundColor;
var enumerator = stuff.GetEnumerator();
while (enumerator.MoveNext()) {
Object o = enumerator.Current;
if (o is ConsoleColor) {
Console.ForegroundColor = ((ConsoleColor)o);
enumerator.MoveNext();
if (enumerator.Current is ConsoleColor) {
Console.BackgroundColor = ((ConsoleColor)enumerator.Current);
enumerator.MoveNext();
}
Console.Write(enumerator.Current.ToString());
} else
Console.Write(enumerator.Current.ToString());
Console.ForegroundColor = oldf;
Console.BackgroundColor = oldb;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment