Skip to main content

grec

https://badge.fury.io/py/grec.png https://travis-ci.org/brisad/grec.png?branch=master https://pypip.in/d/grec/badge.png

Colorize terminal text with regular expressions.

Features

grec is similar to grep; the difference being that instead of printing matching lines in a file, grec colorizes lines. Lines that do not match any pattern are still printed, but without color.

The key feature that separates this utility from other similar ones is that it’s possible to colorize a matching string several times without letting previously matched colors mess up following regular expression matches.

Quick Start

Install with pip:

pip install grec

Print the contents of log_file.txt in its entirety but also colorize any occurrences of errors and warnings:

grec -m ERROR red -m WARN yellow log_file.txt

Or use a pipe:

cat log_file.txt | grec -m ERROR red -m WARN yellow -

To colorize strings in Python code, use the Matcher class from the grec package:

>>> from grec import Matcher
>>> m = Matcher()
>>> m.add_pattern('ERROR', 'red')
>>> m.add_pattern('WARN', 'yellow')
>>> print m.match('ERROR WARN INFO')
ERROR WARN INFO  (with color)

Command line

The command line interface is the following:

grec [-m PATTERN COLOR_INFO] [-g PATTERN [COLOR_INFO ...]] -- file

The -m argument

This argument takes a regular expression and color information. Here’s an example that will make all lines starting with the character “#” have a green color with white background:

-m '^#.*' green_on_white

Whenever a line matches the regular expression, the part of the line that matched is colorized with the color information. Any number of -m arguments can be specified, and colorization will be applied in the order specified on the command line.

The regular expression will be matched by the re module. So for each regular expression, only non-overlapping matches will be colorized. To get overlapping matches use several patterns by adding more -m arguments.

Color information consists of a foreground and optionally a background. Colorization is performed with the termcolor package and thus the following colors are supported: grey, red, green, yellow, blue, magenta, cyan, white.

To only set the foreground color, simply specify the name of the color. To also set a background color, add it to the foreground color. Use quotes or underlines to prevent the shell from interpreting it as several arguments. Examples:

-m <regex> blue_on_yellow
-m <regex> blue_yellow
-m <regex> 'blue on yellow'
-m <regex> 'blue yellow'

The -g argument

This argument is similar to -m but with the difference that instead of colorizing the whole match, this creates a group pattern that only colorizes matched groups of the regular expression.

Because one can have multiple groups within a regular expression, this argument accepts multiple colors. Here’s an example which will colorize the first group with green color on white background and the second with yellow foreground:

-g '^(#)(.*)' green_on_white yellow

If more colors than there are groups in the regular expression are specified, they will be ignored. If the number of colors is less than the groups, the last color specified for the pattern will be used to colorize all of the remaining group matches.

The file argument

This is the file to colorize. If “-” is specified, stdin will be read instead and can be used to colorize the output of a pipe.

If no file is given, stdin will be used as the default.

TODO

  • Add support for attributes like blinking

  • Add support for only changing background color from CLI

  • Python 3 support

History

0.2.0 (2014-10-14)

  • Added support for colorizing groups in regular expressions

0.1.0 (2014-09-26)

  • First release on PyPI.

Metadata

Release files for grec 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for grec 0.2.0
File Size Uploaded Provenance Actions
grec-0.2.0.tar.gz 31.3 kB Without provenance Details for grec-0.2.0.tar.gz

Release files / grec-0.2.0.tar.gz

Download URL grec-0.2.0.tar.gz
Size 31.3 kB
Tags Source
SHA-256 checksum
How to use checksums
ebcefa0888e52ba1cd3103d1216f1a2bbbd51a369920518bd99460f564b614b3
BLAKE2b-256 checksum
How to use checksums
7afdbd22942443b629fcd4dad3fd6907e405b59682cfd822cb1e125657fcc0f4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for grec 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.2.0 This release

1 release file No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page