Created
March 23, 2021 03:20
-
-
Save delannoy/08ae77ed97eb7798ec46c3d7010522e0 to your computer and use it in GitHub Desktop.
Python color with ANSI escape sequences (via tput)
This file contains hidden or 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 C: | |
| # [https://stackoverflow.com/a/287944/13019084] | |
| # [https://janakiev.com/blog/python-shell-commands/] | |
| # [https://www.gnu.org/software/termutils/manual/termutils-2.0/html_chapter/tput_1.html] | |
| reset = os.popen('tput sgr 0 0').read() # Turn off all attributes | |
| bold = os.popen('tput bold').read() # Begin double intensity mode | |
| uline = os.popen('tput smul').read() # Begin underscore mode | |
| class F: # foreground | |
| black, red, green, yellow, blue, magenta, cyan, white = [os.popen(f'tput setaf {c}').read() for c in range(0,8)] | |
| grey, lred, lgreen, lyellow, lblue, lmagenta, lcyan, lwhite = [os.popen(f'tput setaf {c}').read() for c in range(8,16)] | |
| class B: # background | |
| black, red, green, yellow, blue, magenta, cyan, white = [os.popen(f'tput setab {c}').read() for c in range(0,8)] | |
| grey, lred, lgreen, lyellow, lblue, lmagenta, lcyan, lwhite = [os.popen(f'tput setab {c}').read() for c in range(8,16)] | |
| # >>> print(f'{C.F.red}red foreground{C.reset}') | |
| # >>> print(f'{C.F.black}{C.B.lblue}black foreground on light blue background{C.reset}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment