diff --git a/README.md b/README.md index f999fa2..ffb512f 100644 --- a/README.md +++ b/README.md @@ -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` + diff --git a/colout.py b/colout.py index 0efa9a9..db6e953 100755 --- a/colout.py +++ b/colout.py @@ -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