From 1d883a0ff93d4685c4b33e34750d39f34f89210e Mon Sep 17 00:00:00 2001 From: nojhan Date: Fri, 30 Mar 2012 22:46:30 +0200 Subject: [PATCH] Colorisation d'une sortie texte --- README | 1 + src/colorout.py | 70 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 src/colorout.py diff --git a/README b/README index ea0b026..9ddcf2a 100644 --- a/README +++ b/README @@ -27,6 +27,7 @@ Jeux de tableaux Système ------- +* Colorisation d'une sortie texte - CODÉ * Déplacement de fichier selon le type * Télécharger toutes les images d'une page web * Capture d'écran à télécharger sur un site en gardant l'url dans le presse-papier diff --git a/src/colorout.py b/src/colorout.py new file mode 100644 index 0000000..fb69b59 --- /dev/null +++ b/src/colorout.py @@ -0,0 +1,70 @@ +#!/bon/env python3 +#encoding: utf-8 + +import re + +styles = {"standard":0, "bold":1, "reverse":2} +colors = {"black":30, "red":31, "green":32, "yellow":33, "blue":34, "magenta":35, "cyan":36, "white":37} + +def colored( text, pattern, color, style = "standard" ): + + # Caractères spéciaux. + start = "\033[" + stop = "\033[0m" + + # Conversion en string du code couleur demandé. + cs = str(styles[style]) + cc = str(colors[color]) + + # Compilation de l'expression régulière. + regex = re.compile(pattern, re.IGNORECASE) + + # Texte colorié. + ctext = "" + e = 0 + # Pour chaque occurence d'une correspondance dans le texte. + for match in regex.finditer(text): + + # Position dans text du début de l'occurence. + s = match.start() + + # On ajoute le texte entre la dernière occurence,, + # il faut noter que e=0, à la première itération. + ctext += text[e:s] + + # Position dans text de la fin de l'occurence. + e = match.end() + + # On ajoute l'occurence, en colorant. + ctext += start + cs + ";" + cc + "m" + text[s:e] + stop + + # On ajoute la fin du texte. + ctext += text[e:] + + return ctext + + +if __name__ == "__main__": + import sys + + pattern = ".*" + color= "red" + style = "bold" + + nargs = len(sys.argv) + + if nargs < 1 or nargs > 4: + print("Usage: colorout pattern [color] [style]") + print("\tAvailable colors:"," ".join(colors)) + print("\tAvailable styles:"," ".join(styles)) + else: + if nargs > 1: + pattern = sys.argv[1] + if nargs > 2: + color = sys.argv[2] + if nargs > 3: + style = sys.argv[3] + + for line in sys.stdin: + print( colored( line, pattern, color, style ), end="" ) +