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
- Support for ANSI color codes and characters (output is aligned)
- Same syntax and parameters as
column
(seecolumn_ansi --help
for more info) - Right, Left and even Center alignment for every column (1-based)!
- Support for hidden columns
- Support for quoted fields and escaped quotes thanks to the
Text::ParseWords
module!
TODO
- Improve
CONTRIBUTING.md
file - Parse CLI parameters directly from the Perl file (maybe the
Getopts:Long
package?) - Write better tests (maybe use a testing framework?) - Work in progress…
- Add option to keep or skip empty lines (right now it keeps them, which is different from the default behaviour of
column
); - Add option to remove colors and control characters from the output;
- Make the choice of right/center alignment explicit (so that one cannot pass the same column index to both options)
- Update documentation with the description of the various Environment Variables used by the Perl script
- Feel free to suggest anything in the Issue section… 😃
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
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:
-
Original
column
command - WITH colors The output is completely out of alignment because of the colors. -
Original
column
command - WITHOUT colors After removing the colors the output is shown as expected. -
Custom
column2
command - By @NORMAN GEIST Output is shown as expected but is very slow compared tocolumn
. -
Custom
column_ansi
command - By me Output is shown as expected and the time needed to render is way similar to the originalcolumn
one.
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
- Works without a bug (tested)
Cons
- Very slow since it uses Bash
read
command in a loop (see this answer for more info)
Links
SandersJ16 - ccolumn
Very good project written entirely in Bash which has some similarities with the original column
command, yet handles colors.
Pro
- Has both
fill by rows
andfill by columns
options from the originalcolumn
command; - Allows to choose whether or not to print colors
- Has an option to print empty lines too
Cons
- Very slow since it uses Bash
read
command in a loop (see this answer for more info) - Possibly slower than
column2
(not tested)
Links
tecolicom - App-ansicolumn
ANSI terminal sequence aware column command written in Perl (not tested).
Pro
- Has both
fill by rows
andfill by columns
options from the originalcolumn
command; - Good documentation
- Lots of additional options
Cons
Not enough data
Links
Contributors
Luca Salvarani 💻 📖 🚧 💬 👀 ⚠️ |
bartasha 💻 🐛 ⚠️ |
Lukas Willin 🐛 💻 ⚠️ |
||||
Add your contributions </img> |