1Test2::Util::Table(3) User Contributed Perl DocumentationTest2::Util::Table(3)
2
3
4

NAME

6       Test2::Util::Table - Format a header and rows into a table
7

DESCRIPTION

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

SYNOPSIS

14           use Test2::Util::Table qw/table/;
15
16           my @table = table(
17               max_width => 80,
18               collapse => 1, # Do not show empty columns
19               header => [ 'name', 'age', 'hair color' ],
20               rows => [
21                   [ 'Fred Flinstone',  2000000, 'black' ],
22                   [ 'Wilma Flinstone', 1999995, 'red' ],
23                   ...,
24               ],
25           );
26
27           # The @table array contains each line of the table, no newlines added.
28           say $_ for @table;
29
30       This prints a table like this:
31
32           +-----------------+---------+------------+
33           | name            | age     | hair color |
34           +-----------------+---------+------------+
35           | Fred Flinstone  | 2000000 | black      |
36           | Wilma Flinstone | 1999995 | red        |
37           | ...             | ...     | ...        |
38           +-----------------+---------+------------+
39

EXPORTS

41   @rows = table(...)
42       The function returns a list of lines, lines do not have the newline
43       "\n" character appended.
44
45       Options:
46
47       header => [ ... ]
48           If you want a header specify it here. This takes an arrayref with
49           each columns heading.
50
51       rows => [ [...], [...], ... ]
52           This should be an arrayref containing an arrayref per row.
53
54       collapse => $bool
55           Use this if you want to hide empty columns, that is any column that
56           has no data in any row. Having a header for the column will not
57           effect collapse.
58
59       max_width => $num
60           Set the maximum width of the table, the table may not be this big,
61           but it will be no bigger. If none is specified it will attempt to
62           find the width of your terminal and use that, otherwise it falls
63           back to 80.
64
65       sanitize => $bool
66           This will sanitize all the data in the table such that newlines,
67           control characters, and all whitespace except for ASCII 20 ' ' are
68           replaced with escape sequences. This prevents newlines, tabs, and
69           similar whitespace from disrupting the table.
70
71           Note: newlines are marked as '\n', but a newline is also inserted
72           into the data so that it typically displays in a way that is useful
73           to humans.
74
75           Example:
76
77               my $field = "foo\nbar\nbaz\n";
78
79               print join "\n" => table(
80                   sanitize => 1,
81                   rows => [
82                       [$field,      'col2'     ],
83                       ['row2 col1', 'row2 col2']
84                   ]
85               );
86
87           Prints:
88
89               +-----------------+-----------+
90               | foo\n           | col2      |
91               | bar\n           |           |
92               | baz\n           |           |
93               |                 |           |
94               | row2 col1       | row2 col2 |
95               +-----------------+-----------+
96
97           So it marks the newlines by inserting the escape sequence, but it
98           also shows the data across as many lines as it would normally
99           display.
100
101       mark_tail => $bool
102           This will replace the last whitespace character of any trailing
103           whitespace with its escape sequence. This makes it easier to notice
104           trailing whitespace when comparing values.
105
106   my $cols = term_size()
107       Attempts to find the width in columns (characters) of the current
108       terminal.  Returns 80 as a safe bet if it cannot find it another way.
109

NOTE ON UNICODE/WIDE CHARACTERS

111       Some unicode characters, such as "婧" ("U+5A67") are wider than others.
112       These will render just fine if you "use utf8;" as necessary, and
113       Unicode::GCString is installed, however if the module is not installed
114       there will be anomalies in the table:
115
116           +-----+-----+---+
117           | a   | b   | c |
118           +-----+-----+---+
119           | 婧 | x   | y |
120           | x   | y   | z |
121           | x   | 婧 | z |
122           +-----+-----+---+
123

SOURCE

125       The source code repository for Test2-Suite can be found at
126       https://github.com/Test-More/Test2-Suite/.
127

MAINTAINERS

129       Chad Granum <exodist@cpan.org>
130

AUTHORS

132       Chad Granum <exodist@cpan.org>
133
135       Copyright 2018 Chad Granum <exodist@cpan.org>.
136
137       This program is free software; you can redistribute it and/or modify it
138       under the same terms as Perl itself.
139
140       See http://dev.perl.org/licenses/
141
142
143
144perl v5.38.0                      2023-07-21             Test2::Util::Table(3)
Impressum