Skip to the content.

License GitHub contributors (via allcontributors.org) GitHub last commit GitHub issues

CI

ANSI-compatible column: column_ansi

Perl version of column with support for ANSI color codes

Syntax

column_ansi [-s SEPARATOR] [-o SEPARATOR] [-R COLUMNS] [-H COLUMNS] [-C COLUMNS]
column_ansi --help

Options

-t, --table
        Does nothing, was left for compatibility reasons. [DEFAULT]

-s SEPARATOR, --separator SEPARATOR
        Specify the possible input item delimiters (default is whitespace).

-r SEPARATOR, --output-separator SEPARATOR
        Specify the columns delimiter for table output (default is two spaces).

-R COLUMNS, --table-right COLUMNS
        Right align text in the specified columns (comma-separated).

-H COLUMNS, --table-hide COLUMNS
        Don't print specified columns. The special placeholder '-' maybe be used to hide all unnamed columns (see --table-columns).
        IMPORTANT: The striked part of the description is still not implemented.

-C COLUMNS, --table-center COLUMNS
        Center align text in the specified columns (comma-separated). 
        This option is not present in the original column command.

-h, --help
        Display help text and exit.

Features

TODO

Description

I needed to format a very long awk-generated colored output (more than 300 lines) into a nice table. I first thought of using column, but as i discovered it didn’t take into consideration ANSI characters, since the output would come out not aligned.

After searching a bit on Google i found this interesting answer on SO which dynamically calculated the width of every single column in the output after removing the ANSI characters and THEN it built the table.

It was all good, but it was taking way too long to load… In the attached tests test_column_ansi.sh you will find that even for 2 lines column2 (the one i found on SO) takes up to 2-3 seconds (which is a lot in comparison to the 0.0xx seconds of the original column)!

After trying out this version in my production script the time used to display data dropped from 30s to <1s!!

Considerations

Why not use bash?

Because each call to bash’s read is very slow.

Screenshots

Comparison with column command

Comparison with column

Help page

Help page

Tests

In the following screenshot you can see (and try it yourself, by executing the attached test_column_ansi.sh script) 4 tests with the corresponding timings:

  1. Original column command - WITH colors The output is completely out of alignment because of the colors.

  2. Original column command - WITHOUT colors After removing the colors the output is shown as expected.

  3. Custom column2 command - By @NORMAN GEIST Output is shown as expected but is very slow compared to column.

  4. Custom column_ansi command - By me Output is shown as expected and the time needed to render is way similar to the original column one.

Example output

Run tests

To run tests ensure to have shellspec installed, then launch the following command:

shellspec --format d

Other projects

NORMAN GEIST - ccolumn

Very spartan project written entirely in Bash which works but it is very slow… It is the project that led me to the Perl solution.

Pro

Cons

SandersJ16 - ccolumn

Very good project written entirely in Bash which has some similarities with the original column command, yet handles colors.

Pro

Cons

tecolicom - App-ansicolumn

ANSI terminal sequence aware column command written in Perl (not tested).

Pro

Cons

Not enough data

Contributors

Luca Salvarani
Luca Salvarani

💻 📖 🚧 💬 👀 ⚠️
bartasha
bartasha

💻 🐛 ⚠️
Lukas Willin
Lukas Willin

🐛 💻 ⚠️
Add your contributions </img>