Created
May 9, 2018 04:01
-
-
Save vankessel/97aaafc590c66c73378332ca75b4319b 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
#!/usr/bin/env python3 | |
class Color: | |
color_code = None | |
reset = '\033[0m' | |
_color_code_stack = [reset] | |
def __init__(self): | |
pass | |
def __enter__(self): | |
Color._color_code_stack.append(self.color_code) | |
print(Color._color_code_stack[-1], end='') | |
def __exit__(self, type, value, traceback): | |
Color._color_code_stack.pop() | |
print(Color._color_code_stack[-1], end='') | |
class Red(Color): color_code = '\033[31m' | |
class Green(Color): color_code = '\033[32m' | |
class Blue(Color): color_code = '\033[34m' | |
if __name__ == '__main__': | |
print("Normal") | |
with Red(): | |
print('Red!') | |
with Green(): | |
print('Green!') | |
with Blue(): | |
print('Blue!') | |
print('Green again!') | |
print('Red again!') | |
print('Normal again!') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment