/usr/share/perl5/Text/SimpleTable.pm is in libtext-simpletable-perl 2.03-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 | # Copyright (C) 2005-2010, Sebastian Riedel.
package Text::SimpleTable;
use strict;
use warnings;
our $VERSION = '2.03';
# Top
our $TOP_LEFT = '.-';
our $TOP_BORDER = '-';
our $TOP_SEPARATOR = '-+-';
our $TOP_RIGHT = '-.';
# Middle
our $MIDDLE_LEFT = '+-';
our $MIDDLE_BORDER = '-';
our $MIDDLE_SEPARATOR = '-+-';
our $MIDDLE_RIGHT = '-+';
# Left
our $LEFT_BORDER = '| ';
our $SEPARATOR = ' | ';
our $RIGHT_BORDER = ' |';
# Bottom
our $BOTTOM_LEFT = "'-";
our $BOTTOM_SEPARATOR = "-+-";
our $BOTTOM_BORDER = '-';
our $BOTTOM_RIGHT = "-'";
# Wrapper
our $WRAP = '-';
sub new {
my ($class, @args) = @_;
# Instantiate
$class = ref $class || $class;
my $self = bless {}, $class;
# Columns and titles
my $cache = [];
my $max = 0;
for my $arg (@args) {
my $width;
my $name;
if (ref $arg) {
$width = $arg->[0];
$name = $arg->[1];
}
else { $width = $arg }
# Fix size
$width = 2 if $width < 2;
# Wrap
my $title = $name ? $self->_wrap($name, $width) : [];
# Column
my $col = [$width, [], $title];
$max = @{$col->[2]} if $max < @{$col->[2]};
push @$cache, $col;
}
# Padding
for my $col (@$cache) {
push @{$col->[2]}, '' while @{$col->[2]} < $max;
}
$self->{columns} = $cache;
return $self;
}
# The implementation is not very elegant, but gets the job done very well
sub draw {
my $self = shift;
# Shortcut
return unless $self->{columns};
my $rows = @{$self->{columns}->[0]->[1]} - 1;
my $columns = @{$self->{columns}} - 1;
my $output = '';
# Top border
for my $j (0 .. $columns) {
my $column = $self->{columns}->[$j];
my $width = $column->[0];
my $text = $TOP_BORDER x $width;
if (($j == 0) && ($columns == 0)) {
$text = "$TOP_LEFT$text$TOP_RIGHT";
}
elsif ($j == 0) { $text = "$TOP_LEFT$text$TOP_SEPARATOR" }
elsif ($j == $columns) { $text = "$text$TOP_RIGHT" }
else { $text = "$text$TOP_SEPARATOR" }
$output .= $text;
}
$output .= "\n";
my $title = 0;
for my $column (@{$self->{columns}}) {
$title = @{$column->[2]} if $title < @{$column->[2]};
}
if ($title) {
# Titles
for my $i (0 .. $title - 1) {
for my $j (0 .. $columns) {
my $column = $self->{columns}->[$j];
my $width = $column->[0];
my $text = $column->[2]->[$i] || '';
$text = sprintf "%-${width}s", $text;
if (($j == 0) && ($columns == 0)) {
$text = "$LEFT_BORDER$text$RIGHT_BORDER";
}
elsif ($j == 0) { $text = "$LEFT_BORDER$text$SEPARATOR" }
elsif ($j == $columns) { $text = "$text$RIGHT_BORDER" }
else { $text = "$text$SEPARATOR" }
$output .= $text;
}
$output .= "\n";
}
# Title separator
$output .= $self->_draw_hr;
}
# Rows
for my $i (0 .. $rows) {
# Check for hr
if (!grep { defined $self->{columns}->[$_]->[1]->[$i] } 0 .. $columns)
{
$output .= $self->_draw_hr;
next;
}
for my $j (0 .. $columns) {
my $column = $self->{columns}->[$j];
my $width = $column->[0];
my $text = (defined $column->[1]->[$i]) ? $column->[1]->[$i] : '';
$text = sprintf "%-${width}s", $text;
if (($j == 0) && ($columns == 0)) {
$text = "$LEFT_BORDER$text$RIGHT_BORDER";
}
elsif ($j == 0) { $text = "$LEFT_BORDER$text$SEPARATOR" }
elsif ($j == $columns) { $text = "$text$RIGHT_BORDER" }
else { $text = "$text$SEPARATOR" }
$output .= $text;
}
$output .= "\n";
}
# Bottom border
for my $j (0 .. $columns) {
my $column = $self->{columns}->[$j];
my $width = $column->[0];
my $text = $BOTTOM_BORDER x $width;
if (($j == 0) && ($columns == 0)) {
$text = "$BOTTOM_LEFT$text$BOTTOM_RIGHT";
}
elsif ($j == 0) { $text = "$BOTTOM_LEFT$text$BOTTOM_SEPARATOR" }
elsif ($j == $columns) { $text = "$text$BOTTOM_RIGHT" }
else { $text = "$text$BOTTOM_SEPARATOR" }
$output .= $text;
}
$output .= "\n";
return $output;
}
sub hr {
my $self = shift;
for (0 .. @{$self->{columns}} - 1) {
push @{$self->{columns}->[$_]->[1]}, undef;
}
return $self;
}
sub row {
my ($self, @texts) = @_;
my $size = @{$self->{columns}} - 1;
# Shortcut
return $self if $size < 0;
for (1 .. $size) {
last if $size <= @texts;
push @texts, '';
}
my $cache = [];
my $max = 0;
for my $i (0 .. $size) {
my $text = shift @texts;
my $column = $self->{columns}->[$i];
my $width = $column->[0];
my $pieces = $self->_wrap($text, $width);
push @{$cache->[$i]}, @$pieces;
$max = @$pieces if @$pieces > $max;
}
for my $col (@{$cache}) { push @{$col}, '' while @{$col} < $max }
for my $i (0 .. $size) {
my $column = $self->{columns}->[$i];
my $store = $column->[1];
push @{$store}, @{$cache->[$i]};
}
return $self;
}
sub _draw_hr {
my $self = shift;
my $columns = @{$self->{columns}} - 1;
my $output = '';
for my $j (0 .. $columns) {
my $column = $self->{columns}->[$j];
my $width = $column->[0];
my $text = $MIDDLE_BORDER x $width;
if (($j == 0) && ($columns == 0)) {
$text = "$MIDDLE_LEFT$text$MIDDLE_RIGHT";
}
elsif ($j == 0) { $text = "$MIDDLE_LEFT$text$MIDDLE_SEPARATOR" }
elsif ($j == $columns) { $text = "$text$MIDDLE_RIGHT" }
else { $text = "$text$MIDDLE_SEPARATOR" }
$output .= $text;
}
$output .= "\n";
return $output;
}
# Wrap text
sub _wrap {
my ($self, $text, $width) = @_;
my @cache;
my @parts = split "\n", $text;
for my $part (@parts) {
while (length $part > $width) {
my $subtext;
$subtext = substr $part, 0, $width - length($WRAP), '';
push @cache, "$subtext$WRAP";
}
push @cache, $part if defined $part;
}
return \@cache;
}
1;
__END__
=head1 NAME
Text::SimpleTable - Simple Eyecandy ASCII Tables
=head1 SYNOPSIS
use Text::SimpleTable;
my $t1 = Text::SimpleTable->new(5, 10);
$t1->row('foobarbaz', 'yadayadayada');
print $t1->draw;
.-------+------------.
| foob- | yadayaday- |
| arbaz | ada |
'-------+------------'
my $t2 = Text::SimpleTable->new([5, 'Foo'], [10, 'Bar']);
$t2->row('foobarbaz', 'yadayadayada');
$t2->row('barbarbarbarbar', 'yada');
print $t2->draw;
.-------+------------.
| Foo | Bar |
+-------+------------+
| foob- | yadayaday- |
| arbaz | ada |
| barb- | yada |
| arba- | |
| rbar- | |
| bar | |
'-------+------------'
my $t3 = Text::SimpleTable->new([5, 'Foo'], [10, 'Bar']);
$t3->row('foobarbaz', 'yadayadayada');
$t3->hr;
$t3->row('barbarbarbarbar', 'yada');
print $t3->draw;
.-------+------------.
| Foo | Bar |
+-------+------------+
| foob- | yadayaday- |
| arbaz | ada |
+-------+------------+
| barb- | yada |
| arba- | |
| rbar- | |
| bar | |
'-------+------------'
=head1 DESCRIPTION
Simple eyecandy ASCII tables.
=head1 METHODS
L<Text::SimpleTable> implements the following methods.
=head2 C<new>
my $t = Text::SimpleTable->new(5, 10);
my $t = Text::SimpleTable->new([5, 'Col1', 10, 'Col2']);
=head2 C<draw>
my $ascii = $t->draw;
=head2 C<hr>
$t = $t->hr;
=head2 C<row>
$t = $t->row('col1 data', 'col2 data');
=head1 AUTHOR
Sebastian Riedel, C<sri@cpan.org>.
=head1 MAINTAINER
Marcus Ramberg C<mramberg@cpan.org>.
=head1 CREDITS
In alphabetical order:
Brian Cassidy
=head1 COPYRIGHT AND LICENSE
Copyright (C) 2005-2010, Sebastian Riedel.
This program is free software, you can redistribute it and/or modify it under
the terms of the Artistic License version 2.0.
=cut
|