Text-TabularDisplay-1.38/000755 025721 000024 00000000000 12356524101 015723 5ustar00darrenstaff000000 000000 Text-TabularDisplay-1.38/COPYING000644 025721 000024 00000043122 12355277445 016777 0ustar00darrenstaff000000 000000 GNU GENERAL PUBLIC LICENSE Version 2, June 1991 Copyright (C) 1989, 1991 Free Software Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The licenses for most software are designed to take away your freedom to share and change it. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. This General Public License applies to most of the Free Software Foundation's software and to any other program whose authors commit to using it. (Some other Free Software Foundation software is covered by the GNU Library General Public License instead.) You can apply it to your programs, too. When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for this service if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things. To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it. For example, if you distribute copies of such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights. We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software. Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations. Finally, any free program is threatened constantly by software patents. We wish to avoid the danger that redistributors of a free program will individually obtain patent licenses, in effect making the program proprietary. To prevent this, we have made it clear that any patent must be licensed for everyone's free use or not licensed at all. The precise terms and conditions for copying, distribution and modification follow. GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any derivative work under copyright law: that is to say, a work containing the Program or a portion of it, either verbatim or with modifications and/or translated into another language. (Hereinafter, translation is included without limitation in the term "modification".) Each licensee is addressed as "you". Activities other than copying, distribution and modification are not covered by this License; they are outside its scope. The act of running the Program is not restricted, and the output from the Program is covered only if its contents constitute a work based on the Program (independent of having been made by running the Program). Whether that is true depends on what the Program does. 1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this License and to the absence of any warranty; and give any other recipients of the Program a copy of this License along with the Program. You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. 2. You may modify your copy or copies of the Program or any portion of it, thus forming a work based on the Program, and copy and distribute such modifications or work under the terms of Section 1 above, provided that you also meet all of these conditions: a) You must cause the modified files to carry prominent notices stating that you changed the files and the date of any change. b) You must cause any work that you distribute or publish, that in whole or in part contains or is derived from the Program or any part thereof, to be licensed as a whole at no charge to all third parties under the terms of this License. c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the most ordinary way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this License. (Exception: if the Program itself is interactive but does not normally print such an announcement, your work based on the Program is not required to print an announcement.) These requirements apply to the modified work as a whole. If identifiable sections of that work are not derived from the Program, and can be reasonably considered independent and separate works in themselves, then this License, and its terms, do not apply to those sections when you distribute them as separate works. But when you distribute the same sections as part of a whole which is a work based on the Program, the distribution of the whole must be on the terms of this License, whose permissions for other licensees extend to the entire whole, and thus to each and every part regardless of who wrote it. Thus, it is not the intent of this section to claim rights or contest your rights to work written entirely by you; rather, the intent is to exercise the right to control the distribution of derivative or collective works based on the Program. In addition, mere aggregation of another work not based on the Program with the Program (or with a work based on the Program) on a volume of a storage or distribution medium does not bring the other work under the scope of this License. 3. You may copy and distribute the Program (or a work based on it, under Section 2) in object code or executable form under the terms of Sections 1 and 2 above provided that you also do one of the following: a) Accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, b) Accompany it with a written offer, valid for at least three years, to give any third party, for a charge no more than your cost of physically performing source distribution, a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, c) Accompany it with the information you received as to the offer to distribute corresponding source code. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form with such an offer, in accord with Subsection b above.) The source code for a work means the preferred form of the work for making modifications to it. For an executable work, complete source code means all the source code for all modules it contains, plus any associated interface definition files, plus the scripts used to control compilation and installation of the executable. However, as a special exception, the source code distributed need not include anything that is normally distributed (in either source or binary form) with the major components (compiler, kernel, and so on) of the operating system on which the executable runs, unless that component itself accompanies the executable. If distribution of executable or object code is made by offering access to copy from a designated place, then offering equivalent access to copy the source code from the same place counts as distribution of the source code, even though third parties are not compelled to copy the source along with the object code. 4. You may not copy, modify, sublicense, or distribute the Program except as expressly provided under this License. Any attempt otherwise to copy, modify, sublicense or distribute the Program is void, and will automatically terminate your rights under this License. However, parties who have received copies, or rights, from you under this License will not have their licenses terminated so long as such parties remain in full compliance. 5. You are not required to accept this License, since you have not signed it. However, nothing else grants you permission to modify or distribute the Program or its derivative works. These actions are prohibited by law if you do not accept this License. Therefore, by modifying or distributing the Program (or any work based on the Program), you indicate your acceptance of this License to do so, and all its terms and conditions for copying, distributing or modifying the Program or works based on it. 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. You are not responsible for enforcing compliance by third parties to this License. 7. If, as a consequence of a court judgment or allegation of patent infringement or for any other reason (not limited to patent issues), conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot distribute so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not distribute the Program at all. For example, if a patent license would not permit royalty-free redistribution of the Program by all those who receive copies directly or indirectly through you, then the only way you could satisfy both it and this License would be to refrain entirely from distribution of the Program. If any portion of this section is held invalid or unenforceable under any particular circumstance, the balance of the section is intended to apply and the section as a whole is intended to apply in other circumstances. It is not the purpose of this section to induce you to infringe any patents or other property right claims or to contest validity of any such claims; this section has the sole purpose of protecting the integrity of the free software distribution system, which is implemented by public license practices. Many people have made generous contributions to the wide range of software distributed through that system in reliance on consistent application of that system; it is up to the author/donor to decide if he or she is willing to distribute software through any other system and a licensee cannot impose that choice. This section is intended to make thoroughly clear what is believed to be a consequence of the rest of this License. 8. If the distribution and/or use of the Program is restricted in certain countries either by patents or by copyrighted interfaces, the original copyright holder who places the Program under this License may add an explicit geographical distribution limitation excluding those countries, so that distribution is permitted only in or among countries not thus excluded. In such case, this License incorporates the limitation as if written in the body of this License. 9. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies a version number of this License which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of this License, you may choose any version ever published by the Free Software Foundation. 10. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Also add information on how to contact you by electronic and paper mail. If the program is interactive, make it output a short notice like this when it starts in an interactive mode: Gnomovision version 69, Copyright (C) year name of author Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, the commands you use may be called something other than `show w' and `show c'; they could even be mouse-clicks or menu items--whatever suits your program. You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the program, if necessary. Here is a sample; alter the names: Yoyodyne, Inc., hereby disclaims all copyright interest in the program `Gnomovision' (which makes passes at compilers) written by James Hacker. , 1 April 1989 Ty Coon, President of Vice This General Public License does not permit incorporating your program into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Library General Public License instead of this License. Text-TabularDisplay-1.38/examples/000755 025721 000024 00000000000 12356524101 017541 5ustar00darrenstaff000000 000000 Text-TabularDisplay-1.38/Makefile.PL000644 025721 000024 00000000762 12355277445 017721 0ustar00darrenstaff000000 000000 package Text::TabularDisplay; use strict; use vars qw(%PREREQ_PM @EXE_FILES %clean); use ExtUtils::MakeMaker; %PREREQ_PM = (); %clean = ('FILES' => '$(DISTVNAME).tar$(SUFFIX)'); WriteMakefile( NAME => __PACKAGE__, AUTHOR => 'darren chamberlain ', LICENSE => 'gpl', ABSTRACT_FROM => 'TabularDisplay.pm', VERSION_FROM => "TabularDisplay.pm", PREREQ_PM => \%PREREQ_PM, clean => \%clean, ); __END__ Text-TabularDisplay-1.38/MANIFEST000644 025721 000024 00000000527 12356524101 017060 0ustar00darrenstaff000000 000000 COPYING examples/aliases examples/dump-passwd.pl examples/mysql.pl examples/tdisplay Makefile.PL MANIFEST MANIFEST.SKIP README t/01.t t/02.t t/03.t t/04.t t/05.t t/06.t t/07.t t/08.t t/09.t t/10.t t/11.t t/12.t t/13.t t/14.t t/15.t t/16.t t/17.t TabularDisplay.pm META.yml Module meta-data (added by MakeMaker) Text-TabularDisplay-1.38/MANIFEST.SKIP000644 025721 000024 00000000024 12355277445 017634 0ustar00darrenstaff000000 000000 \.git \.travis\.yml Text-TabularDisplay-1.38/META.yml000644 025721 000024 00000001045 12356524101 017174 0ustar00darrenstaff000000 000000 --- #YAML:1.0 name: Text-TabularDisplay version: 1.38 abstract: Display text in formatted table output author: - darren chamberlain license: gpl distribution_type: module configure_requires: ExtUtils::MakeMaker: 0 build_requires: ExtUtils::MakeMaker: 0 requires: {} no_index: directory: - t - inc generated_by: ExtUtils::MakeMaker version 6.56 meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: 1.4 Text-TabularDisplay-1.38/README000644 025721 000024 00000020231 12355516134 016607 0ustar00darrenstaff000000 000000 NAME Text::TabularDisplay - Display text in formatted table output SYNOPSIS use Text::TabularDisplay; my $table = Text::TabularDisplay->new(@columns); $table->add(@row) while (@row = $sth->fetchrow); print $table->render; +----+--------------+ | id | name | +----+--------------+ | 1 | Tom | | 2 | Dick | | 3 | Barry | | | (aka Bazza) | | 4 | Harry | +----+--------------+ DESCRIPTION Text::TabularDisplay simplifies displaying textual data in a table. The output is identical to the columnar display of query results in the mysql text monitor. For example, this data: 1, "Tom Jones", "(666) 555-1212" 2, "Barnaby Jones", "(666) 555-1213" 3, "Bridget Jones", "(666) 555-1214" Used like so: my $t = Text::TabularDisplay->new(qw(id name phone)); $t->add(1, "Tom Jones", "(666) 555-1212"); $t->add(2, "Barnaby Jones", "(666) 555-1213"); $t->add(3, "Bridget Jones", "(666) 555-1214"); print $t->render; Produces: +----+---------------+----------------+ | id | name | phone | +----+---------------+----------------+ | 1 | Tom Jones | (666) 555-1212 | | 2 | Barnaby Jones | (666) 555-1213 | | 3 | Bridget Jones | (666) 555-1214 | +----+---------------+----------------+ METHODS Text::TabularDisplay has four primary methods: new(), columns(), add(), and render(). new() creates a new Text::TabularDisplay instance; columns() sets the column headers in the output table; add() adds data to the instance; and render() returns a formatted string representation of the instance. There are also a few auxiliary convenience methods: clone(), items(), reset(), populate(), and paginate(). new A Text::TabularDisplay instance can be created with column names passed as constructor args, so these two calls produce similar objects: my $t1 = Text::TabularDisplay->new; $t1->columns(qw< one two >); my $t2 = Text::TabularDisplay->new(qw< one two >); Calling new() on a Text::TabularDisplay instance returns a clone of the object. See "clone" in Text::TabularDisplay. columns Gets or sets the column names for an instance. This method is called automatically by the constructor with any parameters that are passed to the constructor (if any are passed). When called in scalar context, columns() returns the *number of columns in the instance*, rather than the columns themselves. In list context, copies of the columns names are returned; the names of the columns cannot be modified this way. add Takes a list of items and appends it to the list of items to be displayed. add() can also take a reference to an array, so that large arrays don't need to be copied. As elements are processed, add() maintains the width of each column so that the resulting table has the correct dimensions. add() returns $self, so that calls to add() can be chained: $t->add(@one)->add(@two)->add(@three); render render() does most of the actual work. It returns a string containing the data added via add(), formatted as a table, with a header containing the column names. render() does not change the state of the object; it can be called multiple times, with identical output (including identical running time: the output of render is not cached). If there are no columns defined, then the output table does not contains a row of column names. Compare these two sequences: my $t = Text::TabularDisplay->new; $t->add(qw< 1 2 3 4 >); $t->add(qw< 5 6 7 8 >); print $t->render; $t->columns(qw< one two three four >); print $t->render; # Example 1 output +---+---+---+---+ | 1 | 2 | 3 | 4 | | 5 | 6 | 7 | 8 | +---+---+---+---+ # Example 2 output +-----+-----+-------+------+ | one | two | three | four | +-----+-----+-------+------+ | 1 | 2 | 3 | 4 | | 5 | 6 | 7 | 8 | +-----+-----+-------+------+ render() takes optional $start and $end arguments; these indicate the start and end *indexes* for the data to be rendered. This can be used for paging and the like: $t->add(1, 2, 3)->add(4, 5, 6)->add(7, 8, 9)->add(10, 11, 12); print $t->render(0, 1), "\n"; print $t->render(2, 3), "\n"; Produces: +-------+--------+-------+ | First | Second | Third | +-------+--------+-------+ | 1 | 2 | 3 | | 4 | 5 | 6 | +-------+--------+-------+ +-------+--------+-------+ | First | Second | Third | +-------+--------+-------+ | 7 | 8 | 9 | | 10 | 11 | 12 | +-------+--------+-------+ As an aside, note the chaining of calls to add(). The elements in the table are padded such that there is the same number of items in each row, including the header. Thus: $t->columns(qw< One Two >); print $t->render; +-----+-----+----+ | One | Two | | +-----+-----+----+ | 1 | 2 | 3 | | 4 | 5 | 6 | | 7 | 8 | 9 | | 10 | 11 | 12 | +-----+-----+----+ And: $t->columns(qw< One Two Three Four>); print $t->render; +-----+-----+-------+------+ | One | Two | Three | Four | +-----+-----+-------+------+ | 1 | 2 | 3 | | | 4 | 5 | 6 | | | 7 | 8 | 9 | | | 10 | 11 | 12 | | +-----+-----+-------+------+ OTHER METHODS clone() The clone() method returns an identical copy of a Text::TabularDisplay instance, completely separate from the cloned instance. items() The items() method returns the number of elements currently stored in the data structure: printf "There are %d elements in \$t.\n", $t->items; reset() Reset deletes the data from the instance, including columns. If passed arguments, it passes them to columns(), just like new(). populate() populate() as a special case of add(); populate() expects a reference to an array of references to arrays, such as returned by DBI's selectall_arrayref method: $sql = "SELECT " . join(", ", @c) . " FROM mytable"; $t->columns(@c); $t->populate($dbh->selectall_arrayref($sql)); This is for convenience only; the implementation maps this to multiple calls to add(). NOTES / ISSUES Text::TabularDisplay assumes it is handling strings, and does stringy things with the data, like length() and sprintf(). Non-character data can be passed in, of course, but will be treated as strings; this may have ramifications for objects that implement overloading. The biggest issue, though, is that this module duplicates a some of the functionality of Data::ShowTable. Of course, Data::ShowTable is a large, complex monolithic tool that does a lot of things, while Text::TabularDisplay is small and fast. AUTHOR darren chamberlain CREDITS The following people have contributed patches, suggestions, tests, feedback, or good karma: David N. Blank-Edelman Eric Cholet Ken Youens-Clark Michael Fowler Paul Cameron Prakash Kailasa Slaven Rezic Harlan Lieberman-Berg Patrick Kuijvenhoven Miko O'Sullivan VERSION This documentation describes "Text::TabularDisplay" version 1.37. Text-TabularDisplay-1.38/t/000755 025721 000024 00000000000 12356524101 016166 5ustar00darrenstaff000000 000000 Text-TabularDisplay-1.38/TabularDisplay.pm000644 025721 000024 00000036564 12356523675 021236 0ustar00darrenstaff000000 000000 package Text::TabularDisplay; # ------------------------------------------------------------------- # Text::TabularDisplay - Display text in formatted table output # Copyright (C) 2004-2014 darren chamberlain # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License as # published by the Free Software Foundation; version 2. # # This program is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA # 02110-1301 USA # ------------------------------------------------------------------- use strict; use integer; use vars qw($VERSION); $VERSION = "1.38"; # ---======================= Public Methods ======================--- # ------------------------------------------------------------------- # new([@columns]) # # Instantiate a new Text::TabularDisplay instance. Optionally takes # column names, which are passed to the columns method. # ------------------------------------------------------------------- sub new { my $class = shift; return $class->clone if ref $class && UNIVERSAL::isa($class, __PACKAGE__); my $self = bless { _COLUMNS => [ ], _DATA => [ ], _LENGTHS => [ ], _SIZE => 0, } => $class; $self->columns(@_) if (@_); return $self; } # ------------------------------------------------------------------- # clone # # Clones a Text::TabluarDisplay instance. # ------------------------------------------------------------------- sub clone { my $self = shift; my $class = ref $self || return $self->new; my $clone = $class->new($self->columns); for (@{ $self->{ _DATA } }) { $clone->add(@$_); } return $clone; } # ------------------------------------------------------------------- # columns([@columns]) # # Returns a list of column names in list context, but returns the # number of columns in scalar context. # ------------------------------------------------------------------- sub columns { my $self = shift; my @columns; if (@_) { my $cnum = $self->{ _SIZE }; if ($cnum > scalar @_) { push @_, "" while ($self->columns > scalar @_); } @{ $self->{ _COLUMNS } } = (); $self->_add($self->{ _COLUMNS }, $self->{ _LENGTHS }, \$self->{ _SIZE }, [ @_ ]); } @columns = @{ $self->{ _COLUMNS }->[0] || [ ]}; return wantarray ? @columns : scalar @columns; } # ------------------------------------------------------------------- # add(@data) # # Adds a row to the instance. Returns $self, for chaining: # $self->add(@one)->add(@two)->add(@three); # ------------------------------------------------------------------- sub add { my $self = shift; my $add = UNIVERSAL::isa($_[0], 'ARRAY') ? shift : [ @_ ]; if (@$add) { $self->_add($self->{ _DATA }, $self->{ _LENGTHS }, \$self->{ _SIZE }, $add); } return $self; } # ------------------------------------------------------------------- # render([$start, $end]) # # Returns the data formatted as a table. By default, all rows are # returned; if $start or $end are specified, then only those indexes # are returned. Those are the start and end indexes! # ------------------------------------------------------------------- sub render { my $self = shift; my $start = shift || 0; my $end = shift || $#{ $self->{ _DATA } }; my $size = $self->{ _SIZE }; my ($bar, @columns, $datum, @text); $bar = join "+", "", map( { "-" x ($_ + 2) } @{ $self->{ _LENGTHS } }), ""; push @text, $bar; if (@columns = $self->columns) { push @text, _format_line(\@columns, $self->{ _LENGTHS }); push @text, $bar; } for (my $i = $start; $i <= $end; $i++) { $datum = $self->{ _DATA }->[$i]; last unless defined $datum; # Pad the array if there are more elements in @columns push @$datum, "" until (@$datum == $size); push @text, _format_line($datum, $self->{ _LENGTHS }); } push @text, $bar; return join "\n", @text; } sub items { my $self = shift; return scalar @{ $self->{ _DATA } }; } # ---------------------------------------------------------------------- # reset() # # Resets the instance. # ---------------------------------------------------------------------- sub reset { my $self = shift; @{ $self->{ _COLUMNS } } = (); @{ $self->{ _LENGTHS } } = (); @{ $self->{ _DATA } } = (); $self->columns(@_) if (@_); return $self; } # ---------------------------------------------------------------------- # populate(\@data) # # populate() takes a reference to an array of references to arrays, # and calls add() repeatedly. Primarily for use with DBI's # selectall_arrayref() method. # ---------------------------------------------------------------------- sub populate { my $self = shift; (@_) or return $self; my $data = UNIVERSAL::isa($_[0], 'ARRAY') ? shift : [ @_ ]; for (my $i = 0; $i <= $#$data; $i++) { $self->add($data->[$i]); } return $self; } # ---------------------------------------------------------------------- # paginate($items_per_page) # # Returns a list of rendered pages, with $items_per_page - 4 elements # on each (operational overhead) # ---------------------------------------------------------------------- sub paginate { my $self = shift; my $items_per_page = shift || 62; my ($items, $pages, $current, @pages); $items = $self->items; $pages = $items / $items_per_page; $pages += 1 if $items % $items_per_page; for (my $i = 0; $i < $pages; $i++) { push @pages, $self->render($current, $items_per_page); $current += $items_per_page; } return @pages; } # ---====================== Private Methods ======================--- # ------------------------------------------------------------------- # _add(\@where, \@lengths, \@add) # # Adds @add to @where and modifies @lengths, as necessary # ------------------------------------------------------------------- sub _add { my $self = shift; my ($where, $length, $size, $add) = @_; my @data; $$size = scalar @$add if (scalar @$add > $$size); for (my $i = 0; $i <= $#$add; $i++) { my $l = _column_length($add->[$i]); push @data, $add->[$i]; $length->[$i] = $l unless $length->[$i] && $length->[$i] > $l; } push @$where, \@data; } # ------------------------------------------------------------------- # _format_line(\@columns, \@lengths) # # Returns a formatted line out of @columns; the size of $column[$i] # is determined by $length[$i]. # ------------------------------------------------------------------- sub _format_line { my ($columns, $lengths) = @_; my $height = 0; my @col_lines; for (@$columns) { my @lines = split "\n", ((defined $_ && length $_) ? $_ : ' '); $height = scalar @lines if $height < @lines; push @col_lines, \@lines; } my @lines; for my $h (0 .. $height - 1 ) { my @line; for (my $i = 0; $i <= $#$columns; $i++) { my $val = defined($col_lines[$i][$h]) ? $col_lines[$i][$h] : ''; push @line, sprintf " %-" . $lengths->[$i] . "s ", $val; } push @lines, join '|', "", @line, ""; } return join "\n", @lines; } # ------------------------------------------------------------------- # _column_length($str) # ------------------------------------------------------------------- sub _column_length { my ($str) = @_; $str = '' unless defined $str; my $len = 0; for (split "\n", $str) { $len = length if $len < length; } # untaint $len unless ($len =~ m|^(\d+)$|s) { die 'invalid length: ' . $len } $len = $1; return $len; } 1; __END__ =head1 NAME Text::TabularDisplay - Display text in formatted table output =head1 SYNOPSIS use Text::TabularDisplay; my $table = Text::TabularDisplay->new(@columns); $table->add(@row) while (@row = $sth->fetchrow); print $table->render; +----+--------------+ | id | name | +----+--------------+ | 1 | Tom | | 2 | Dick | | 3 | Barry | | | (aka Bazza) | | 4 | Harry | +----+--------------+ =head1 DESCRIPTION Text::TabularDisplay simplifies displaying textual data in a table. The output is identical to the columnar display of query results in the mysql text monitor. For example, this data: 1, "Tom Jones", "(666) 555-1212" 2, "Barnaby Jones", "(666) 555-1213" 3, "Bridget Jones", "(666) 555-1214" Used like so: my $t = Text::TabularDisplay->new(qw(id name phone)); $t->add(1, "Tom Jones", "(666) 555-1212"); $t->add(2, "Barnaby Jones", "(666) 555-1213"); $t->add(3, "Bridget Jones", "(666) 555-1214"); print $t->render; Produces: +----+---------------+----------------+ | id | name | phone | +----+---------------+----------------+ | 1 | Tom Jones | (666) 555-1212 | | 2 | Barnaby Jones | (666) 555-1213 | | 3 | Bridget Jones | (666) 555-1214 | +----+---------------+----------------+ =head1 METHODS Text::TabularDisplay has four primary methods: new(), columns(), add(), and render(). new() creates a new Text::TabularDisplay instance; columns() sets the column headers in the output table; add() adds data to the instance; and render() returns a formatted string representation of the instance. There are also a few auxiliary convenience methods: clone(), items(), reset(), populate(), and paginate(). =over =item B A Text::TabularDisplay instance can be created with column names passed as constructor args, so these two calls produce similar objects: my $t1 = Text::TabularDisplay->new; $t1->columns(qw< one two >); my $t2 = Text::TabularDisplay->new(qw< one two >); Calling new() on a Text::TabularDisplay instance returns a clone of the object. See L. =item B Gets or sets the column names for an instance. This method is called automatically by the constructor with any parameters that are passed to the constructor (if any are passed). When called in scalar context, columns() returns the I, rather than the columns themselves. In list context, copies of the columns names are returned; the names of the columns cannot be modified this way. =item B Takes a list of items and appends it to the list of items to be displayed. add() can also take a reference to an array, so that large arrays don't need to be copied. As elements are processed, add() maintains the width of each column so that the resulting table has the correct dimensions. add() returns $self, so that calls to add() can be chained: $t->add(@one)->add(@two)->add(@three); =item B render() does most of the actual work. It returns a string containing the data added via add(), formatted as a table, with a header containing the column names. render() does not change the state of the object; it can be called multiple times, with identical output (including identical running time: the output of render is not cached). If there are no columns defined, then the output table does not contains a row of column names. Compare these two sequences: my $t = Text::TabularDisplay->new; $t->add(qw< 1 2 3 4 >); $t->add(qw< 5 6 7 8 >); print $t->render; $t->columns(qw< one two three four >); print $t->render; # Example 1 output +---+---+---+---+ | 1 | 2 | 3 | 4 | | 5 | 6 | 7 | 8 | +---+---+---+---+ # Example 2 output +-----+-----+-------+------+ | one | two | three | four | +-----+-----+-------+------+ | 1 | 2 | 3 | 4 | | 5 | 6 | 7 | 8 | +-----+-----+-------+------+ render() takes optional $start and $end arguments; these indicate the start and end I for the data to be rendered. This can be used for paging and the like: $t->add(1, 2, 3)->add(4, 5, 6)->add(7, 8, 9)->add(10, 11, 12); print $t->render(0, 1), "\n"; print $t->render(2, 3), "\n"; Produces: +-------+--------+-------+ | First | Second | Third | +-------+--------+-------+ | 1 | 2 | 3 | | 4 | 5 | 6 | +-------+--------+-------+ +-------+--------+-------+ | First | Second | Third | +-------+--------+-------+ | 7 | 8 | 9 | | 10 | 11 | 12 | +-------+--------+-------+ As an aside, note the chaining of calls to add(). The elements in the table are padded such that there is the same number of items in each row, including the header. Thus: $t->columns(qw< One Two >); print $t->render; +-----+-----+----+ | One | Two | | +-----+-----+----+ | 1 | 2 | 3 | | 4 | 5 | 6 | | 7 | 8 | 9 | | 10 | 11 | 12 | +-----+-----+----+ And: $t->columns(qw< One Two Three Four>); print $t->render; +-----+-----+-------+------+ | One | Two | Three | Four | +-----+-----+-------+------+ | 1 | 2 | 3 | | | 4 | 5 | 6 | | | 7 | 8 | 9 | | | 10 | 11 | 12 | | +-----+-----+-------+------+ =back =head1 OTHER METHODS =over 4 =item clone() The clone() method returns an identical copy of a Text::TabularDisplay instance, completely separate from the cloned instance. =item items() The items() method returns the number of elements currently stored in the data structure: printf "There are %d elements in \$t.\n", $t->items; =item reset() Reset deletes the data from the instance, including columns. If passed arguments, it passes them to columns(), just like new(). =item populate() populate() as a special case of add(); populate() expects a reference to an array of references to arrays, such as returned by DBI's selectall_arrayref method: $sql = "SELECT " . join(", ", @c) . " FROM mytable"; $t->columns(@c); $t->populate($dbh->selectall_arrayref($sql)); This is for convenience only; the implementation maps this to multiple calls to add(). =back =head1 NOTES / ISSUES Text::TabularDisplay assumes it is handling strings, and does stringy things with the data, like length() and sprintf(). Non-character data can be passed in, of course, but will be treated as strings; this may have ramifications for objects that implement overloading. The biggest issue, though, is that this module duplicates a some of the functionality of Data::ShowTable. Of course, Data::ShowTable is a large, complex monolithic tool that does a lot of things, while Text::TabularDisplay is small and fast. =head1 AUTHOR darren chamberlain Edarren@cpan.orgE =head1 CREDITS The following people have contributed patches, suggestions, tests, feedback, or good karma: David N. Blank-Edelman Eric Cholet Ken Youens-Clark Michael Fowler Paul Cameron Prakash Kailasa Slaven Rezic Harlan Lieberman-Berg Patrick Kuijvenhoven Miko O'Sullivan =head1 VERSION This documentation describes C version 1.38. Text-TabularDisplay-1.38/t/01.t000644 025721 000024 00000001706 12355277445 016616 0ustar00darrenstaff000000 000000 #!/usr/bin/perl # vim: set ft=perl: use strict; use Text::TabularDisplay; use Test; BEGIN { plan tests => 4; } my @data = ( [ qw(id name phone) ], [ 1, "Tom Jones", "(666) 555-1212" ], [ 2, "Barnaby Jones", "(666) 555-1212" ], [ 3, "Bridget Jones", "(666) 555-1212" ], [ 4, "Quincy Jones", "(666) 555-1212" ], ); my $t; ok(1); # loaded... $t = Text::TabularDisplay->new; ok(2); # instantiated... my @columns = @{ shift @data }; $t->columns(@columns); ok(scalar @columns, scalar $t->columns); # columns gets/sets correctly for (@data) { $t->add(@$_); } # How's this for an ugly test? ok($t->render, "+----+---------------+----------------+ | id | name | phone | +----+---------------+----------------+ | 1 | Tom Jones | (666) 555-1212 | | 2 | Barnaby Jones | (666) 555-1212 | | 3 | Bridget Jones | (666) 555-1212 | | 4 | Quincy Jones | (666) 555-1212 | +----+---------------+----------------+"); Text-TabularDisplay-1.38/t/02.t000644 025721 000024 00000001317 12355277445 016615 0ustar00darrenstaff000000 000000 #!/usr/bin/perl # vim: set ft=perl: use strict; use Text::TabularDisplay; use Test; BEGIN { plan tests => 5; } ok(my $t = Text::TabularDisplay->new("name", "favorite color", "shoe size")); ok($t->add("Joe Shmoe", "red", "9 1/2")); ok($t->add("Bob Smith", "chartreuse", "11")); ok($t->add("Jumpin' Jack Flash", "yellow", "12")); ok($t->render, "+--------------------+----------------+-----------+ | name | favorite color | shoe size | +--------------------+----------------+-----------+ | Joe Shmoe | red | 9 1/2 | | Bob Smith | chartreuse | 11 | | Jumpin' Jack Flash | yellow | 12 | +--------------------+----------------+-----------+"); Text-TabularDisplay-1.38/t/03.t000644 025721 000024 00000003400 12355277445 016611 0ustar00darrenstaff000000 000000 #!/usr/bin/perl # vim: set ft=perl: # Yes, this is the same test file (almost) as 01.t, but # this one tests resetting the object, and there needs to # be an object with data in order to test resetting it. use strict; use Text::TabularDisplay; use Test; BEGIN { plan tests => 6; } my @data = ( [ qw(id name phone) ], [ 1, "Tom Jones", "(666) 555-1212" ], [ 2, "Barnaby Jones", "(666) 555-1212" ], [ 3, "Bridget Jones", "(666) 555-1212" ], [ 4, "Quincy Jones", "(666) 555-1212" ], ); my $t; ok(1); # loaded... $t = Text::TabularDisplay->new; ok(2); # instantiated... my @columns = @{ shift @data }; $t->columns(@columns); ok(scalar @columns, scalar $t->columns); # columns gets/sets correctly for (@data) { $t->add(@$_); } # How's this for an ugly test? ok($t->render, "+----+---------------+----------------+ | id | name | phone | +----+---------------+----------------+ | 1 | Tom Jones | (666) 555-1212 | | 2 | Barnaby Jones | (666) 555-1212 | | 3 | Bridget Jones | (666) 555-1212 | | 4 | Quincy Jones | (666) 555-1212 | +----+---------------+----------------+"); # Reset instance $t->reset(@columns); # Ensure that new columns resets instance ok($t->render, "+----+------+-------+ | id | name | phone | +----+------+-------+ +----+------+-------+"); for (@data) { $t->add(@$_); } # ...And repeat the ugly test, to ensure that a re-instanted # object renders the same way. ok($t->render, "+----+---------------+----------------+ | id | name | phone | +----+---------------+----------------+ | 1 | Tom Jones | (666) 555-1212 | | 2 | Barnaby Jones | (666) 555-1212 | | 3 | Bridget Jones | (666) 555-1212 | | 4 | Quincy Jones | (666) 555-1212 | +----+---------------+----------------+"); Text-TabularDisplay-1.38/t/04.t000644 025721 000024 00000001343 12355277445 016616 0ustar00darrenstaff000000 000000 #!/usr/bin/perl # vim: set ft=perl: use strict; use Text::TabularDisplay; use Test; BEGIN { plan tests => 4; } ok(my $t = Text::TabularDisplay->new); ok(scalar $t->columns("name", "favorite color", "shoe size")); ok($t->add("Joe Shmoe", "red", "9 1/2") ->add("Bob Smith", "chartreuse", "11") ->add("Jumpin' Jack Flash", "yellow", "12")); ok($t->render, "+--------------------+----------------+-----------+ | name | favorite color | shoe size | +--------------------+----------------+-----------+ | Joe Shmoe | red | 9 1/2 | | Bob Smith | chartreuse | 11 | | Jumpin' Jack Flash | yellow | 12 | +--------------------+----------------+-----------+"); Text-TabularDisplay-1.38/t/05.t000644 025721 000024 00000001673 12355277445 016625 0ustar00darrenstaff000000 000000 #!/usr/bin/perl # vim: set ft=perl: # Test passing arguments to render() use strict; use Text::TabularDisplay; use Test; BEGIN { plan tests => 7; } ok(my $t = Text::TabularDisplay->new); ok(scalar $t->columns("name", "favorite color", "shoe size")); ok($t->add("Joe Shmoe", "red", "9 1/2")); ok($t->add("Bob Smith", "chartreuse", "11")); ok($t->add("John Doe", "mahogany", 13)); ok($t->render(0, 1), "+-----------+----------------+-----------+ | name | favorite color | shoe size | +-----------+----------------+-----------+ | Joe Shmoe | red | 9 1/2 | | Bob Smith | chartreuse | 11 | +-----------+----------------+-----------+"); ok($t->render(1, 2), "+-----------+----------------+-----------+ | name | favorite color | shoe size | +-----------+----------------+-----------+ | Bob Smith | chartreuse | 11 | | John Doe | mahogany | 13 | +-----------+----------------+-----------+"); Text-TabularDisplay-1.38/t/06.t000644 025721 000024 00000001334 12355277445 016620 0ustar00darrenstaff000000 000000 #!/usr/bin/perl # vim: set ft=perl: use strict; use Text::TabularDisplay; use Test; BEGIN { plan tests => 5; } ok(my $t = Text::TabularDisplay->new("name", "favorite color", "shoe size")); ok($t->add([ "Joe Shmoe", "red", "9 1/2" ])); ok($t->add([ "Bob Smith", "chartreuse", "11" ])); ok($t->add([ "Jumpin' Jack Flash", "yellow", "12" ])); ok($t->render, "+--------------------+----------------+-----------+ | name | favorite color | shoe size | +--------------------+----------------+-----------+ | Joe Shmoe | red | 9 1/2 | | Bob Smith | chartreuse | 11 | | Jumpin' Jack Flash | yellow | 12 | +--------------------+----------------+-----------+"); Text-TabularDisplay-1.38/t/07.t000644 025721 000024 00000001031 12355277445 016613 0ustar00darrenstaff000000 000000 #!/usr/bin/perl # vim: set ft=perl: use strict; use Text::TabularDisplay; use Test; BEGIN { plan tests => 5; } ok(my $t = Text::TabularDisplay->new); ok($t->add("Joe Shmoe", "red", "9 1/2")); ok($t->add("Bob Smith", "chartreuse", "11")); ok($t->add("Jumpin' Jack Flash", "yellow", "12")); ok($t->render, "+--------------------+------------+-------+ | Joe Shmoe | red | 9 1/2 | | Bob Smith | chartreuse | 11 | | Jumpin' Jack Flash | yellow | 12 | +--------------------+------------+-------+"); Text-TabularDisplay-1.38/t/08.t000644 025721 000024 00000001030 12355277445 016613 0ustar00darrenstaff000000 000000 #!/usr/bin/perl # vim: set ft=perl: use strict; use Text::TabularDisplay; use Test; BEGIN { plan tests => 5; } ok(my $t = Text::TabularDisplay->new); ok($t->add("Joe Shmoe", "red", "9 1/2")); ok($t->add("Bob Smith", "chartreuse", "11")); ok($t->add("Jumpin' Jack Flash", "yellow", "12")); ok($t->render, "+--------------------+------------+-------+ | Joe Shmoe | red | 9 1/2 | | Bob Smith | chartreuse | 11 | | Jumpin' Jack Flash | yellow | 12 | +--------------------+------------+-------+"); Text-TabularDisplay-1.38/t/09.t000644 025721 000024 00000003156 12355277445 016627 0ustar00darrenstaff000000 000000 #!/usr/bin/perl # vim: set ft=perl: # Test clone method use strict; use Text::TabularDisplay; use Test; BEGIN { plan tests => 9; } ok(my $t = Text::TabularDisplay->new("name", "favorite color", "shoe size")); ok($t->add("Joe Shmoe", "red", "9 1/2")); ok($t->add("Bob Smith", "chartreuse", "11")); ok($t->add("Jumpin' Jack Flash", "yellow", "12")); ok($t->render, "+--------------------+----------------+-----------+ | name | favorite color | shoe size | +--------------------+----------------+-----------+ | Joe Shmoe | red | 9 1/2 | | Bob Smith | chartreuse | 11 | | Jumpin' Jack Flash | yellow | 12 | +--------------------+----------------+-----------+"); ok(my $u = $t->clone); ok($u->render, # should render the same... "+--------------------+----------------+-----------+ | name | favorite color | shoe size | +--------------------+----------------+-----------+ | Joe Shmoe | red | 9 1/2 | | Bob Smith | chartreuse | 11 | | Jumpin' Jack Flash | yellow | 12 | +--------------------+----------------+-----------+"); ok($t->add("Lemony Snicket", "raw umbder", 6)); ok($u->render, # ...even after original is modified "+--------------------+----------------+-----------+ | name | favorite color | shoe size | +--------------------+----------------+-----------+ | Joe Shmoe | red | 9 1/2 | | Bob Smith | chartreuse | 11 | | Jumpin' Jack Flash | yellow | 12 | +--------------------+----------------+-----------+"); Text-TabularDisplay-1.38/t/10.t000644 025721 000024 00000001343 12355277445 016613 0ustar00darrenstaff000000 000000 #!/usr/bin/perl # vim: set ft=perl: # Test add() chaining. use strict; use Text::TabularDisplay; use Test; BEGIN { plan tests => 3; } ok(my $t = Text::TabularDisplay->new("name", "favorite color", "shoe size")); ok($t->add("Joe Shmoe", "red", "9 1/2") ->add("Bob Smith", "chartreuse", "11") ->add("Jumpin' Jack Flash", "yellow", "12")); ok($t->render, "+--------------------+----------------+-----------+ | name | favorite color | shoe size | +--------------------+----------------+-----------+ | Joe Shmoe | red | 9 1/2 | | Bob Smith | chartreuse | 11 | | Jumpin' Jack Flash | yellow | 12 | +--------------------+----------------+-----------+"); Text-TabularDisplay-1.38/t/11.t000644 025721 000024 00000001373 12355277445 016617 0ustar00darrenstaff000000 000000 #!/usr/bin/perl # vim: set ft=perl: use strict; use Text::TabularDisplay; use Test; BEGIN { plan tests => 4; } my $data = [ [ "Joe Shmoe", "red", "9 1/2" ], [ "Bob Smith", "chartreuse", "11" ], [ "Jumpin' Jack Flash", "yellow", "12" ], ]; ok(my $t = Text::TabularDisplay->new); ok($t->columns("name", "favorite color", "shoe size")); ok($t->populate($data)), ok($t->render, "+--------------------+----------------+-----------+ | name | favorite color | shoe size | +--------------------+----------------+-----------+ | Joe Shmoe | red | 9 1/2 | | Bob Smith | chartreuse | 11 | | Jumpin' Jack Flash | yellow | 12 | +--------------------+----------------+-----------+"); Text-TabularDisplay-1.38/t/12.t000644 025721 000024 00000001721 12355277445 016615 0ustar00darrenstaff000000 000000 #!/usr/bin/perl # vim: set ft=perl: use strict; use Text::TabularDisplay; use Test; BEGIN { plan tests => 7; } ok(my $t = Text::TabularDisplay->new); ok($t->add("Joe Shmoe", "red", "9 1/2")); ok($t->add("Bob Smith", "chartreuse", "11")); ok($t->add("Jumpin' Jack Flash", "yellow", "12")); ok($t->render, "+--------------------+------------+-------+ | Joe Shmoe | red | 9 1/2 | | Bob Smith | chartreuse | 11 | | Jumpin' Jack Flash | yellow | 12 | +--------------------+------------+-------+"); ok($t->columns("name", "favorite color", "shoe size")); ok($t->render, "+--------------------+----------------+-----------+ | name | favorite color | shoe size | +--------------------+----------------+-----------+ | Joe Shmoe | red | 9 1/2 | | Bob Smith | chartreuse | 11 | | Jumpin' Jack Flash | yellow | 12 | +--------------------+----------------+-----------+"); Text-TabularDisplay-1.38/t/13.t000755 025721 000024 00000002477 12355277445 016632 0ustar00darrenstaff000000 000000 #!/usr/bin/perl # vim: set ft=perl: # Following a bug report from David N. Blank-Edelman , # I have added this test to check for silliness in how columns behaves. # Regardless of how many times columns gets called, there should only be # one element in $t->{ _COLUMNS }. use strict; use Text::TabularDisplay; use Test; BEGIN { plan tests => 14; } my $t; $t = Text::TabularDisplay->new; $t->columns(qw{hi there}); ok(scalar @{$t->{ _COLUMNS }}, 1); ok(scalar $t->columns, 2); $t->columns(qw{hi there folks}); ok(scalar @{$t->{ _COLUMNS }}, 1); ok(scalar $t->columns, 3); ok($t->reset); ok($t->add(qw[1 2 3])); ok($t->add(qw[4 5 6])); ok($t->render, "+---+---+---+ | 1 | 2 | 3 | | 4 | 5 | 6 | +---+---+---+"); ok($t->columns(qw[one two three])); ok($t->render, "+-----+-----+-------+ | one | two | three | +-----+-----+-------+ | 1 | 2 | 3 | | 4 | 5 | 6 | +-----+-----+-------+"); ok($t->columns(qw[one two])); ok($t->render, "+-----+-----+-------+ | one | two | | +-----+-----+-------+ | 1 | 2 | 3 | | 4 | 5 | 6 | +-----+-----+-------+"); ok($t->columns(qw[one two three four])); ok($t->render, "+-----+-----+-------+------+ | one | two | three | four | +-----+-----+-------+------+ | 1 | 2 | 3 | | | 4 | 5 | 6 | | +-----+-----+-------+------+"); Text-TabularDisplay-1.38/t/14.t000644 025721 000024 00000002760 12355277445 016623 0ustar00darrenstaff000000 000000 #!/usr/bin/perl # vim: set ft=perl: use strict; use Text::TabularDisplay; use Test; BEGIN { plan tests => 4; } my @data = ( [ "Joe Shmoe", "red", "9 1/2" ], [ qw(foo bar baz) ], [ "Bob Smith", "chartreuse", "11" ], [ qw(foo bar baz) ], [ "Jumpin' Jack Flash", "yellow", "12" ], [ qw(foo bar baz) ], [ "Joe Shmoe", "red", "9 1/2" ], [ qw(foo bar baz) ], [ "Bob Smith", "chartreuse", "11" ], [ qw(foo bar baz) ], [ "Jumpin' Jack Flash", "yellow", "12" ], [ qw(foo bar baz) ], ); ok(my $t = Text::TabularDisplay->new("name", "favorite color", "shoe size")); ok($t->populate([ @data ])); ok($t->items, scalar @data); ok($t->render, "+--------------------+----------------+-----------+ | name | favorite color | shoe size | +--------------------+----------------+-----------+ | Joe Shmoe | red | 9 1/2 | | foo | bar | baz | | Bob Smith | chartreuse | 11 | | foo | bar | baz | | Jumpin' Jack Flash | yellow | 12 | | foo | bar | baz | | Joe Shmoe | red | 9 1/2 | | foo | bar | baz | | Bob Smith | chartreuse | 11 | | foo | bar | baz | | Jumpin' Jack Flash | yellow | 12 | | foo | bar | baz | +--------------------+----------------+-----------+"); Text-TabularDisplay-1.38/t/15.t000644 025721 000024 00000003127 12355277445 016622 0ustar00darrenstaff000000 000000 #!/usr/bin/perl # vim: set ft=perl: use strict; use Text::TabularDisplay; use Test; BEGIN { plan tests => 1; } my @data = ( [ "Joe\nShmoe", "r\ned", "9\n1/2" ], [ qw(foo bar baz) ], [ "Bob Smith", "chartreuse", "11" ], [ qw(foo bar baz) ], [ "Jumpin' Jack Flash", "yellow", "12" ], [ qw(foo bar baz) ], [ "Joe Shmoe", "red", "9 1/2" ], [ qw(foo bar baz) ], [ "Bob Smith", "chartreuse", "11" ], [ qw(foo bar baz) ], [ "Jumpin'\nJack Flash", "yellow", "12" ], [ "foo", "bar", "ba\nz" ], ); my $t = Text::TabularDisplay->new("name", "favorite color", "shoe\nsize"); $t->populate([ @data ]); ok($t->render, "+--------------------+----------------+-------+ | name | favorite color | shoe | | | | size | +--------------------+----------------+-------+ | Joe | r | 9 | | Shmoe | ed | 1/2 | | foo | bar | baz | | Bob Smith | chartreuse | 11 | | foo | bar | baz | | Jumpin' Jack Flash | yellow | 12 | | foo | bar | baz | | Joe Shmoe | red | 9 1/2 | | foo | bar | baz | | Bob Smith | chartreuse | 11 | | foo | bar | baz | | Jumpin' | yellow | 12 | | Jack Flash | | | | foo | bar | ba | | | | z | +--------------------+----------------+-------+"); Text-TabularDisplay-1.38/t/16.t000644 025721 000024 00000001044 12355277445 016617 0ustar00darrenstaff000000 000000 #!/usr/bin/perl # vim: set ft=perl: # Test suggested by Patrick Kuijvenhoven # in https://github.com/dlc/text--tabulardisplay/commit/4b9bd105d9ebaf8ac838e8e993216e4b56d85683#commitcomment-1537151 use strict; use Text::TabularDisplay; use Test; BEGIN { plan tests => 1; } my @data = ( [0, "c"], ["d", "e"], [0, 0], [0, "f"], ); my $t = Text::TabularDisplay->new("a", "b"); $t->populate([ @data ]); ok($t->render, "+---+---+ | a | b | +---+---+ | 0 | c | | d | e | | 0 | 0 | | 0 | f | +---+---+"); Text-TabularDisplay-1.38/t/17.t000644 025721 000024 00000001140 12355277445 016615 0ustar00darrenstaff000000 000000 #!/usr/bin/perl # vim: set ft=perl: # Test suggested by Patrick Kuijvenhoven # in https://github.com/dlc/text--tabulardisplay/commit/4b9bd105d9ebaf8ac838e8e993216e4b56d85683#commitcomment-1540416 use strict; use Text::TabularDisplay; use Test; BEGIN { plan tests => 1; } my @data = ( ["a", "b"], ["", ""], [undef, ""], ["", undef], [undef, undef], [0, 0], ); my $t = Text::TabularDisplay->new("a", "b"); $t->populate([ @data ]); ok($t->render, "+---+---+ | a | b | +---+---+ | a | b | | | | | | | | | | | | | | 0 | 0 | +---+---+"); Text-TabularDisplay-1.38/examples/aliases000755 025721 000024 00000001065 12355277445 021131 0ustar00darrenstaff000000 000000 #!/usr/bin/perl -l use Text::TabularDisplay; use Mail::Address; my @f = readfile("$ENV{'HOME'}/.mutt/aliases"); my $t = Text::TabularDisplay->new("alias", "address"); for (@f) { my @l = split; shift @l; my $alias = shift @l; my $addr = Mail::Address->new("@l"); $addr = $addr->format; $t->add($alias, $addr); } print $t->render; sub readfile { my $file = shift; my @lines; local *F; open F, $file or return; while() { chomp; push @lines, $_; } return @lines; } Text-TabularDisplay-1.38/examples/dump-passwd.pl000755 025721 000024 00000000544 12355517611 022357 0ustar00darrenstaff000000 000000 #!/usr/bin/perl -l use strict; use Text::TabularDisplay; my $t = Text::TabularDisplay->new("uid", "login", "name"); local *PASSWD; open PASSWD, "/etc/passwd" or die "Can't open /etc/passwd: $!"; for () { $t->add((split /:/, $_)[2, 0, 4]) unless /^#/; } close PASSWD or die "Can't close /etc/passwd: $!"; print $t->render; Text-TabularDisplay-1.38/examples/mysql.pl000755 025721 000024 00000005215 12355277445 021270 0ustar00darrenstaff000000 000000 #!/usr/bin/perl # ---------------------------------------------------------------------- # Example usage of Text::TabularDisplay to implement a low-functionality # version of the mysql text monitor. # ---------------------------------------------------------------------- use strict; use vars qw(%opts $prog $histfile); use Carp qw(carp); use DBI; use File::Basename qw(basename); use File::Spec; use Getopt::Long; use Term::ReadLine; use Text::TabularDisplay; $prog = basename $0; $histfile = File::Spec->catfile($ENV{'HOME'}, ".mysql_history"); eval { require MySQL::Config; my %defaults = MySQL::Config::parse_defaults("my", [ qw(client) ]); for (qw(user password host)) { $opts{$_} = $defaults{$_}; } }; GetOptions(\%opts, "user|u=s", "password|p=s", "help|?!", "host=s"); $opts{'host'} ||= "localhost"; if (defined $opts{'help'}) { print STDERR "$prog - mysql command line client emulation\n", "Usage: $prog [OPTIONS] DB_NAME\n\n", "OPTIONS include:\n", " --user=\$username Username to connect as\n", " --password=\$password Password for \$user\n", " --host=\$host Host on which DB_NAME can be found\n", " --help You're reading it.\n\n"; exit(1); } my $db = shift(@ARGV) or die "$prog: Must supply a database name!\n"; # Create the Text::TabularDisplay and Term::ReadLine instances, and # make the database connection. my $table = Text::TabularDisplay->new; my $term = Term::ReadLine->new("mysql"); my $dbh = DBI->connect("dbi:mysql:database=$db;host=$opts{'host'}", $opts{'user'}, $opts{'password'}) or die "Can't connect to $db on $opts{'host'}: $DBI::errstr"; $term->ReadHistory($histfile); while (defined (my $line = $term->readline("mysql> "))) { #$term->AddHistory($line); my $sth; if ($line =~ /^\s*(quit|exit)/) { last; } unless ($sth = $dbh->prepare($line)) { carp "Can't prepare line: " . $dbh->errstr; next; } # Reset the table $table->reset; unless ($sth->execute) { carp "Can't execute query: " . $sth->errstr; next; } # Set the columns my $names = $sth->{'NAME'}; $table->columns(@$names); while (my $row = $sth->fetchrow_arrayref) { # Add data to the table $table->add($row); } $sth->finish; # Print the final version of the table # Note that without the trailing \n, the last line is buffered, # which is pretty ugly... printf "%s\n", $table->render; } $term->WriteHistory($histfile); print "Bye!\n"; exit(0); Text-TabularDisplay-1.38/examples/tdisplay000755 025721 000024 00000002033 12355277445 021335 0ustar00darrenstaff000000 000000 #!/usr/bin/perl -l use strict; use vars qw($VERSION); use vars qw($t $data @data $re); use vars qw(@f $help $separator); $VERSION = "1.00"; use Getopt::Long; use Text::TabularDisplay; GetOptions("f=s" => \@f, "h!" => \$help, "s=s" => \$separator); if (defined $help) { print usage(); exit 0; } $separator = '\s' unless defined $separator; $re = qr($separator); $t = Text::TabularDisplay->new; chomp($data = ); @data = split $re, $data; @f = split /,/, $f[0] if (@f == 1); @f = (0 .. $#data) unless (@f); $t->columns(@data[@f]); while (defined($data = )) { chomp $data; @data = split $re, $data; $t->add(@data[@f]) } print $t->render; sub usage { require File::Basename; my $prog = File::Basename::basename($0); return <<"USAGE"; $prog v.$VERSION $prog [-s \$separator] [-f fields] < data data should be a series of \$separator-delimited lines, which will be displayed in a table, of which the fields defined by -f will be displayed (defaults to all fields). USAGE }