| 1 | package Text::Table::TinyColorWide; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | our $DATE = '2016-10-19'; # DATE | |||||||||
| 4 | 1 | our $VERSION = '0.001'; # VERSION | |||||||||
| 5 | |||||||||||
| 6 | 1 | ▶ | use 5.006; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 1 | ▶ | use List::Util qw(); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 1 | ▶ | use Text::ANSI::WideUtil qw(ta_mbswidth ta_mbpad); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | 1 | ▶ | use Exporter qw(import); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 1 | our @EXPORT_OK = qw/ generate_table /; | |||||||||
| 15 | |||||||||||
| 16 | 1 | our $COLUMN_SEPARATOR = '|'; | |||||||||
| 17 | 1 | our $ROW_SEPARATOR = '-'; | |||||||||
| 18 | 1 | our $CORNER_MARKER = '+'; | |||||||||
| 19 | 1 | our $HEADER_ROW_SEPARATOR = '='; | |||||||||
| 20 | 1 | our $HEADER_CORNER_MARKER = 'O'; | |||||||||
| 21 | |||||||||||
| 22 | 0 | ▶ | sub generate_table { | ||||||||
|
Subroutine
generate_table: not called CC=9 SCAR=45.0
Pod
undocumented
| |||||||||||
| 23 | |||||||||||
| 24 | 0 | my %params = @_; | |||||||||
| 25 | 0 | ▶ | my $rows = $params{rows} or die "Must provide rows!"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 26 | |||||||||||
| 27 | # foreach col, get the biggest width | ||||||||||
| 28 | 0 | my $widths = _maxwidths($rows); | |||||||||
| 29 | 0 | my $max_index = _max_array_index($rows); | |||||||||
| 30 | |||||||||||
| 31 | # use that to get the field format and separators | ||||||||||
| 32 | 0 | my $format = _get_format($widths); | |||||||||
| 33 | 0 | my $row_sep = _get_row_separator($widths); | |||||||||
| 34 | 0 | my $head_row_sep = _get_header_row_separator($widths); | |||||||||
| 35 | |||||||||||
| 36 | # here we go... | ||||||||||
| 37 | 0 | my @table; | |||||||||
| 38 | 0 | push @table, $row_sep; | |||||||||
| 39 | |||||||||||
| 40 | # if the first row's a header: | ||||||||||
| 41 | 0 | my $data_begins = 0; | |||||||||
| 42 | 0 | ▶ | if ( $params{header_row} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 43 | 0 | my $header_row = $rows->[0]; | |||||||||
| 44 | 0 | $data_begins++; | |||||||||
| 45 | push @table, sprintf( | ||||||||||
| 46 | $format, | ||||||||||
| 47 | 0 | ▶ | map { ta_mbpad((defined($header_row->[$_]) ? $header_row->[$_] : ''), $widths->[$_]) } (0..$max_index) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 48 | ); | ||||||||||
| 49 | 0 | ▶ | push @table, $params{separate_rows} ? $head_row_sep : $row_sep; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 50 | } | ||||||||||
| 51 | |||||||||||
| 52 | # then the data | ||||||||||
| 53 | 0 | foreach my $row ( @{ $rows }[$data_begins..$#$rows] ) { | |||||||||
| 54 | push @table, sprintf( | ||||||||||
| 55 | $format, | ||||||||||
| 56 | 0 | ▶ | map { ta_mbpad((defined($row->[$_]) ? $row->[$_] : ''), $widths->[$_]) } (0..$max_index) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 57 | ); | ||||||||||
| 58 | 0 | ▶ | push @table, $row_sep if $params{separate_rows}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 59 | } | ||||||||||
| 60 | |||||||||||
| 61 | # this will have already done the bottom if called explicitly | ||||||||||
| 62 | 0 | ▶ | push @table, $row_sep unless $params{separate_rows}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 63 | 0 | return join("\n",grep {$_} @table); | |||||||||
| 64 | } | ||||||||||
| 65 | |||||||||||
| 66 | 0 | ▶ | sub _get_cols_and_rows ($) { | ||||||||
|
Subroutine
_get_cols_and_rows: not called CC=1 SCAR=6.9
| |||||||||||
| 67 | 0 | my $rows = shift; | |||||||||
| 68 | 0 | return ( List::Util::max( map { scalar @$_ } @$rows), scalar @$rows); | |||||||||
| 69 | } | ||||||||||
| 70 | |||||||||||
| 71 | 0 | ▶ | sub _maxwidths { | ||||||||
|
Subroutine
_maxwidths: not called CC=3 SCAR=24.8
| |||||||||||
| 72 | 0 | my $rows = shift; | |||||||||
| 73 | # what's the longest array in this list of arrays? | ||||||||||
| 74 | 0 | my $max_index = _max_array_index($rows); | |||||||||
| 75 | 0 | my $widths = []; | |||||||||
| 76 | 0 | for my $i (0..$max_index) { | |||||||||
| 77 | # go through the $i-th element of each array, find the longest | ||||||||||
| 78 | 0 | ▶ | my $max = List::Util::max(map {defined $$_[$i] ? ta_mbswidth($$_[$i]) : 0} @$rows); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 79 | 0 | push @$widths, $max; | |||||||||
| 80 | } | ||||||||||
| 81 | 0 | return $widths; | |||||||||
| 82 | } | ||||||||||
| 83 | |||||||||||
| 84 | # return highest top-index from all rows in case they're different lengths | ||||||||||
| 85 | 0 | ▶ | sub _max_array_index { | ||||||||
|
Subroutine
_max_array_index: not called CC=1 SCAR=6.9
| |||||||||||
| 86 | 0 | my $rows = shift; | |||||||||
| 87 | 0 | return List::Util::max( map { $#$_ } @$rows ); | |||||||||
| 88 | } | ||||||||||
| 89 | |||||||||||
| 90 | 0 | ▶ | sub _get_format { | ||||||||
|
Subroutine
_get_format: not called CC=1 SCAR=6.9
| |||||||||||
| 91 | 0 | my $widths = shift; | |||||||||
| 92 | 0 | return "$COLUMN_SEPARATOR ".join(" $COLUMN_SEPARATOR ",map { "%s" } @$widths)." $COLUMN_SEPARATOR"; | |||||||||
| 93 | } | ||||||||||
| 94 | |||||||||||
| 95 | 0 | ▶ | sub _get_row_separator { | ||||||||
|
Subroutine
_get_row_separator: not called CC=1 SCAR=6.9
| |||||||||||
| 96 | 0 | my $widths = shift; | |||||||||
| 97 | 0 | return "$CORNER_MARKER$ROW_SEPARATOR".join("$ROW_SEPARATOR$CORNER_MARKER$ROW_SEPARATOR",map { $ROW_SEPARATOR x $_ } @$widths)."$ROW_SEPARATOR$CORNER_MARKER"; | |||||||||
| 98 | } | ||||||||||
| 99 | |||||||||||
| 100 | 0 | ▶ | sub _get_header_row_separator { | ||||||||
|
Subroutine
_get_header_row_separator: not called CC=1 SCAR=6.9
| |||||||||||
| 101 | 0 | my $widths = shift; | |||||||||
| 102 | 0 | return "$HEADER_CORNER_MARKER$HEADER_ROW_SEPARATOR".join("$HEADER_ROW_SEPARATOR$HEADER_CORNER_MARKER$HEADER_ROW_SEPARATOR",map { $HEADER_ROW_SEPARATOR x $_ } @$widths)."$HEADER_ROW_SEPARATOR$HEADER_CORNER_MARKER"; | |||||||||
| 103 | } | ||||||||||
| 104 | |||||||||||
| 105 | # Back-compat: 'table' is an alias for 'generate_table', but isn't exported | ||||||||||
| 106 | { | ||||||||||
| 107 | 1 | ▶ | no warnings 'once'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 108 | 1 | *table = \&generate_table; | |||||||||
| 109 | } | ||||||||||
| 110 | |||||||||||
| 111 | 1 | 1; | |||||||||
| 112 | # ABSTRACT: Text::Table::Tiny + support for colored text & wide character | ||||||||||
| 113 | |||||||||||
| 114 | __END__ | ||||||||||