Last active
October 30, 2024 22:43
-
-
Save rene-d/9e584a7dd2935d0f461904b9f2950007 to your computer and use it in GitHub Desktop.
ANSI color codes in Python
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
# SGR color constants | |
# rene-d 2018 | |
class Colors: | |
""" ANSI color codes """ | |
BLACK = "\033[0;30m" | |
RED = "\033[0;31m" | |
GREEN = "\033[0;32m" | |
BROWN = "\033[0;33m" | |
BLUE = "\033[0;34m" | |
PURPLE = "\033[0;35m" | |
CYAN = "\033[0;36m" | |
LIGHT_GRAY = "\033[0;37m" | |
DARK_GRAY = "\033[1;30m" | |
LIGHT_RED = "\033[1;31m" | |
LIGHT_GREEN = "\033[1;32m" | |
YELLOW = "\033[1;33m" | |
LIGHT_BLUE = "\033[1;34m" | |
LIGHT_PURPLE = "\033[1;35m" | |
LIGHT_CYAN = "\033[1;36m" | |
LIGHT_WHITE = "\033[1;37m" | |
BOLD = "\033[1m" | |
FAINT = "\033[2m" | |
ITALIC = "\033[3m" | |
UNDERLINE = "\033[4m" | |
BLINK = "\033[5m" | |
NEGATIVE = "\033[7m" | |
CROSSED = "\033[9m" | |
END = "\033[0m" | |
# cancel SGR codes if we don't write to a terminal | |
if not __import__("sys").stdout.isatty(): | |
for _ in dir(): | |
if isinstance(_, str) and _[0] != "_": | |
locals()[_] = "" | |
else: | |
# set Windows console in VT mode | |
if __import__("platform").system() == "Windows": | |
kernel32 = __import__("ctypes").windll.kernel32 | |
kernel32.SetConsoleMode(kernel32.GetStdHandle(-11), 7) | |
del kernel32 | |
if __name__ == '__main__': | |
for i in dir(Colors): | |
if i[0:1] != "_" and i != "END": | |
print("{:>16} {}".format(i, getattr(Colors, i) + i + Colors.END)) |
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
#! /bin/bash | |
# rene-d 2015 | |
# définition de constantes SGR pour affichage de couleur | |
# exemple: echo -e ${COLOR_RED}rouge${COLOR_END} | |
if ! tty -s; then | |
# ne pas changer l'indentation des lignes suivantes | |
COLOR_BLACK="" | |
COLOR_RED="" | |
COLOR_GREEN="" | |
COLOR_BROWN="" | |
COLOR_BLUE="" | |
COLOR_PURPLE="" | |
COLOR_CYAN="" | |
COLOR_LIGHT_GRAY="" | |
COLOR_DARK_GRAY="" | |
COLOR_LIGHT_RED="" | |
COLOR_LIGHT_GREEN="" | |
COLOR_YELLOW="" | |
COLOR_LIGHT_BLUE="" | |
COLOR_LIGHT_PURPLE="" | |
COLOR_LIGHT_CYAN="" | |
COLOR_LIGHT_WHITE="" | |
COLOR_BOLD="" | |
COLOR_FAINT="" | |
COLOR_ITALIC="" | |
COLOR_UNDERLINE="" | |
COLOR_BLINK="" | |
COLOR_NEGATIVE="" | |
COLOR_CROSSED="" | |
COLOR_END="" | |
else | |
# ne pas indenter les lignes suivantes (pour le sed suivant) | |
COLOR_BLACK="\033[0;30m" | |
COLOR_RED="\033[0;31m" | |
COLOR_GREEN="\033[0;32m" | |
COLOR_BROWN="\033[0;33m" | |
COLOR_BLUE="\033[0;34m" | |
COLOR_PURPLE="\033[0;35m" | |
COLOR_CYAN="\033[0;36m" | |
COLOR_LIGHT_GRAY="\033[0;37m" | |
COLOR_DARK_GRAY="\033[1;30m" | |
COLOR_LIGHT_RED="\033[1;31m" | |
COLOR_LIGHT_GREEN="\033[1;32m" | |
COLOR_YELLOW="\033[1;33m" | |
COLOR_LIGHT_BLUE="\033[1;34m" | |
COLOR_LIGHT_PURPLE="\033[1;35m" | |
COLOR_LIGHT_CYAN="\033[1;36m" | |
COLOR_LIGHT_WHITE="\033[1;37m" | |
COLOR_BOLD="\033[1m" | |
COLOR_FAINT="\033[2m" | |
COLOR_ITALIC="\033[3m" | |
COLOR_UNDERLINE="\033[4m" | |
COLOR_BLINK="\033[5m" | |
COLOR_NEGATIVE="\033[7m" | |
COLOR_CROSSED="\033[9m" | |
# ne pas changer l'indentation des lignes suivantes | |
COLOR_END="\033[0m" | |
fi | |
# affiche les couleurs | |
if [ "$1" = "-h" ] ; then | |
for i in `cat $0 | sed -e 's/^\(COLOR.*\)=\(.*\)/\1/p;d'`; do | |
[ "$i" != "COLOR_END" ] && echo -e ${!i}$i${COLOR_END} | |
done | |
fi |
Merci pour les codes couleurs.
For simply white: "\033[0m"
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
These comments are the best way to comment on issues.