|
NAMEText::ASCIITable - Create a nice formatted table using ASCII characters.SHORT DESCRIPTIONPretty 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 "cool" way.SYNOPSISuse 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 | '----+-----------------+-------' FUNCTIONSnew(options)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}); setCols(@cols)Define the columns for the table(compare with <TH> in HTML). For example "setCols(['Id','Nick','Name'])". Note that you cannot add Cols after you have added a row. Multiline columnnames are allowed.addRow(@collist)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 ], ]); addRowLine([$row])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. (HINT: If you want a line after every row, read about the drawRowLine option in setOptions())Example without arguments: $t->addRow('one','two','three'); $t->addRowLine(); $t->addRow('one','two','three'); Example with argument: $t->addRow('one','two','three'); $t->addRow('one','two','three'); $t->addRow('one','two','three'); $t->addRow('one','two','three'); $t->addRowLine(1); # or multiple: $t->addRowLine([2,3]); alignCol($col,$direction) or alignCol({col1 => direction1, col2 => direction2, ... })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)alignColName($col,$direction)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.)setColWidth($col,$width,$strict)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); getTableWidth()If you need to know how wide your table will be before you draw it. Use this function.setOptions(name,value) or setOptions({ option1 => value1, option2 => value2, ... })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
draw([@topdesign,@toprow,@middle,@middlerow,@bottom,@rowline])All the arrays containing the layout is optional. If you want to make your own "design" 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 LINE layout and the second and fourth is ROW layout. The "fourth" 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>])
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 "auto", 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 ANSI 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($_); } drawPage($page,@topdesign,@toprow,@middle,@middlerow,@bottom,@rowline)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"; } FEATURESIn case you need to know if this module has what you need, I have made this list of features included in Text::ASCIITable.
REQUIRESExporter, CarpAUTHORHaakon Nessjoen, <lunatic@cpan.org>VERSIONCurrent version is 0.22.COPYRIGHTCopyright 2002-2011 by Haakon Nessjoen. All rights reserved. This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.SEE ALSOText::FormatTable, Text::Table, Text::SimpleTable
Visit the GSP FreeBSD Man Page Interface. |