This file is indexed.

/usr/share/perl5/Text/TabularDisplay.pm is in libtext-tabulardisplay-perl 1.22-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
package Text::TabularDisplay;

# -------------------------------------------------------------------
# $Id: TabularDisplay.pm,v 1.2 2005/12/09 21:04:48 dlc Exp $
# -------------------------------------------------------------------
# Text::TabularDisplay - Display text in formatted table output
# Copyright (C) 2004 darren chamberlain <darren@cpan.org>
#
# 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., 59 Temple Place, Suite 330, Boston, MA
# 02111-1307  USA
# -------------------------------------------------------------------

use strict;
use integer;
use vars qw($VERSION $REVISION);

$VERSION = "1.22";  # $Date: 2005/12/09 21:04:48 $
$REVISION = sprintf "%d.%02d", q$Revision: 1.2 $ =~ /(\d+)\.(\d+)/;

# ---======================= 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 } } = ();
        _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) {
        _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 ($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";
        $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) = @_;

    my $len = 0;
    for (split "\n", $str) {
        $len = length
            if $len < length;
    }

    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 auxilliary convenience methods: clone(), items(),
reset(), populate(), and paginate().

=over

=item B<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 L<Text::TabularDisplay/clone>.

=item B<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 I<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.

=item B<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);

=item B<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 I<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    |      |
    +-----+-----+-------+------+

=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 legnth() 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 E<lt>darren@cpan.orgE<gt>

=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

=head1 VERSION

This documentation describes C<Text::TabularDisplay> version 1.20.