Skip to content

Instantly share code, notes, and snippets.

@adrian-the-git
adrian-the-git / cprint.py
Last active August 20, 2020 07:42
Cross-platform colored text for python3
# If you can write a simpler but still *readable* cross-platform colored text print function please comment!
try:
from ctypes import windll
BLUE, GREEN, CYAN, RED, MAGENTA, YELLOW, GRAY, BOLD, RESET = [*range(1, 9), 7]
set_color = (lambda a, b, f=windll.kernel32.SetConsoleTextAttribute,
s=windll.kernel32.GetStdHandle(-11): f(s, a | b))
except ImportError:
RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, GRAY, BOLD, RESET = [*range(31, 38), 1, 0]

Keybase proof

I hereby claim:

  • I am adrian-the-git on github.
  • I am adriancarpenter (https://keybase.io/adriancarpenter) on keybase.
  • I have a public key whose fingerprint is 5590 0259 8B73 97C9 2099 3FFD C554 970F 409D 496E

To claim this, I am signing this object: