NAME

Term::TablePrint - Print a table on the terminal.

VERSION

Version 0.001_02

SYNOPSIS

use Term::TablePrint qw( print_table );

my $table = [ [ 'id', 'name' ],
              [    1, 'Ruth' ],
              [    2, 'John' ],
              [    3, 'Mark' ],
              [    4, 'Nena' ], ];

print_table( $table );


use DBI;

my $dbh = DBI->connect( ... );
my $sth = $dbh->prepare( "SELECT * FROM table" );
$sth->execute();
my $a_ref = $sth->fetchall_arrayref();
unshift @$a_ref, $sth->{NAME};

my $opt = {};
print_table( $a_ref, $opt );

DESCRIPTION

Print a table to the terminal.

print_table provides a cursor. The row on which the cursor is located is highlighted.

The user can scroll through the table with the cursor up/down keys - see USAGE.

If the table has more rows than the terminal, the table is divided up on several sides automatically.

If the cursor reaches the end of a side, the next page is shown automatically (until the last site is reached).

Also if the cursor reaches the topmost line, the previous side is shown automatically if it is not already the first site.

If the terminal is too narrow to print the table, the columns are adjusted to the available breadth automatically.

If the option "table_expand" is enabled and the highlighted row is selected, each column of that row is output in its one line preceded by the column name. This might be useful if the columns were cut due to the too low terminal breadth.

To get a proper output, print_table uses the columns method from Unicode::GCString to calculate the string length.

SUBROUTINES

print_table( $array_ref, [ \%options ] )

The first argument must be a reference to an array of arrays. The first array of the arrays are the column names. The following arrays are the table rows where the elements of these arrays are the field values.

As a second and optional argument it can be passed a reference to a hash which holds the options as pairs of "option-name" and "option-value".

USAGE

Keys to move around

  • the ArrowDown key (or the j key) to move down and ArrowUp key (or the k key) to move up.

  • the PageUp key (or Ctrl-B) to go back one page, the PageDown key (or Ctrl-F) to go forward one page.

  • the Home key (or Ctrl-A) to jump to the first row of the table, the End key (or Ctrl-E) to jump to the last row of the table.

  • the Enter/Return key to close the table or to print the highlighted row if "table_expand" is enabled.

With the option "table_expand" disabled:

- Pressing ENTER jumps to the head of the table.

- Selecting the head of the table closes the table.

With the option "table_expand" enabled:

- If one selects a row twice in succession, the pointer jumps to the head of the table.

- Selecting the head of the table closes the table.

- If the width of the window is changed the user can rewrite the screen by choosing a row.

OPTIONS

Defaults may change in a future release.

tab_width

Set the number of spaces between columns.

Default: 2

colwidth

Set the width the columns should have at least when printed.

Default: 30

undef

Set the string that will be shown on the screen instead of an undefined field.

Default: '' (empty string)

max_rows

Set the maximum number of printed table rows.

To disable the automatic limit set max rows to undef (--).

Default: 50_000

progress_bar

Set the progress bar threshold. If the number of fields (rows x columns) is higher than the threshold a progress bar is shown while preparing the data for the output.

Default: 20_000

table_expand

"table_expand" set to 1 enables printing the chosen table row by pressing the Enter key.

Default: 1

mouse

Set the "mouse" mode (see "OPTIONS/mouse" in Term::Choose).

Default: 0

binary_filter

Print "BNRY" instead of arbitrary binary data.

If the data matches the repexp /[\x00-\x08\x0B-\x0C\x0E-\x1F]/ it is considered arbitrary binary data.

Printing arbitrary binary data could break the output.

Default: 0

REQUIREMENTS

See "REQUIREMENTS" in Term::Choose

Perl version

Requires Perl version 5.10.1 or greater.

SUPPORT

You can find documentation for this module with the perldoc command.

perldoc Term::TablePrint

AUTHOR

Matthäus Kiem <cuer2s@gmail.com>

CREDITS

Thanks to the Perl-Community.de and the people form stackoverflow for the help.

LICENSE AND COPYRIGHT

Copyright 2012-2014 Matthäus Kiem.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl 5.10.0. For details, see the full text of the licenses in the file LICENSE.