Created
December 7, 2016 17:28
-
-
Save jsmarble/b12f8e9c8454588fec98d67688867f82 to your computer and use it in GitHub Desktop.
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
public class ConsoleColorContext : IDisposable | |
{ | |
private ConsoleColor backupBackgroundColor; | |
private ConsoleColor backupForegroundColor; | |
private ConsoleColorContext() | |
{ | |
this.backupForegroundColor = Console.ForegroundColor; | |
this.backupBackgroundColor = Console.BackgroundColor; | |
} | |
public ConsoleColorContext WithForeground(ConsoleColor foregroundColor) | |
{ | |
Console.ForegroundColor = foregroundColor; | |
return this; | |
} | |
public ConsoleColorContext WithBackground(ConsoleColor backgroundColor) | |
{ | |
Console.BackgroundColor = backgroundColor; | |
return this; | |
} | |
public ConsoleColorContext RestoreBackgroundTo(ConsoleColor backgroundColor) | |
{ | |
this.backupBackgroundColor = backgroundColor; | |
return this; | |
} | |
public ConsoleColorContext RestoreForegroundTo(ConsoleColor foregroundColor) | |
{ | |
this.backupForegroundColor = foregroundColor; | |
return this; | |
} | |
public void Dispose() | |
{ | |
Console.ForegroundColor = backupForegroundColor; | |
Console.BackgroundColor = backupBackgroundColor; | |
} | |
public static ConsoleColorContext Create() | |
{ | |
return new ConsoleColorContext(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment