<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package Term::Table;
use strict;
use warnings;

our $VERSION = '0.018';

use Term::Table::Cell();

use Term::Table::Util qw/term_size uni_length USE_GCS/;
use Scalar::Util qw/blessed/;
use List::Util qw/max sum/;
use Carp qw/croak carp/;

use Term::Table::HashBase qw/rows _columns collapse max_width mark_tail sanitize show_header auto_columns no_collapse header allow_overflow pad/;

sub BORDER_SIZE()   { 4 }    # '| ' and ' |' borders
sub DIV_SIZE()      { 3 }    # ' | ' column delimiter
sub CELL_PAD_SIZE() { 2 }    # space on either side of the |

sub init {
    my $self = shift;

    croak "You cannot have a table with no rows"
        unless $self-&gt;{+ROWS} &amp;&amp; @{$self-&gt;{+ROWS}};

    $self-&gt;{+MAX_WIDTH}   ||= term_size();
    $self-&gt;{+NO_COLLAPSE} ||= {};
    if (ref($self-&gt;{+NO_COLLAPSE}) eq 'ARRAY') {
        $self-&gt;{+NO_COLLAPSE} = {map { ($_ =&gt; 1) } @{$self-&gt;{+NO_COLLAPSE}}};
    }

    if ($self-&gt;{+NO_COLLAPSE} &amp;&amp; $self-&gt;{+HEADER}) {
        my $header = $self-&gt;{+HEADER};
        for(my $idx = 0; $idx &lt; @$header; $idx++) {
            $self-&gt;{+NO_COLLAPSE}-&gt;{$idx} ||= $self-&gt;{+NO_COLLAPSE}-&gt;{$header-&gt;[$idx]};
        }
    }

    $self-&gt;{+PAD} = 4 unless defined $self-&gt;{+PAD};

    $self-&gt;{+COLLAPSE}  = 1 unless defined $self-&gt;{+COLLAPSE};
    $self-&gt;{+SANITIZE}  = 1 unless defined $self-&gt;{+SANITIZE};
    $self-&gt;{+MARK_TAIL} = 1 unless defined $self-&gt;{+MARK_TAIL};

    if($self-&gt;{+HEADER}) {
        $self-&gt;{+SHOW_HEADER}  = 1 unless defined $self-&gt;{+SHOW_HEADER};
    }
    else {
        $self-&gt;{+HEADER}       = [];
        $self-&gt;{+AUTO_COLUMNS} = 1;
        $self-&gt;{+SHOW_HEADER}  = 0;
    }
}

sub columns {
    my $self = shift;

    $self-&gt;regen_columns unless $self-&gt;{+_COLUMNS};

    return $self-&gt;{+_COLUMNS};
}

sub regen_columns {
    my $self = shift;

    my $has_header = $self-&gt;{+SHOW_HEADER} &amp;&amp; @{$self-&gt;{+HEADER}};
    my %new_col = (width =&gt; 0, count =&gt; $has_header ? -1 : 0);

    my $cols = [map { {%new_col} } @{$self-&gt;{+HEADER}}];
    my @rows = @{$self-&gt;{+ROWS}};

    for my $row ($has_header ? ($self-&gt;{+HEADER}, @rows) : (@rows)) {
        for my $ci (0 .. max(@$cols - 1, @$row - 1)) {
            $cols-&gt;[$ci] ||= {%new_col} if $self-&gt;{+AUTO_COLUMNS};
            my $c = $cols-&gt;[$ci] or next;
            $c-&gt;{idx}  ||= $ci;
            $c-&gt;{rows} ||= [];

            my $r = $row-&gt;[$ci];
            $r = Term::Table::Cell-&gt;new(value =&gt; $r)
                unless blessed($r)
                &amp;&amp; ($r-&gt;isa('Term::Table::Cell')
                || $r-&gt;isa('Term::Table::CellStack')
                || $r-&gt;isa('Term::Table::Spacer'));

            $r-&gt;sanitize  if $self-&gt;{+SANITIZE};
            $r-&gt;mark_tail if $self-&gt;{+MARK_TAIL};

            my $rs = $r-&gt;width;
            $c-&gt;{width} = $rs if $rs &gt; $c-&gt;{width};
            $c-&gt;{count}++ if $rs;

            push @{$c-&gt;{rows}} =&gt; $r;
        }
    }

    # Remove any empty columns we can
    @$cols = grep {$_-&gt;{count} &gt; 0 || $self-&gt;{+NO_COLLAPSE}-&gt;{$_-&gt;{idx}}} @$cols
        if $self-&gt;{+COLLAPSE};

    my $current = sum(map {$_-&gt;{width}} @$cols);
    my $border = sum(BORDER_SIZE, $self-&gt;{+PAD}, DIV_SIZE * (@$cols - 1));
    my $total = $current + $border;

    if ($total &gt; $self-&gt;{+MAX_WIDTH}) {
        my $fair = ($self-&gt;{+MAX_WIDTH} - $border) / @$cols;
        if ($fair &lt; 1) {
            return $self-&gt;{+_COLUMNS} = $cols if $self-&gt;{+ALLOW_OVERFLOW};
            croak "Table is too large ($total including $self-&gt;{+PAD} padding) to fit into max-width ($self-&gt;{+MAX_WIDTH})";
        }

        my $under = 0;
        my @fix;
        for my $c (@$cols) {
            if ($c-&gt;{width} &gt; $fair) {
                push @fix =&gt; $c;
            }
            else {
                $under += $c-&gt;{width};
            }
        }

        # Recalculate fairness
        $fair = int(($self-&gt;{+MAX_WIDTH} - $border - $under) / @fix);
        if ($fair &lt; 1) {
            return $self-&gt;{+_COLUMNS} = $cols if $self-&gt;{+ALLOW_OVERFLOW};
            croak "Table is too large ($total including $self-&gt;{+PAD} padding) to fit into max-width ($self-&gt;{+MAX_WIDTH})";
        }

        # Adjust over-long columns
        $_-&gt;{width} = $fair for @fix;
    }

    $self-&gt;{+_COLUMNS} = $cols;
}

sub render {
    my $self = shift;

    my $cols = $self-&gt;columns;
    for my $col (@$cols) {
        for my $cell (@{$col-&gt;{rows}}) {
            $cell-&gt;reset;
        }
    }
    my $width = sum(BORDER_SIZE, $self-&gt;{+PAD}, DIV_SIZE * @$cols, map { $_-&gt;{width} } @$cols);

    #&lt;&lt;&lt; NO-TIDY
    my $border   = '+' . join('+', map { '-' x ($_-&gt;{width}  + CELL_PAD_SIZE) }      @$cols) . '+';
    my $template = '|' . join('|', map { my $w = $_-&gt;{width} + CELL_PAD_SIZE; '%s' } @$cols) . '|';
    my $spacer   = '|' . join('|', map { ' ' x ($_-&gt;{width}  + CELL_PAD_SIZE) }      @$cols) . '|';
    #&gt;&gt;&gt;

    my @out = ($border);
    my ($row, $split, $found) = (0, 0, 0);
    while(1) {
        my @row;

        my $is_spacer = 0;

        for my $col (@$cols) {
            my $r = $col-&gt;{rows}-&gt;[$row];
            unless($r) {
                push @row =&gt; '';
                next;
            }

            my ($v, $vw);

            if ($r-&gt;isa('Term::Table::Cell')) {
                my $lw = $r-&gt;border_left_width;
                my $rw = $r-&gt;border_right_width;
                $vw = $col-&gt;{width} - $lw - $rw;
                $v = $r-&gt;break-&gt;next($vw);
            }
            elsif ($r-&gt;isa('Term::Table::CellStack')) {
                ($v, $vw) = $r-&gt;break-&gt;next($col-&gt;{width});
            }
            elsif ($r-&gt;isa('Term::Table::Spacer')) {
                $is_spacer = 1;
            }

            if ($is_spacer) {
                last;
            }
            elsif (defined $v) {
                $found++;
                my $bcolor = $r-&gt;border_color || '';
                my $vcolor = $r-&gt;value_color  || '';
                my $reset  = $r-&gt;reset_color  || '';

                if (my $need = $vw - uni_length($v)) {
                    $v .= ' ' x $need;
                }

                my $rt = "${reset}${bcolor}\%s${reset} ${vcolor}\%s${reset} ${bcolor}\%s${reset}";
                push @row =&gt; sprintf($rt, $r-&gt;border_left || '', $v, $r-&gt;border_right || '');
            }
            else {
                push @row =&gt; ' ' x ($col-&gt;{width} + 2);
            }
        }

        if (!grep {$_ &amp;&amp; m/\S/} @row) {
            last unless $found || $is_spacer;

            push @out =&gt; $border if $row == 0 &amp;&amp; $self-&gt;{+SHOW_HEADER} &amp;&amp; @{$self-&gt;{+HEADER}};
            push @out =&gt; $spacer if $split &gt; 1 || $is_spacer;

            $row++;
            $split = 0;
            $found = 0;

            next;
        }

        if ($split == 1 &amp;&amp; @out &gt; 1 &amp;&amp; $out[-2] ne $border &amp;&amp; $out[-2] ne $spacer) {
            my $last = pop @out;
            push @out =&gt; ($spacer, $last);
        }

        push @out =&gt; sprintf($template, @row);
        $split++;
    }

    pop @out while @out &amp;&amp; $out[-1] eq $spacer;

    unless (USE_GCS) {
        for my $row (@out) {
            next unless $row =~ m/[^\x00-\x7F]/;
            unshift @out =&gt; "Unicode::GCString is not installed, table may not display all unicode characters properly";
            last;
        }
    }

    return (@out, $border);
}

sub display {
    my $self = shift;
    my ($fh) = @_;

    my @parts = map "$_\n", $self-&gt;render;

    print $fh @parts if $fh;
    print @parts;
}

1;

__END__


=pod

=encoding UTF-8

=head1 NAME

Term::Table - Format a header and rows into a table

=head1 DESCRIPTION

This is used by some failing tests to provide diagnostics about what has gone
wrong. This module is able to format rows of data into tables.

=head1 SYNOPSIS

    use Term::Table;

    my $table = Term::Table-&gt;new(
        max_width      =&gt; 80,    # Defaults to terminal size
        pad            =&gt; 4,     # Extra padding between table and max-width (defaults to 4)
        allow_overflow =&gt; 0,     # Default is 0, when off an exception will be thrown if the table is too big
        collapse       =&gt; 1,     # Do not show empty columns

        header =&gt; ['name', 'age', 'hair color'],
        rows   =&gt; [
            ['Fred Flintstone',  2000000, 'black'],
            ['Wilma Flintstone', 1999995, 'red'],
            ...
        ],
    );

    say $_ for $table-&gt;render;

This prints a table like this:

    +------------------+---------+------------+
    | name             | age     | hair color |
    +------------------+---------+------------+
    | Fred Flintstone  | 2000000 | black      |
    | Wilma Flintstone | 1999995 | red        |
    | ...              | ...     | ...        |
    +------------------+---------+------------+

=head1 INTERFACE

    use Term::Table;
    my $table = Term::Table-&gt;new(...);

=head2 OPTIONS

=over 4

=item header =&gt; [ ... ]

If you want a header specify it here.
This takes an arrayref with each columns heading.

=item rows =&gt; [ [...], [...], ... ]

This should be an arrayref containing an arrayref per row.

=item collapse =&gt; $bool

Use this if you want to hide empty columns, that is any column that has no data
in any row. Having a header for the column will not effect collapse.

=item max_width =&gt; $num

Set the maximum width of the table, the table may not be this big, but it will
be no bigger. If none is specified it will attempt to find the width of your
terminal and use that, otherwise it falls back to the terminal width or C&lt;80&gt;.

=item pad =&gt; $num

Defaults to C&lt;4&gt;, extra padding for row width calculations.
Default is for legacy support.
Set this to C&lt;0&gt; to turn padding off.

=item allow_overflow =&gt; $bool

Defaults to C&lt;0&gt;. If this is off then an exception will be thrown if the table
cannot be made to fit inside the max-width. If this is set to C&lt;1&gt; then the
table will be rendered anyway, larger than max-width, if it is not possible
to stay within the max-width. In other words this turns max-width from a
hard-limit to a soft recommendation.

=item sanitize =&gt; $bool

This will sanitize all the data in the table such that newlines, control
characters, and all whitespace except for ASCII 20 C&lt;' '&gt; are replaced with
escape sequences. This prevents newlines, tabs, and similar whitespace from
disrupting the table.

B&lt;Note:&gt; newlines are marked as C&lt;\n&gt;, but a newline is also inserted into the
data so that it typically displays in a way that is useful to humans.

Example:

    my $field = "foo\nbar\nbaz\n";

    print join "\n" =&gt; table(
        sanitize =&gt; 1,
        rows =&gt; [
            [$field,      'col2'     ],
            ['row2 col1', 'row2 col2']
        ]
    );

Prints:

    +-----------------+-----------+
    | foo\n           | col2      |
    | bar\n           |           |
    | baz\n           |           |
    |                 |           |
    | row2 col1       | row2 col2 |
    +-----------------+-----------+

So it marks the newlines by inserting the escape sequence, but it also shows
the data across as many lines as it would normally display.

=item mark_tail =&gt; $bool

This will replace the last whitespace character of any trailing whitespace with
its escape sequence. This makes it easier to notice trailing whitespace when
comparing values.

=item show_header =&gt; $bool

Set this to false to hide the header. This defaults to true if the header is
set, false if no header is provided.

=item auto_columns =&gt; $bool

Set this to true to automatically add columns that are not named in the header.
This defaults to false if a header is provided, and defaults to true when there
is no header.

=item no_collapse =&gt; [ $col_num_a, $col_num_b, ... ]

=item no_collapse =&gt; [ $col_name_a, $col_name_b, ... ]

=item no_collapse =&gt; { $col_num_a =&gt; 1, $col_num_b =&gt; 1, ... }

=item no_collapse =&gt; { $col_name_a =&gt; 1, $col_name_b =&gt; 1, ... }

Specify (by number and/or name) columns that should not be removed when empty.
The 'name' form only works when a header is specified. There is currently no
protection to insure that names you specify are actually in the header, invalid
names are ignored, patches to fix this will be happily accepted.

=back

=head1 NOTE ON UNICODE/WIDE CHARACTERS

Some unicode characters, such as C&lt;å©§&gt; (C&lt;U+5A67&gt;) are wider than others. These
will render just fine if you C&lt;use utf8;&gt; as necessary, and
L&lt;Unicode::GCString&gt; is installed, however if the module is not installed there
will be anomalies in the table:

    +-----+-----+---+
    | a   | b   | c |
    +-----+-----+---+
    | å©§ | x   | y |
    | x   | y   | z |
    | x   | å©§ | z |
    +-----+-----+---+

=head1 SOURCE

The source code repository for C&lt;Term-Table&gt; can be found at
L&lt;https://github.com/exodist/Term-Table/&gt;.

=head1 MAINTAINERS

=over 4

=item Chad Granum E&lt;lt&gt;exodist@cpan.orgE&lt;gt&gt;

=back

=head1 AUTHORS

=over 4

=item Chad Granum E&lt;lt&gt;exodist@cpan.orgE&lt;gt&gt;

=back

=head1 COPYRIGHT

Copyright 2016 Chad Granum E&lt;lt&gt;exodist@cpan.orgE&lt;gt&gt;.

This program is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.

See L&lt;https://dev.perl.org/licenses/&gt;

=cut
</pre></body></html>