Skip to content

Instantly share code, notes, and snippets.

@rldotai
Created February 6, 2021 03:51
Show Gist options
  • Save rldotai/4ced459e15689d6225e91b5833e3d0af to your computer and use it in GitHub Desktop.
Save rldotai/4ced459e15689d6225e91b5833e3d0af to your computer and use it in GitHub Desktop.
Demo of colorama's different output stylings
from colorama import Fore, Back, Style
foregrounds_dark = {
i: Fore.__dict__[i]
for i in dir(Fore)
if not (i.startswith("_") or i.startswith("LIGHT") or i.startswith("RESET"))
}
foregrounds_light = {
i: Fore.__dict__[i]
for i in dir(Fore)
if i.startswith("LIGHT") and not (i.startswith("_") or i.startswith("RESET"))
}
backgrounds_dark = {
i: Back.__dict__[i]
for i in dir(Fore)
if not (i.startswith("_") or i.startswith("RESET") or i.startswith("LIGHT"))
}
backgrounds_light = {
i: Back.__dict__[i]
for i in dir(Fore)
if i.startswith("LIGHT") and not (i.startswith("_") or i.startswith("RESET"))
}
# Combined backgrounds
backgrounds = {**backgrounds_dark, **backgrounds_light}
styles = {
"DIM": Style.DIM,
"NORMAL": Style.NORMAL,
"BRIGHT": Style.BRIGHT,
}
for sn, sc in styles.items():
print(" " * 17 + "-" * 80)
print(" " * 18 + f"{sn:^82}")
print(" " * 17 + "-" * 80)
# Testing dark foregrounds
colnames = "".join(["{:^10.10s}".format(k) for k in foregrounds_dark])
teststring = "foo 123"
rowtext = "".join(
[f"{fc} {sc} {teststring:8s}" for fg, fc in foregrounds_dark.items()]
)
print(" " * 18 + colnames)
print(Style.RESET_ALL)
for bg, bc in backgrounds.items():
print(f"{bg:<16s} {bc} {rowtext} {Style.RESET_ALL}")
print()
# Test light foreground text
colnames = "".join(["{:^10.8s}".format(k) for k in foregrounds_light])
teststring = "foo 123"
rowtext = "".join(
[f"{fc} {sc } {teststring:8s}" for fg, fc in foregrounds_light.items()]
)
print(" " * 18 + colnames)
print(Style.RESET_ALL)
for bg, bc in backgrounds.items():
print(f"{bg:<16s} {bc} {rowtext} {Style.RESET_ALL}")
print()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment