Create a nice formatted table using ascii characters.
Pretty nifty if you want to output dynamic text to your console or other fixed-size-font displays, and at the same time it will display it in a nice human-readable, or \*(L"cool\*(R" way.
use Text::ASCIITable; $t = Text::ASCIITable->new({ headingText => 'Basket' }); $t->setCols('Id','Name','Price'); $t->addRow(1,'Dummy product 1',24.4); $t->addRow(2,'Dummy product 2',21.2); $t->addRow(3,'Dummy product 3',12.3); $t->addRowLine(); $t->addRow('','Total',57.9); print $t; # Result: .------------------------------. | Basket | +----+-----------------+-------+ | Id | Name | Price | +----+-----------------+-------+ | 1 | Dummy product 1 | 24.4 | | 2 | Dummy product 2 | 21.2 | | 3 | Dummy product 3 | 12.3 | +----+-----------------+-------+ | | Total | 57.9 | '----+-----------------+-------'
Initialize a new table. You can specify output-options. For more options, check out the usage for setOptions()
Usage: $t = Text::ASCIITable->new();
Or with options: $t = Text::ASCIITable->new({ hide_Lastline => 1, reportErrors => 0});
Define the columns for the table(compare with <\s-1TH\s0> in \s-1HTML\s0). For example \*(C`setCols(['Id','Nick','Name'])\*(C'. Note that you cannot add Cols after you have added a row. Multiline columnnames are allowed.
Adds one row to the table. This must be an array of strings. If you defined 3 columns. This array must have 3 items in it. And so on. Should be self explanatory. The strings can contain newlines.
Note: It does not require argument to be an array, thus; $t->addRow(['id','name']) and $t->addRow('id','name') does the same thing.
This module is also overloaded to accept push. To construct a table with the use of overloading you might do the following:
$t = Text::ASCIITable->new(); $t->setCols('one','two','three','four'); push @$t, ( "one\ntwo" ) x 4; # Replaces $t->addrow(); print $t; # Replaces print $t->draw();
Which would construct: .-----+-----+-------+------. | one | two | three | four | |=----+-----+-------+-----=| | one | one | one | one | # Note that theese two lines | two | two | two | two | # with text are one singe row. '-----+-----+-------+------'
There is also possible to give this function an array of arrayrefs and hence support the output from DBI::selectall_arrayref($sql) without changes.
Example of multiple-rows pushing: $t->addRow([ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ], ]);
Will add a line after the current row. As an argument, you may specify after which row you want a line (first row is 1) or an array of row numbers. (\s-1HINT:\s0 If you want a line after every row, read about the drawRowLine option in setOptions())
Example without arguments:
$t->addRow('one','two'X'three'); $t->addRowLine(); $t->addRow('one','two'X'three');
Example with argument:
$t->addRow('one','two'X'three'); $t->addRow('one','two'X'three'); $t->addRow('one','two'X'three'); $t->addRow('one','two'X'three'); $t->addRowLine\|(1); # or multiple: $t->addRowLine([2,3]);
Given a columnname, it aligns all data to the given direction in the table. This looks nice on numerical displays in a column. The column names in the table will be unaffected by the alignment. Possible directions is: left, center, right, justify, auto or your own subroutine. (Hint: Using auto(default), aligns numbers right and text left)
Given a columnname, it aligns the columnname in the row explaining columnnames, to the given direction. (auto,left,right,center,justify or a subroutine) (Hint: Overrides the 'alignHeadRow' option for the specified column.)
Wordwrapping/strict size. Set a max-width(in chars) for a column. If last parameter is 1, the column will be set to the specified width, even if no text is that long.
Usage: $t->setColWidth('Description',30);
If you need to know how wide your table will be before you draw it. Use this function.
Use this to set options like: hide_FirstLine,reportErrors, etc.
Usage: $t->setOptions('hide_HeadLine',1);
Or set more than one option on the fly: $t->setOptions({ hide_HeadLine => 1, hide_HeadRow => 1 });
Possible Options
Hides output of the columnlisting. Together with hide_HeadLine, this makes a table only show the rows. (However, even though the column-names will not be shown, they will affect the output if they have for example ridiculoustly long names, and the rows contains small amount of info. You would end up with a lot of whitespace)
Set to 0 to disable error reporting. Though if a function encounters an error, it will still return the value 1, to tell you that things didn't go exactly as they should.
If you are going to use Text::ASCIITable to be shown on \s-1HTML\s0 pages, you should set this option to 1 when you are going to use \s-1HTML\s0 tags to for example color the text inside the rows, and you want the browser to handle the table correct.
If you use \s-1ANSI\s0 codes like <\s-1ESC\s0>[1mHi this is bold<\s-1ESC\s0>[m or similar. This option will make the table to be displayed correct when showed in a \s-1ANSI\s0 compliant terminal. Set this to 1 to enable. There is an example of \s-1ANSI\s0 support in this package, named ansi-example.pl.
Set wich direction the Column-names(in the headrow) are supposed to point. Must be left, right, center, justify, auto or a user-defined subroutine.
Speaks for it self?
Set this to 1 to print a line between each row. You can also define the outputstyle of this line in the draw() function.
Add a heading above the columnnames/rows wich uses the whole width of the table to output a heading/title to the table. The heading-part of the table is automatically shown when the headingText option contains text. Note: If this text is so long that it makes the table wider, it will not hesitate to change width of columns that have \*(L"strict width\*(R". It supports multiline, and with Text::ASCIITable::Wrap you may wrap your text before entering it, to prevent the title from expanding the table. Internal wrapping-support for headingText might come in the future.
Align the heading(as mentioned above) to left, right, center, auto or using a subroutine.
Choose the startingchar and endingchar of the row where the title is. The default is '|' on both. If you didn't understand this, try reading about the draw() function.
Set the callback subroutine to use when counting characters inside the table. This is useful to make support for having characters or codes inside the table that are not shown on the screen to the user, so the table should not count these characters. This could be for example \s-1HTML\s0 tags, or \s-1ANSI\s0 codes. Though those two examples are alredy supported internally with the allowHTML and allowANSI, options. This option expects a \s-1CODE\s0 reference. (\&callback_function)
Sets the replacing string that replaces an undef value sent to addRow() (or even the overloaded push version of addRow()). The default value is an empty string ''. An example of use would be to set it to '(undef)', to show that the input really was undefined.
Set this to 1 to support chainging of methods. The default is 0, where the methods return 1 if they come upon an error as mentioned in the reportErrors option description. Usage example: print Text::ASCIITable->new({ chaining => 1 }) ->setCols('One','Two','Three') ->addRow([ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ], ]) ->draw(); Note that ->draw() can be omitted, since Text::ASCIITable is overloaded to print the table by default.
All the arrays containing the layout is optional. If you want to make your own \*(L"design\*(R" to the table, you can do that by giving this method these arrays containing information about which characters to use where.
Custom tables
The draw method takes 6 arrays of strings to define the layout. The first, third, fifth and sixth is \s-1LINE\s0 layout and the second and fourth is \s-1ROW\s0 layout. The \*(C`fourth\*(C' parameter is repeated for each row in the table. The sixth parameter is only used if drawRowLine is enabled.
$t->draw(<LINE>,<ROW>,<LINE>,<ROW>,<LINE>,[<ROWLINE>])
Takes an array of 4 strings. For example \*(C`['|','|','-','+']\*(C'
\s-1LEFT\s0 - Defines the left chars. May be more than one char.
\s-1RIGHT\s0 - Defines the right chars. May be more then one char.
\s-1LINE\s0 - Defines the char used for the line. Must be only one char.
\s-1DELIMETER\s0 - Defines the char used for the delimeters. Must be only one char.
Takes an array of 3 strings. You should not give more than one char to any of these parameters, if you do.. it will probably destroy the output.. Unless you do it with the knowledge of how it will end up. An example: \*(C`['|','|','+']\*(C'
\s-1LEFT\s0 - Define the char used for the left side of the table.
\s-1RIGHT\s0 - Define the char used for the right side of the table.
\s-1DELIMETER\s0 - Defines the char used for the delimeters.
Examples:
The easiest way:
print $t;
Explanatory example:
print $t->draw( ['L','R','l','D'], # LllllllDllllllR ['L','R','D'], # L info D info R ['L','R','l','D'], # LllllllDllllllR ['L','R','D'], # L info D info R ['L','R','l','D'] # LllllllDllllllR );
Nice example:
print $t->draw( ['.','.','-','-'], # .-------------. ['|','|','|'], # | info | info | ['|','|','-','-'], # |-------------| ['|','|','|'], # | info | info | [' \\','/ ','_','|'] # \_\|_\|_\|_\|_|_\|_\|_\|_\|_/ );
Nice example2:
print $t->draw( ['.=','=.','-','-'], # .=-----------=. ['|','|','|'], # | info | info | ['|=','=|','-','+'], # |=-----+-----=| ['|','|','|'], # | info | info | ["'=","='",'-','-'] # '=-----------=' );
With Options:
$t->setOptions('drawRowLine',1); print $t->draw( ['.=','=.','-','-'], # .=-----------=. ['|','|','|'], # | info | info | ['|-','-|','=','='], # |-===========-| ['|','|','|'], # | info | info | ["'=","='",'-','-'], # '=-----------=' ['|=','=|','-','+'] # rowseperator ); Which makes this output: .=-----------=. | col1 | col2 | |-===========-| | info | info | |=-----+-----=| <-- rowseperator between each row | info | info | '=-----------='
A tips is to enable allowANSI, and use the extra charset in your terminal to create a beautiful table. But don't expect to get good results if you use ANSI-formatted table with $t->drawPage.
User-defined subroutines for aligning
If you want to format your text more throughoutly than \*(L"auto\*(R", or think you have a better way of aligning text; you can make your own subroutine.
Here's a exampleroutine that aligns the text to the right.
sub myownalign_cb { my ($text,$length,$count,$strict) = @_; $text = (" " x ($length - $count)) . $text; return substr($text,0,$length) if ($strict); return $text; }
$t->alignCol('Info',\&myownalign_cb);
User-defined subroutines for counting
This is a feature to use if you are not happy with the internal allowHTML or allowANSI support. Given is an example of how you make a count-callback that makes ASCIITable support \s-1ANSI\s0 codes inside the table. (would make the same result as setting allowANSI to 1)
$t->setOptions('cb_count',\&myallowansi_cb); sub myallowansi_cb { $_=shift; s/\33\[(\d+(;\d+)?)?[musfwhojBCDHRJK]//g; return length($_); }
If you don't want your table to be wider than your screen you can use this with $t->setOptions('outputWidth',40) to set the max size of the output.
Example:
$t->setOptions('outputWidth',80); for my $page (1..$t->pageCount()) { print $t->drawPage($page)."\n"; print "continued..\n\n"; }
In case you need to know if this module has what you need, I have made this list of features included in Text::ASCIITable.
You can easily alter how the table should look, in many ways. There are a few examples in the draw() section of this documentation. And you can remove parts of the layout or even add a heading-part to the table.
Align the text in a column auto(matically), left, right, center or justify. Usually you want to align text to right if you only have numbers in that row. The 'auto' direction aligns text to left, and numbers to the right. The 'justify' alignment evens out your text on each line, so the first and the last word always are at the beginning and the end of the current line. This gives you the newspaper paragraph look. You can also use your own subroutine as a callback-function to align your text.
With the \n(ewline) character you can have rows use more than just one line on the output. (This looks nice with the drawRowLine option enabled)
You can set a column to not be wider than a set amount of characters. If a line exceedes for example 30 characters, the line will be broken up in several lines.
If you put in <\s-1HTML\s0> tags inside the rows, the output would usually be broken when viewed in a browser, since the browser \*(L"execute\*(R" the tags instead of displaying it. But if you enable allowHTML. You are able to write html tags inside the rows without the output being broken if you display it in a browser. But you should not mix this with wordwrap, since this could make undesirable results.
Allows you to decorate your tables with colors or bold/underline when you display your tables to a terminal window.
If you don't want the table to get wider than your terminal-width.
If you write a script in perl, and don't want users to be notified of the errormessages from Text::ASCIITable. You can easily turn of error reporting by setting reportErrors to 0. You will still get an 1 instead of undef returned from the function.
Exporter, Carp
Ha\*okon Nessjo\*/en, <[email protected]>
Current version is 0.20.
Copyright 2002-2011 by Ha\*okon Nessjo\*/en. All rights reserved. This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Text::FormatTable, Text::Table, Text::SimpleTable