Add the possibility to color a source code SUBSTRING

`echo "There is an error in 'static void Functor::operator()( EOT& indiv ) { return indiv; }' you should fix it" | colout "'(.*)'" Cpp monokai`
This commit is contained in:
Johann Dreo 2013-03-23 17:52:32 +01:00
commit 9da74a1202
2 changed files with 24 additions and 4 deletions

View file

@ -36,7 +36,7 @@ When not specified, a *COLOR* defaults to _red_ and a *STYLE* defaults to _bold_
(see the `-t` switch below).
If the python-pygments library is available, `colout` can be used as an interface
to it (see the `-s` switch below).
to it (see also the `-s` switch below).
`colout` is released under the GNU Public License v3.
@ -124,3 +124,6 @@ special characters that would be recognize by your shell.
* Color a JSON stream:
`echo '{"foo": "lorem", "bar":"ipsum"}' | python -mjson.tool | colout -t json`
* Color a source code substring:
`echo "There is an error in 'static void Functor::operator()( EOT& indiv ) { return indiv; }' you should fix it" | colout "'(.*)'" Cpp monokai`

View file

@ -80,9 +80,8 @@ def colorin(text, color="red", style="normal"):
if style == "random" or style == "Random":
style = random.choice(list(styles.keys()))
else:
assert(style in styles)
style_code = str(styles[style])
if style in styles:
style_code = str(styles[style])
if color == "random":
mode = 8
@ -125,6 +124,24 @@ def colorin(text, color="red", style="normal"):
mode = 8
color_code = str(30 + colors[color])
# programming language
elif color.lower() in lexers:
lexer = get_lexer_by_name(color.lower())
# Python => 256 colors, python => 8 colors
ask_256 = color[0].isupper()
if ask_256:
try:
formatter = Terminal256Formatter(style=style)
except: # style not found
formatter = Terminal256Formatter()
else:
if style not in ("light","dark"):
style = "dark" # dark color scheme by default
formatter = TerminalFormatter(bg=style)
# We should return all but the last character,
# because Pygments adds a newline char.
return highlight(text, lexer, formatter)[:-1]
# 256 colors mode
else:
mode = 256