From 949dbdc1d88cc254ae2ebd992f9786dada5cde70 Mon Sep 17 00:00:00 2001 From: nojhan Date: Sat, 31 Mar 2012 12:46:56 +0200 Subject: [PATCH 001/228] first commit --- README | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 README diff --git a/README b/README new file mode 100644 index 0000000..e69de29 From 26d2e376ae5935114ec1b42648f4c8781f0cd0b7 Mon Sep 17 00:00:00 2001 From: nojhan Date: Sat, 31 Mar 2012 13:05:42 +0200 Subject: [PATCH 002/228] Colorisation d'une sortie texte Conflicts: README --- README | 1 + src/colorout.py | 70 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 src/colorout.py diff --git a/README b/README index e69de29..a7b60cf 100644 --- a/README +++ b/README @@ -0,0 +1 @@ +Color Up Arbitrary Command Ouput 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="" ) + From 35a401540972eb0534f74a7b5d151bfb6566d726 Mon Sep 17 00:00:00 2001 From: nojhan Date: Fri, 30 Mar 2012 22:50:32 +0200 Subject: [PATCH 003/228] bugfix: use /usr/bin/env instead of /bon in colorout.py --- src/colorout.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) mode change 100644 => 100755 src/colorout.py diff --git a/src/colorout.py b/src/colorout.py old mode 100644 new mode 100755 index fb69b59..3cbe42c --- a/src/colorout.py +++ b/src/colorout.py @@ -1,4 +1,4 @@ -#!/bon/env python3 +#!/usr/bin/env python3 #encoding: utf-8 import re From fe19251750c44e12c98434c05da1a569cf41aab7 Mon Sep 17 00:00:00 2001 From: nojhan Date: Fri, 30 Mar 2012 23:16:45 +0200 Subject: [PATCH 004/228] bugfix arguments handling and error message + description header --- src/colorout.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/colorout.py b/src/colorout.py index 3cbe42c..1c45c9e 100755 --- a/src/colorout.py +++ b/src/colorout.py @@ -1,12 +1,23 @@ #!/usr/bin/env python3 #encoding: utf-8 +############################################################################################ +# Ce script permet de colorier chaque occurence d'un pattern provenant de l'entrée standard. +# Exemples d'utilisation: +# cat colorout.py | colorout color red bold +# colorout /home/[a-z]+ magenta < /etc/passwd +# ls -l | colorout .\(r.-\){3} yellow standard +# make 2>&1 | colorout [0-9]+ green | colorout error +############################################################################################ + 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" ): + """Formatte chaque occurence de l'expression régulière 'pattern' dans 'text' avec la couleur et le style indiqué, + en utilisant les séquences d'échappement ANSI appropriés.""" # Caractères spéciaux. start = "\033[" @@ -53,10 +64,11 @@ if __name__ == "__main__": 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)) + if nargs <= 1 or nargs >= 4: + msg = "Usage: colorout pattern [color] [style]" + msg += "\n\tAvailable colors: "+" ".join(colors) + msg += "\n\tAvailable styles: "+" ".join(styles) + sys.exit(msg) else: if nargs > 1: pattern = sys.argv[1] From f526f72bf8ca0c61eb279e6ff86915c965ff7c31 Mon Sep 17 00:00:00 2001 From: nojhan Date: Fri, 30 Mar 2012 23:28:42 +0200 Subject: [PATCH 005/228] colorout: bugfix arguments number, more examples --- src/colorout.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/colorout.py b/src/colorout.py index 1c45c9e..61e9428 100755 --- a/src/colorout.py +++ b/src/colorout.py @@ -8,6 +8,7 @@ # colorout /home/[a-z]+ magenta < /etc/passwd # ls -l | colorout .\(r.-\){3} yellow standard # make 2>&1 | colorout [0-9]+ green | colorout error +# make 2>&1 | colorout [0-9]+ yellow standard | colorout error | colorout warning magenta | colorout \(note\)\|\(#pragma\\s+\) green standard ############################################################################################ import re @@ -64,7 +65,7 @@ if __name__ == "__main__": nargs = len(sys.argv) - if nargs <= 1 or nargs >= 4: + if nargs <= 1 or nargs >= 5: msg = "Usage: colorout pattern [color] [style]" msg += "\n\tAvailable colors: "+" ".join(colors) msg += "\n\tAvailable styles: "+" ".join(styles) From fe7b0850e6981f68acad9040ace175d07ac16da2 Mon Sep 17 00:00:00 2001 From: nojhan Date: Sat, 31 Mar 2012 13:08:18 +0200 Subject: [PATCH 006/228] new name --- colout.py | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100755 colout.py diff --git a/colout.py b/colout.py new file mode 100755 index 0000000..61e9428 --- /dev/null +++ b/colout.py @@ -0,0 +1,83 @@ +#!/usr/bin/env python3 +#encoding: utf-8 + +############################################################################################ +# Ce script permet de colorier chaque occurence d'un pattern provenant de l'entrée standard. +# Exemples d'utilisation: +# cat colorout.py | colorout color red bold +# colorout /home/[a-z]+ magenta < /etc/passwd +# ls -l | colorout .\(r.-\){3} yellow standard +# make 2>&1 | colorout [0-9]+ green | colorout error +# make 2>&1 | colorout [0-9]+ yellow standard | colorout error | colorout warning magenta | colorout \(note\)\|\(#pragma\\s+\) green standard +############################################################################################ + +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" ): + """Formatte chaque occurence de l'expression régulière 'pattern' dans 'text' avec la couleur et le style indiqué, + en utilisant les séquences d'échappement ANSI appropriés.""" + + # 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 >= 5: + msg = "Usage: colorout pattern [color] [style]" + msg += "\n\tAvailable colors: "+" ".join(colors) + msg += "\n\tAvailable styles: "+" ".join(styles) + sys.exit(msg) + 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="" ) + From 6a33ab321dbd9aacbc422703bc89c4c39885ac8e Mon Sep 17 00:00:00 2001 From: nojhan Date: Sat, 31 Mar 2012 13:10:07 +0200 Subject: [PATCH 007/228] remove old file --- src/colorout.py | 83 ------------------------------------------------- 1 file changed, 83 deletions(-) delete mode 100755 src/colorout.py diff --git a/src/colorout.py b/src/colorout.py deleted file mode 100755 index 61e9428..0000000 --- a/src/colorout.py +++ /dev/null @@ -1,83 +0,0 @@ -#!/usr/bin/env python3 -#encoding: utf-8 - -############################################################################################ -# Ce script permet de colorier chaque occurence d'un pattern provenant de l'entrée standard. -# Exemples d'utilisation: -# cat colorout.py | colorout color red bold -# colorout /home/[a-z]+ magenta < /etc/passwd -# ls -l | colorout .\(r.-\){3} yellow standard -# make 2>&1 | colorout [0-9]+ green | colorout error -# make 2>&1 | colorout [0-9]+ yellow standard | colorout error | colorout warning magenta | colorout \(note\)\|\(#pragma\\s+\) green standard -############################################################################################ - -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" ): - """Formatte chaque occurence de l'expression régulière 'pattern' dans 'text' avec la couleur et le style indiqué, - en utilisant les séquences d'échappement ANSI appropriés.""" - - # 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 >= 5: - msg = "Usage: colorout pattern [color] [style]" - msg += "\n\tAvailable colors: "+" ".join(colors) - msg += "\n\tAvailable styles: "+" ".join(styles) - sys.exit(msg) - 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="" ) - From ad010765d82d59779ddb142642be5524d2257120 Mon Sep 17 00:00:00 2001 From: nojhan Date: Sat, 31 Mar 2012 14:39:20 +0200 Subject: [PATCH 008/228] description in README --- README | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README b/README index a7b60cf..92cfdb9 100644 --- a/README +++ b/README @@ -1 +1,10 @@ Color Up Arbitrary Command Ouput + +A regular expression based formatter that color up an arbitrary text output stream. +Examples : + cat colout.py | colout color red bold + colout /home/[a-z]+ magenta < /etc/passwd + ls -l | colout .\(r.-\){3} yellow standard + make 2>&1 | colout [0-9]+ green | colout error + make 2>&1 | colout [0-9]+ yellow standard | colout error | colout warning magenta | colout \(note\)\|\(#pragma\\s+\) green standard + From fea7e2c03f62fe9e284e88696ee99be419ca43ad Mon Sep 17 00:00:00 2001 From: nojhan Date: Sat, 31 Mar 2012 14:40:46 +0200 Subject: [PATCH 009/228] now supports coloring only the grouping parts of the regex, if any --- colout.py | 80 +++++++++++++++++++++++++++++-------------------------- 1 file changed, 42 insertions(+), 38 deletions(-) diff --git a/colout.py b/colout.py index 61e9428..16cf69d 100755 --- a/colout.py +++ b/colout.py @@ -1,59 +1,63 @@ #!/usr/bin/env python3 #encoding: utf-8 -############################################################################################ -# Ce script permet de colorier chaque occurence d'un pattern provenant de l'entrée standard. -# Exemples d'utilisation: -# cat colorout.py | colorout color red bold -# colorout /home/[a-z]+ magenta < /etc/passwd -# ls -l | colorout .\(r.-\){3} yellow standard -# make 2>&1 | colorout [0-9]+ green | colorout error -# make 2>&1 | colorout [0-9]+ yellow standard | colorout error | colorout warning magenta | colorout \(note\)\|\(#pragma\\s+\) green standard -############################################################################################ - 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" ): - """Formatte chaque occurence de l'expression régulière 'pattern' dans 'text' avec la couleur et le style indiqué, - en utilisant les séquences d'échappement ANSI appropriés.""" - # Caractères spéciaux. +def colorin( text, color, style ): + """Return the given text, surrounded by the given color ASCII markers.""" + # Special characters. start = "\033[" stop = "\033[0m" - - # Conversion en string du code couleur demandé. + + # Convert the color code. cs = str(styles[style]) cc = str(colors[color]) - # Compilation de l'expression régulière. + return start + cs + ";" + cc + "m" + text + stop + + +def colorout( text, match, prev_end, color, style, group=0 ): + """Build the text from the previous match to the current one, coloring up the matching characters.""" + start = match.start(group) + colored_text = text[prev_end:start] + end = match.end(group) + colored_text += colorin(text[start:end], color, style) + return colored_text,end + + +def colorup( text, pattern, color, style = "standard" ): + """Color up every characters that match the given patterns. + If groups are specified, only color up them and not the whole pattern.""" regex = re.compile(pattern, re.IGNORECASE) - # Texte colorié. - ctext = "" - e = 0 - # Pour chaque occurence d'une correspondance dans le texte. + # Prepare the colored text. + colored_text = "" + end = 0 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() + # If not groups are specified + if not match.groups(): + # Color the previous partial line, + partial,end = colorout( text, match, end, color, style ) + # add it to the final text. + colored_text += partial + else: + # For each group index. + # Note that match.groups returns a tuple (thus being indexed in [0,n[), + # but that match.start(0) refers to the whole match, the groups being indexed in [1,n]. + # Thus, we need to range in [1,n+1[. + for group in range(1,len(match.groups())+1): + partial,end = colorout( text, match, end, color, style, group ) + colored_text += partial + + # Append the remaining part of the text, if any. + colored_text += text[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 + return colored_text if __name__ == "__main__": @@ -79,5 +83,5 @@ if __name__ == "__main__": style = sys.argv[3] for line in sys.stdin: - print( colored( line, pattern, color, style ), end="" ) + print( colorup( line, pattern, color, style ), end="" ) From 60c86f9182b06188ce0dea5b92b724e83efbee66 Mon Sep 17 00:00:00 2001 From: nojhan Date: Sat, 31 Mar 2012 15:11:27 +0200 Subject: [PATCH 010/228] use argparse, comments in english --- colout.py | 42 ++++++++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/colout.py b/colout.py index 16cf69d..30d8f98 100755 --- a/colout.py +++ b/colout.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python3 +#!/usr/bin/env python #encoding: utf-8 import re @@ -62,26 +62,32 @@ def colorup( text, pattern, color, style = "standard" ): if __name__ == "__main__": import sys + import argparse - pattern = ".*" - color= "red" - style = "bold" + parser = argparse.ArgumentParser( + description="A regular expression based formatter that color up an arbitrary text output stream.") - nargs = len(sys.argv) + parser.add_argument("pattern", metavar="REGEX", type=str, nargs=1, + help="A regular expression") - if nargs <= 1 or nargs >= 5: - msg = "Usage: colorout pattern [color] [style]" - msg += "\n\tAvailable colors: "+" ".join(colors) - msg += "\n\tAvailable styles: "+" ".join(styles) - sys.exit(msg) - else: - if nargs > 1: - pattern = sys.argv[1] - if nargs > 2: - color = sys.argv[2] - if nargs > 3: - style = sys.argv[3] + parser.add_argument("color", metavar="COLOR", type=str, nargs='?', + default="red", + help="One of the following colors: "+" ".join(colors), choices = colors) + + parser.add_argument("style", metavar="STYLE", type=str, nargs='?', + default="bold", + help="One of the following styles: "+" ".join(styles), choices=styles) + + parser.add_argument("-e", "--stderr", action="store_true", + help="Output on the stderr instead of stdout") + + args = parser.parse_args() for line in sys.stdin: - print( colorup( line, pattern, color, style ), end="" ) + if not args.stderr: + print colorup( line, args.pattern[0], args.color, args.style ), + sys.stdout.flush() + else: + print >> sys.stderr, colorup( line, args.pattern[0], args.color, args.style ), + sys.stderr.flush() From 5117fd92fc147b768fbb83fb1b17380ff1bf19ad Mon Sep 17 00:00:00 2001 From: nojhan Date: Sat, 31 Mar 2012 15:45:46 +0200 Subject: [PATCH 011/228] a complete man page --- README | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 59 insertions(+), 8 deletions(-) diff --git a/README b/README index 92cfdb9..8490a5c 100644 --- a/README +++ b/README @@ -1,10 +1,61 @@ -Color Up Arbitrary Command Ouput +colout(1) -- Color Up Arbitrary Command Ouput +============================================= -A regular expression based formatter that color up an arbitrary text output stream. -Examples : - cat colout.py | colout color red bold - colout /home/[a-z]+ magenta < /etc/passwd - ls -l | colout .\(r.-\){3} yellow standard - make 2>&1 | colout [0-9]+ green | colout error - make 2>&1 | colout [0-9]+ yellow standard | colout error | colout warning magenta | colout \(note\)\|\(#pragma\\s+\) green standard +## SYNOPSIS + +`colout` [-h] [-e] PATTERN [COLOR] [STYLE] + + +## DESCRIPTION + +`colout` read lines of text stream on the standard input and output characters +matching a given regular expression in given and