pericmd 046: Customizing table output (2)

Continuing from previous post, if we use Perinci::CmdLine::Classic as a backend, there are a few other options to customize table output. Let’s use the same list-files script, but use the classic backend:

% PERINCI_CMDLINE_ANY=classic ./list-files -v

pericmd046-1

You’ll notice that compared to the default Perinci::CmdLine::Lite’s output (which uses Text::Table::Tiny to produce the table), the Perinci::CmdLine::Classic’s output (which uses Text::ANSITable) is a bit fancier, e.g. colors and boxchars (and/or Unicode characters).

By default, Text::ANSITable colors columns differently according to data type. The second column, since it contains only numbers and thus is a numeric column, is colored cyan by default. While string columns are colored light grey by default.

Of course, like the lite backend, the classic backend supports reordering columns:

% PERINCI_CMDLINE_ANY=classic ./list-files2 -v

pericmd046-2

% PERINCI_CMDLINE_ANY=classic FORMAT_PRETTY_TABLE_COLUMN_ORDERS='[["type","size","links"]]' ./list-files2 -v

pericmd046-3

Aside from FORMAT_PRETTY_TABLE_COLUMN_ORDERS, there’s also FORMAT_PRETTY_TABLE_COLUMN_TYPES:

% PERINCI_CMDLINE_ANY=classic FORMAT_PRETTY_TABLE_COLUMN_TYPES='[{"modified":"date"}]' ./list-files3 -v

pericmd046-4

The mentioned list-files3 is exactly the same as list-files2 except that it adds a column modified containing mtime Unix timestamp of file. By default will be shown as a number (cyan), but with the above FORMAT_PRETTY_TABLE_COLUMN_TYPES hint the column is shown as a date (yellow).

Note that there is some heuristics employed, so if you name the column “mtime” or “something_date”, you don’t have to give any hint to show the column as date.

There is also FORMAT_PRETTY_TABLE_COLUMN_FORMATS to apply some formatting to columns, for example:

% PERINCI_CMDLINE_ANY=classic FORMAT_PRETTY_TABLE_COLUMN_FORMATS='[{"size":[["num",{"style":"kilo"}]]}]' ./list-files2 -v

pericmd046-5

The POD for Data::Format::Pretty::Console describes these options in more details.

Aside from these, the Text::ANSITable module itself provides lots of options to configure its output. For example, to choose border style and color theme:

% PERINCI_CMDLINE_ANY=classic ANSITABLE_BORDER_STYLE="Default::csingle" ANSITABLE_COLOR_THEME="Tint::tint_red" ./list-files2 -v

pericmd046-6

With Text::ANSITable you can also customize cell padding/spacing, column widths, or alignments. You can hide some columns/rows, repeat some columns/rows, or even do conditional styles involving Perl code. For more available options, refer to the POD.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s