Skip to content

Instantly share code, notes, and snippets.

@vankessel
Created May 9, 2018 04:01
Show Gist options
  • Save vankessel/97aaafc590c66c73378332ca75b4319b to your computer and use it in GitHub Desktop.
Save vankessel/97aaafc590c66c73378332ca75b4319b to your computer and use it in GitHub Desktop.
#!/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