Skip to main content

A simple terminal colorizer

Project description

tcolr - table colouriser

This is a simple program that handles colorising tabular content using a set of rules, similar to how grc or similar out of band colorisers work.

Overview

When working using a command shell is it often very useful to have colourised output. This makes it easier to parse output at a glance and see

Consider the following output.

bash% kubectl get pods
NAME                               READY   STATUS    RESTARTS        AGE
etcd-node001                       1/1     Running   1 (4d20h ago)   15d
kube-apiserver-node001             1/1     Running   0               4d20h
kube-controller-manager-node001    0/1     Pending   0               4d20h
kube-proxy-ngz4s                   1/1     Running   0               4d20h
kube-scheduler-node001             1/1     Running   0               4d20h

This is a typical example from the kubernetes world. The word Pending is often difficult to recognise in a page full of pods since the overall shape of the word is very similar to Running.

There is the kubecolor drop in replacement for kubectl but this implies that you will always want the same colourisation logic to be applied in all circumstances.

It is often more beneficial to decouple the colourisation of the content from the command that generates it. This is where "generic colourisers" such are grc (amongst others) have value.

However, all of these colourisers struggle to work flexibly with tabular content since their pattern matching logic is line based.

This is the reason for tcolr. It only exists for handling the colourisation of tabular context. Whilst the examples above are all examples from the kubernetes world, this is intended for for colourising tabular content from any source.

Usage

Basic Usage

tcolr is driven from a simple configuration file such as the one below:

rules:
  - column: STATUS
    match: Running
    color: green
  - column: STATUS
    match: Failed
    color: red
  - column: READY
    match: '1/1'
    color: yellow

When tcolr parses each line, it first parses the line into columns based on the column header widths. It then evaluates the rules for each line and if the rule matches it colourises that cell to the given colour. The matches in the above example are all literals, but they are evaulated as standard regular expressions.

No effort at present has been made to avoid multiple rules matching for a given column. It is up to you to ensure this doesn't occur.

Variables

Sometimes it might be valuable to parse the line into variables so that you can make colourisation decisions with more control or alternatively to colourise one cell based on the values in another cell on the line. This is possible by specifying a config file with a values block.

If you consider the sample output and the following config file:

variables:
  - name: ready
    column: READY
    match: (\d+)/\d+
  - name: max
    column: READY
    match: \d+/(\d+)

rules:
  - column: READY
    match: '.*'
    color: red
    when: 'int(ready) < int(max)'
  - column: READY
    match: '.*'
    color: yellow

If any variables are specified tcolr will generate all these variables first when parsing a line. Once these are present, they are available to all of the rules. The above example will colourise the READY field as red if all of the containers are not ready (i.e. 0/1) and yellow for the normal case.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tcolr-0.2.0.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tcolr-0.2.0-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file tcolr-0.2.0.tar.gz.

File metadata

  • Download URL: tcolr-0.2.0.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.8.2 requests/2.28.1 setuptools/68.2.2 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.6

File hashes

Hashes for tcolr-0.2.0.tar.gz
Algorithm Hash digest
SHA256 0b4bd0ccf3d2e984878e8e1476429546a1f9c3f621f5ff01ebe708b0a41ce7b2
MD5 935e805fbe3217e252658001f2e8625d
BLAKE2b-256 86a153d3941595eb9ecc14bd46d1da8beed0fce196092d425cc726859b003201

See more details on using hashes here.

File details

Details for the file tcolr-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: tcolr-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 5.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.8.2 requests/2.28.1 setuptools/68.2.2 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.6

File hashes

Hashes for tcolr-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 28f7223e4a865ca29d24ac467b0dbb307e32fdb50c202db1ae07e4079452a13c
MD5 0105899ae290576c8421963e92beebda
BLAKE2b-256 f5c612fde7f8c1630e9ad41738c7380db9a381abb4f309ca6fd9922a62813d9c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page