File Coverage

blib/lib/Text/Table/Sprintf.pm
Criterion Covered Total %
statement 0 29 0.0
branch 0 24 0.0
condition 0 9 0.0
subroutine 0 1 0.0
pod 1 1 100.0
total 1 64 1.5


line stmt bran cond sub pod time code
1             # we strive for minimality
2             ## no critic: TestingAndDebugging::RequireUseStrict
3             package Text::Table::Sprintf;
4              
5             #IFUNBUILT
6             # # use strict 'subs', 'vars';
7             # # use warnings;
8             #END IFUNBUILT
9              
10             our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
11             our $DATE = '2023-11-11'; # DATE
12             our $DIST = 'Text-Table-Sprintf'; # DIST
13             our $VERSION = '0.008'; # VERSION
14              
15             our %FEATURES = (
16             set_v => {
17             TextTable => 1,
18             },
19              
20             features => {
21             TextTable => {
22             can_align_cell_containing_wide_character => 0,
23             can_align_cell_containing_color_code => 0,
24             can_align_cell_containing_newline => 0,
25             can_use_box_character => 0,
26             can_customize_border => 0,
27             can_halign => {
28             value => 1,
29             summary => 'Only support l (left) and r (right) alignment, not c (center)',
30             },
31             can_halign_individual_row => 0,
32             can_halign_individual_column => 0,
33             can_halign_individual_cell => 0,
34             can_valign => 0,
35             can_valign_individual_row => 0,
36             can_valign_individual_column => 0,
37             can_valign_individual_cell => 0,
38             can_rowspan => 0,
39             can_colspan => 0,
40             can_color => 0,
41             can_color_theme => 0,
42             can_set_cell_height => 0,
43             can_set_cell_height_of_individual_row => 0,
44             can_set_cell_width => 0,
45             can_set_cell_width_of_individual_column => 0,
46             speed => 'fast',
47             can_hpad => 0,
48             can_hpad_individual_row => 0,
49             can_hpad_individual_column => 0,
50             can_hpad_individual_cell => 0,
51             can_vpad => 0,
52             can_vpad_individual_row => 0,
53             can_vpad_individual_column => 0,
54             can_vpad_individual_cell => 0,
55             },
56             },
57             );
58              
59             sub table {
60 0     0 1   my %params = @_;
61 0 0         my $rows = $params{rows} or die "Must provide rows!";
62             # XXX check that all rows contain the same number of columns
63              
64 0 0         return "" unless @$rows;
65              
66             # determine the width of each column
67 0           my @widths;
68 0           for my $row (@$rows) {
69 0           for (0..$#{$row}) {
  0            
70 0           my $len = length $row->[$_];
71 0 0 0       $widths[$_] = $len if !defined $widths[$_] || $widths[$_] < $len;
72             }
73             }
74              
75             # determine the alignment of each column
76 0 0         my @aligns = @$rows ? (map {'l'} @{$rows->[0]}) : ();
  0            
  0            
77 0 0         if ($params{align}) {
78 0 0         if (ref $params{align} eq 'ARRAY') {
79 0           @aligns = @{ $params{align} }
  0            
80             } else {
81 0           $_ = $params{align} for @aligns;
82             }
83             }
84              
85             # determine the sprintf format for a single row
86             my $rowfmt = join(
87             "",
88 0 0         (map { ($_ ? "" : "|") . " %".($aligns[$_] eq 'r' ? '':'-')."$widths[$_]s |" } 0..$#widths),
  0 0          
89             "\n");
90             my $line = join(
91             "",
92 0 0         (map { ($_ ? "" : "+") . ("-" x ($widths[$_]+2)) . "+" } 0..$#widths),
  0            
93             "\n");
94              
95             # determine the sprintf format for the whole table
96 0           my $tblfmt;
97 0 0         if ($params{header_row}) {
98             $tblfmt = join(
99             "",
100             $line,
101             $rowfmt,
102             $line,
103 0 0 0       (map { $rowfmt . ($params{separate_rows} && $_ < $#{$rows} ? $line : '') } 1..@$rows-1),
  0            
104             $line,
105             );
106             } else {
107             $tblfmt = join(
108             "",
109             $line,
110 0 0 0       (map { $rowfmt . ($params{separate_rows} && $_ < $#{$rows} ? $line : '') } 1..@$rows),
  0            
111             $line,
112             );
113             }
114              
115             # generate table
116 0           sprintf $tblfmt, map { @$_ } @$rows;
  0            
117             }
118              
119             {
120             #IFUNBUILT
121             # # no warnings 'once';
122             #END IFUNBUILT
123             *generate_table = \&table;
124             }
125              
126             1;
127             # ABSTRACT: Generate simple text tables from 2D arrays using sprintf()
128              
129             __END__