Skip to content

ANSI-friendly version of `column` (Perl version).

License

Notifications You must be signed in to change notification settings

bartasha/column_ansi

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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 (see column_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

  • 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 one cannot pass the same column index to both options)
  • 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

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

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 and fill by columns options from the original column 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

About

ANSI-friendly version of `column` (Perl version).

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Shell 100.0%